perf(compiler): deduplicate client peer state synchronization
This commit is contained in:
@@ -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) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user