perf(compiler): deduplicate client peer state synchronization
This commit is contained in:
@@ -240,29 +240,28 @@ function functionEntry(
|
||||
const syncStateFromContext = stateNames
|
||||
.map((name) => `${name} = context.state.${name};`)
|
||||
.join(" ");
|
||||
const peerAliases = functionAliases
|
||||
.map((name) => {
|
||||
const call = `context.functions[${JSON.stringify(name)}](...__wrnexusPeerArgs)`;
|
||||
if (!stateNames.length) {
|
||||
return `const ${name} = (...__wrnexusPeerArgs) => ${call};`;
|
||||
}
|
||||
return `const ${name} = (...__wrnexusPeerArgs) => {
|
||||
${syncStateToContext}
|
||||
let __wrnexusPeerResult;
|
||||
const peerAliases = !stateNames.length
|
||||
? functionAliases
|
||||
.map((name) => `const ${name} = (...__wrnexusPeerArgs) => context.functions[${JSON.stringify(name)}](...__wrnexusPeerArgs);`)
|
||||
.join("\n")
|
||||
: `const __wrnexusFlush = () => { ${syncStateToContext} };
|
||||
const __wrnexusRestore = () => { ${syncStateFromContext} };
|
||||
const __wrnexusPeer = (name, args) => {
|
||||
__wrnexusFlush();
|
||||
let result;
|
||||
try {
|
||||
__wrnexusPeerResult = ${call};
|
||||
} catch (__wrnexusPeerError) {
|
||||
${syncStateFromContext}
|
||||
throw __wrnexusPeerError;
|
||||
result = context.functions[name](...args);
|
||||
} catch (error) {
|
||||
__wrnexusRestore();
|
||||
throw error;
|
||||
}
|
||||
if (__wrnexusPeerResult && typeof __wrnexusPeerResult.then === "function") {
|
||||
return Promise.resolve(__wrnexusPeerResult).finally(() => { ${syncStateFromContext} });
|
||||
if (result && typeof result.then === "function") {
|
||||
return Promise.resolve(result).finally(__wrnexusRestore);
|
||||
}
|
||||
${syncStateFromContext}
|
||||
return __wrnexusPeerResult;
|
||||
};`;
|
||||
})
|
||||
.join("\n");
|
||||
__wrnexusRestore();
|
||||
return result;
|
||||
};
|
||||
${functionAliases.map((name) => `const ${name} = (...__wrnexusPeerArgs) => __wrnexusPeer(${JSON.stringify(name)}, __wrnexusPeerArgs);`).join("\n")}`;
|
||||
const copyBack = stateNames
|
||||
.map(
|
||||
(name) =>
|
||||
|
||||
Reference in New Issue
Block a user