Skip to content

Commit

Permalink
refactor(daemon): Extract caplet construction prelude to function
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Mar 9, 2024
1 parent 86ee463 commit 520b2ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
24 changes: 15 additions & 9 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ const makeDaemonCore = async (
) => {
const {
powersFormulaIdentifier,
unconfinedFormulaNumber: formulaNumber,
capletFormulaNumber,
workerFormulaIdentifier,
} = await formulaGraphMutex.enqueue(async () => {
const ownFormulaNumber = await randomHex512();
Expand All @@ -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,
),
Expand All @@ -1254,8 +1254,10 @@ const makeDaemonCore = async (
powers: powersFormulaIdentifier,
specifier,
};
return /** @type {import('./types.js').IncarnateResult<unknown>} */ (
provideValueForNumberedFormula(formula.type, formulaNumber, formula)
return provideValueForNumberedFormula(
formula.type,
capletFormulaNumber,
formula,
);
};

Expand All @@ -1269,7 +1271,7 @@ const makeDaemonCore = async (
) => {
const {
powersFormulaIdentifier,
bundleFormulaNumber: formulaNumber,
capletFormulaNumber,
workerFormulaIdentifier,
} = await formulaGraphMutex.enqueue(async () => {
const ownFormulaNumber = await randomHex512();
Expand All @@ -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,
),
Expand All @@ -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']} */
Expand Down
53 changes: 24 additions & 29 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('./types.js').MakeUnconfinedDeferredTaskParams>} */
/** @type {import('./types.js').DeferredTasks<import('./types.js').MakeCapletDeferredTaskParams>} */
const tasks = makeDeferredTasks();

const workerFormulaIdentifier = prepareWorkerFormulaIdentifier(
Expand All @@ -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(
Expand All @@ -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<import('./types.js').MakeBundleDeferredTaskParams>} */
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
Expand Down
14 changes: 4 additions & 10 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -157,9 +151,9 @@ type MakeBundleFormula = {
// TODO formula slots
};

export type MakeBundleDeferredTaskParams = {
export type MakeCapletDeferredTaskParams = {
capletFormulaIdentifier: string;
powersFormulaIdentifier: string;
bundleFormulaIdentifier: string;
workerFormulaIdentifier: string;
};

Expand Down Expand Up @@ -785,14 +779,14 @@ export interface DaemonCore {
incarnateUnconfined: (
hostFormulaIdentifier: string,
specifier: string,
deferredTasks: DeferredTasks<MakeUnconfinedDeferredTaskParams>,
deferredTasks: DeferredTasks<MakeCapletDeferredTaskParams>,
specifiedWorkerFormulaIdentifier?: string,
specifiedPowersFormulaIdentifier?: string,
) => IncarnateResult<unknown>;
incarnateBundle: (
hostFormulaIdentifier: string,
bundleFormulaIdentifier: string,
deferredTasks: DeferredTasks<MakeBundleDeferredTaskParams>,
deferredTasks: DeferredTasks<MakeCapletDeferredTaskParams>,
specifiedWorkerFormulaIdentifier?: string,
specifiedPowersFormulaIdentifier?: string,
) => IncarnateResult<unknown>;
Expand Down

0 comments on commit 520b2ae

Please sign in to comment.