perf(compiler): deduplicate client peer state synchronization

This commit is contained in:
2026-08-09 12:57:37 +05:30
parent e4502f2437
commit 64bb0e366a
+19 -20
View File
@@ -240,29 +240,28 @@ function functionEntry(
const syncStateFromContext = stateNames const syncStateFromContext = stateNames
.map((name) => `${name} = context.state.${name};`) .map((name) => `${name} = context.state.${name};`)
.join(" "); .join(" ");
const peerAliases = functionAliases const peerAliases = !stateNames.length
.map((name) => { ? functionAliases
const call = `context.functions[${JSON.stringify(name)}](...__wrnexusPeerArgs)`; .map((name) => `const ${name} = (...__wrnexusPeerArgs) => context.functions[${JSON.stringify(name)}](...__wrnexusPeerArgs);`)
if (!stateNames.length) { .join("\n")
return `const ${name} = (...__wrnexusPeerArgs) => ${call};`; : `const __wrnexusFlush = () => { ${syncStateToContext} };
} const __wrnexusRestore = () => { ${syncStateFromContext} };
return `const ${name} = (...__wrnexusPeerArgs) => { const __wrnexusPeer = (name, args) => {
${syncStateToContext} __wrnexusFlush();
let __wrnexusPeerResult; let result;
try { try {
__wrnexusPeerResult = ${call}; result = context.functions[name](...args);
} catch (__wrnexusPeerError) { } catch (error) {
${syncStateFromContext} __wrnexusRestore();
throw __wrnexusPeerError; throw error;
} }
if (__wrnexusPeerResult && typeof __wrnexusPeerResult.then === "function") { if (result && typeof result.then === "function") {
return Promise.resolve(__wrnexusPeerResult).finally(() => { ${syncStateFromContext} }); return Promise.resolve(result).finally(__wrnexusRestore);
} }
${syncStateFromContext} __wrnexusRestore();
return __wrnexusPeerResult; return result;
};`; };
}) ${functionAliases.map((name) => `const ${name} = (...__wrnexusPeerArgs) => __wrnexusPeer(${JSON.stringify(name)}, __wrnexusPeerArgs);`).join("\n")}`;
.join("\n");
const copyBack = stateNames const copyBack = stateNames
.map( .map(
(name) => (name) =>