perf(strategy): fuse validated state copying and freezing (P08)
This commit is contained in:
parent
dbb6f4f5d1
commit
70d173a908
6 changed files with 6016 additions and 9 deletions
|
|
@ -26,7 +26,17 @@ export function freeze<T>(value: T): DeepReadonly<T> {
|
|||
|
||||
/** Check descriptors before reading values: JSON.stringify would silently lose
|
||||
* symbols/custom array keys, invoke accessors, or rewrite non-JSON objects. */
|
||||
export function stateCopy(input: unknown, ancestors = new Set<object>()): unknown {
|
||||
export function stateCopy(input: unknown): unknown {
|
||||
return copyJsonState(input, new Set(), false);
|
||||
}
|
||||
|
||||
/** Validate and detach callback state, freezing each completed container once.
|
||||
* Never freeze author-owned input or trust its current frozen status. */
|
||||
export function frozenStateCopy(input: unknown): DeepReadonly<unknown> {
|
||||
return copyJsonState(input, new Set(), true);
|
||||
}
|
||||
|
||||
function copyJsonState(input: unknown, ancestors: Set<object>, frozen: boolean): unknown {
|
||||
if (input === null || typeof input === 'string' || typeof input === 'boolean') {
|
||||
return input;
|
||||
}
|
||||
|
|
@ -44,7 +54,6 @@ export function stateCopy(input: unknown, ancestors = new Set<object>()): unknow
|
|||
if (Object.getPrototypeOf(input) !== (array ? Array.prototype : Object.prototype)) {
|
||||
throw invalid();
|
||||
}
|
||||
const descriptors = Object.getOwnPropertyDescriptors(input);
|
||||
const keys = Reflect.ownKeys(input);
|
||||
if (keys.some(key => typeof key !== 'string')) {
|
||||
throw invalid();
|
||||
|
|
@ -52,20 +61,27 @@ export function stateCopy(input: unknown, ancestors = new Set<object>()): unknow
|
|||
ancestors.add(input);
|
||||
try {
|
||||
const data = (key: string): unknown => {
|
||||
const descriptor = descriptors[key];
|
||||
const descriptor = Object.getOwnPropertyDescriptor(input, key);
|
||||
if (!descriptor?.enumerable || !('value' in descriptor)) {
|
||||
throw invalid();
|
||||
}
|
||||
return stateCopy(descriptor.value, ancestors);
|
||||
return copyJsonState(descriptor.value, ancestors, frozen);
|
||||
};
|
||||
let output: unknown[] | Record<string, unknown>;
|
||||
if (array) {
|
||||
if (keys.length !== input.length + 1) {
|
||||
throw invalid();
|
||||
}
|
||||
return Array.from({ length: input.length }, (_, index) => data(String(index)));
|
||||
const values = new Array<unknown>(input.length);
|
||||
for (let index = 0; index < input.length; index++) {
|
||||
values[index] = data(String(index));
|
||||
}
|
||||
output = values;
|
||||
} else {
|
||||
// fromEntries preserves an own '__proto__' key without changing prototype.
|
||||
output = Object.fromEntries((keys as string[]).sort().map(key => [key, data(key)]));
|
||||
}
|
||||
// fromEntries preserves an own '__proto__' key without changing prototype.
|
||||
return Object.fromEntries((keys as string[]).sort().map(key => [key, data(key)]));
|
||||
return frozen ? Object.freeze(output) : output;
|
||||
} finally {
|
||||
ancestors.delete(input);
|
||||
}
|
||||
|
|
@ -185,7 +201,7 @@ export function stageStrategyDecision(
|
|||
if (!output || typeof output !== 'object' || 'then' in output) {
|
||||
throw new Error('Strategy decide must synchronously return nextState and intent');
|
||||
}
|
||||
const state = freeze(stateCopy(output.nextState));
|
||||
const state = frozenStateCopy(output.nextState);
|
||||
const decision = parseFinancialStrategyDecision({
|
||||
version: 1,
|
||||
sessionId: sessionId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue