From 520b2ae81739b1a95dea9a72b2ede6ee9ca6ae45 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Fri, 8 Mar 2024 16:38:57 -0800 Subject: [PATCH] refactor(daemon): Extract caplet construction prelude to function --- packages/daemon/src/daemon.js | 24 +++++++++------ packages/daemon/src/host.js | 53 +++++++++++++++------------------- packages/daemon/src/types.d.ts | 14 +++------ 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/packages/daemon/src/daemon.js b/packages/daemon/src/daemon.js index 7f81997d77..7eabc55a70 100644 --- a/packages/daemon/src/daemon.js +++ b/packages/daemon/src/daemon.js @@ -1224,7 +1224,7 @@ const makeDaemonCore = async ( ) => { const { powersFormulaIdentifier, - unconfinedFormulaNumber: formulaNumber, + capletFormulaNumber, workerFormulaIdentifier, } = await formulaGraphMutex.enqueue(async () => { const ownFormulaNumber = await randomHex512(); @@ -1233,12 +1233,12 @@ const makeDaemonCore = async ( hostFormulaIdentifier, specifiedPowersFormulaIdentifier, ), - unconfinedFormulaIdentifier: serializeFormulaIdentifier({ + capletFormulaIdentifier: serializeFormulaIdentifier({ type: 'make-unconfined', number: ownFormulaNumber, node: ownNodeIdentifier, }), - unconfinedFormulaNumber: ownFormulaNumber, + capletFormulaNumber: ownFormulaNumber, workerFormulaIdentifier: await provideWorkerFormulaIdentifier( specifiedWorkerFormulaIdentifier, ), @@ -1254,8 +1254,10 @@ const makeDaemonCore = async ( powers: powersFormulaIdentifier, specifier, }; - return /** @type {import('./types.js').IncarnateResult} */ ( - provideValueForNumberedFormula(formula.type, formulaNumber, formula) + return provideValueForNumberedFormula( + formula.type, + capletFormulaNumber, + formula, ); }; @@ -1269,7 +1271,7 @@ const makeDaemonCore = async ( ) => { const { powersFormulaIdentifier, - bundleFormulaNumber: formulaNumber, + capletFormulaNumber, workerFormulaIdentifier, } = await formulaGraphMutex.enqueue(async () => { const ownFormulaNumber = await randomHex512(); @@ -1278,12 +1280,12 @@ const makeDaemonCore = async ( hostFormulaIdentifier, specifiedPowersFormulaIdentifier, ), - bundleFormulaIdentifier: serializeFormulaIdentifier({ + capletFormulaIdentifier: serializeFormulaIdentifier({ type: 'make-bundle', number: ownFormulaNumber, node: ownNodeIdentifier, }), - bundleFormulaNumber: ownFormulaNumber, + capletFormulaNumber: ownFormulaNumber, workerFormulaIdentifier: await provideWorkerFormulaIdentifier( specifiedWorkerFormulaIdentifier, ), @@ -1299,7 +1301,11 @@ const makeDaemonCore = async ( powers: powersFormulaIdentifier, bundle: bundleFormulaIdentifier, }; - return provideValueForNumberedFormula(formula.type, formulaNumber, formula); + return provideValueForNumberedFormula( + formula.type, + capletFormulaNumber, + formula, + ); }; /** @type {import('./types.js').DaemonCore['incarnateBundler']} */ diff --git a/packages/daemon/src/host.js b/packages/daemon/src/host.js index 053ad26a62..aebd65c5ad 100644 --- a/packages/daemon/src/host.js +++ b/packages/daemon/src/host.js @@ -345,16 +345,16 @@ export const makeHostMaker = ({ return value; }; - /** @type {import('./types.js').EndoHost['makeUnconfined']} */ - const makeUnconfined = async ( - workerName, - specifier, - powersName, - resultName, - ) => { + /** + * Helper function for makeUnconfined and makeBundle. + * @param {string} powersName + * @param {string} workerName + * @param {string} [resultName] + */ + const prepareMakeCaplet = (powersName, workerName, resultName) => { assertPowersName(powersName); - /** @type {import('./types.js').DeferredTasks} */ + /** @type {import('./types.js').DeferredTasks} */ const tasks = makeDeferredTasks(); const workerFormulaIdentifier = prepareWorkerFormulaIdentifier( @@ -369,10 +369,23 @@ export const makeHostMaker = ({ if (resultName !== undefined) { tasks.push(identifiers => - petStore.write(resultName, identifiers.unconfinedFormulaIdentifier), + petStore.write(resultName, identifiers.capletFormulaIdentifier), ); } + return { tasks, workerFormulaIdentifier, powersFormulaIdentifier }; + }; + + /** @type {import('./types.js').EndoHost['makeUnconfined']} */ + const makeUnconfined = async ( + workerName, + specifier, + powersName, + resultName, + ) => { + const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } = + prepareMakeCaplet(powersName, workerName, resultName); + // Behold, recursion: // eslint-disable-next-line no-use-before-define const { value } = await incarnateUnconfined( @@ -397,31 +410,13 @@ export const makeHostMaker = ({ powersName, resultName, ) => { - assertPowersName(powersName); - const bundleFormulaIdentifier = petStore.identifyLocal(bundleName); if (bundleFormulaIdentifier === undefined) { throw new TypeError(`Unknown pet name for bundle: ${bundleName}`); } - /** @type {import('./types.js').DeferredTasks} */ - const tasks = makeDeferredTasks(); - - const workerFormulaIdentifier = prepareWorkerFormulaIdentifier( - workerName, - tasks.push, - ); - - const powersFormulaIdentifier = preparePowersFormulaIdentifier( - powersName, - tasks.push, - ); - - if (resultName !== undefined) { - tasks.push(identifiers => - petStore.write(resultName, identifiers.bundleFormulaIdentifier), - ); - } + const { tasks, workerFormulaIdentifier, powersFormulaIdentifier } = + prepareMakeCaplet(powersName, workerName, resultName); // Behold, recursion: // eslint-disable-next-line no-use-before-define diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index c2d4fdf26c..99b238b3f0 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -143,12 +143,6 @@ type MakeUnconfinedFormula = { // TODO formula slots }; -export type MakeUnconfinedDeferredTaskParams = { - powersFormulaIdentifier: string; - unconfinedFormulaIdentifier: string; - workerFormulaIdentifier: string; -}; - type MakeBundleFormula = { type: 'make-bundle'; worker: string; @@ -157,9 +151,9 @@ type MakeBundleFormula = { // TODO formula slots }; -export type MakeBundleDeferredTaskParams = { +export type MakeCapletDeferredTaskParams = { + capletFormulaIdentifier: string; powersFormulaIdentifier: string; - bundleFormulaIdentifier: string; workerFormulaIdentifier: string; }; @@ -785,14 +779,14 @@ export interface DaemonCore { incarnateUnconfined: ( hostFormulaIdentifier: string, specifier: string, - deferredTasks: DeferredTasks, + deferredTasks: DeferredTasks, specifiedWorkerFormulaIdentifier?: string, specifiedPowersFormulaIdentifier?: string, ) => IncarnateResult; incarnateBundle: ( hostFormulaIdentifier: string, bundleFormulaIdentifier: string, - deferredTasks: DeferredTasks, + deferredTasks: DeferredTasks, specifiedWorkerFormulaIdentifier?: string, specifiedPowersFormulaIdentifier?: string, ) => IncarnateResult;