feat(compiler): commit deferred client state writes

This commit is contained in:
2026-08-09 13:21:34 +05:30
parent 3e77a621f5
commit b2e83bc941
3 changed files with 49 additions and 1 deletions
+9 -1
View File
@@ -268,6 +268,13 @@ function functionEntry(
`if (!Object.is(${name}, __wrnexusInitialState[${JSON.stringify(name)}])) context.state.${name} = ${name};`,
)
.join("\n");
const commitBinding = stateNames.length
? `const __wrnexusCommit = () => { ${copyBack} };
${!parameterNames.has("commit") && !declaredLocals.has("commit") ? "const commit = __wrnexusCommit;" : ""}
${!parameterNames.has("setTimeout") && !declaredLocals.has("setTimeout") ? `const setTimeout = (callback, delay, ...args) => globalThis.setTimeout(() => {
try { return callback(...args); } finally { __wrnexusCommit(); }
}, delay);` : ""}`
: "";
const body = eraseFunctionTypes(fn.body);
const runtimeBindings = [
!parameterNames.has("output") ? "const output = context.output;" : "",
@@ -281,12 +288,13 @@ function functionEntry(
${initialStateSnapshot}
${stateAliases}
${propAliases}
${commitBinding}
${peerAliases}
${runtimeBindings}
try {
${body}
} finally {
${copyBack}
${stateNames.length ? "__wrnexusCommit();" : ""}
}
}`;
}