Skip to content

Commit

Permalink
refactor(daemon): Coordinate host provideX() implementations
Browse files Browse the repository at this point in the history
coordinate, verb, "to combine in harmonious relation or action."

Refactors the host's `provideX()` methods such that they all:
1. Attempt to get the existing formula id for the provided name, if any.
2. If there is no existing formula id, incarnate and return a new value.

All type checks during step 1 have been removed. In other words, if you
attempt to provide something under an existing name that resolves to a
different value type, you're on your own. We may revisit this decision
in the future.
  • Loading branch information
rekmarks committed Mar 16, 2024
1 parent 77e08e5 commit 7483103
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
1 change: 0 additions & 1 deletion packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,6 @@ const makeDaemonCore = async (

await deferredTasks.execute({
workerFormulaIdentifier: formatId({
type: 'worker',
number: formulaNumber,
node: ownNodeIdentifier,
}),
Expand Down
64 changes: 27 additions & 37 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ export const makeHostMaker = ({
/** @type {import('./types.js').DeferredTasks<import('./types.js').WorkerDeferredTaskParams>} */
const tasks = makeDeferredTasks();
// eslint-disable-next-line no-use-before-define
const workerFormulaIdentifier = prepareWorkerFormulaIdentifier(
workerName,
tasks.push,
);
const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName);
// eslint-disable-next-line no-use-before-define
prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push);

if (workerFormulaIdentifier !== undefined) {
return /** @type {Promise<import('./types.js').EndoWorker>} */ (
Expand All @@ -146,39 +145,32 @@ export const makeHostMaker = ({

/**
* @param {string} workerName
* @param {import('./types.js').DeferredTasks<{ workerFormulaIdentifier: string }>['push']} deferTask
* @returns {string | undefined}
*/
const prepareWorkerFormulaIdentifier = (workerName, deferTask) => {
const tryGetWorkerFormulaIdentifier = workerName => {
if (workerName === 'MAIN') {
return mainWorkerFormulaIdentifier;
} else if (workerName === 'NEW') {
return undefined;
}

assertPetName(workerName);
const workerFormulaIdentifier = petStore.identifyLocal(workerName);
if (workerFormulaIdentifier === undefined) {
deferTask(identifiers =>
petStore.write(workerName, identifiers.workerFormulaIdentifier),
);
}
return workerFormulaIdentifier;
return petStore.identifyLocal(workerName);
};

/**
* @param {string | 'NONE' | 'SELF' | 'ENDO'} agentName
* @param {import('./types.js').DeferredTasks<{ powersFormulaIdentifier: string }>['push']} deferTask
* @returns {string | undefined}
* @param {string} workerName
* @param {string | undefined} workerFormulaIdentifier
* @param {import('./types.js').DeferredTasks<{ workerFormulaIdentifier: string }>['push']} deferTask
*/
const preparePowersFormulaIdentifier = (agentName, deferTask) => {
const powersFormulaIdentifier = petStore.identifyLocal(agentName);
if (powersFormulaIdentifier === undefined) {
const prepareWorkerIncarnation = (
workerName,
workerFormulaIdentifier,
deferTask,
) => {
if (workerFormulaIdentifier === undefined) {
deferTask(identifiers =>
petStore.write(agentName, identifiers.powersFormulaIdentifier),
petStore.write(workerName, identifiers.workerFormulaIdentifier),
);
}
return powersFormulaIdentifier;
};

/**
Expand All @@ -205,10 +197,8 @@ export const makeHostMaker = ({
/** @type {import('./types.js').DeferredTasks<import('./types.js').EvalDeferredTaskParams>} */
const tasks = makeDeferredTasks();

const workerFormulaIdentifier = prepareWorkerFormulaIdentifier(
workerName,
tasks.push,
);
const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName);
prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push);

/** @type {(string | string[])[]} */
const endowmentFormulaIdsOrPaths = petNamePaths.map(
Expand Down Expand Up @@ -259,15 +249,15 @@ export const makeHostMaker = ({
/** @type {import('./types.js').DeferredTasks<import('./types.js').MakeCapletDeferredTaskParams>} */
const tasks = makeDeferredTasks();

const workerFormulaIdentifier = prepareWorkerFormulaIdentifier(
workerName,
tasks.push,
);
const workerFormulaIdentifier = tryGetWorkerFormulaIdentifier(workerName);
prepareWorkerIncarnation(workerName, workerFormulaIdentifier, tasks.push);

const powersFormulaIdentifier = preparePowersFormulaIdentifier(
powersName,
tasks.push,
);
const powersFormulaIdentifier = petStore.identifyLocal(powersName);
if (powersFormulaIdentifier === undefined) {
tasks.push(identifiers =>
petStore.write(powersName, identifiers.powersFormulaIdentifier),
);
}

if (resultName !== undefined) {
tasks.push(identifiers =>
Expand Down Expand Up @@ -398,7 +388,7 @@ export const makeHostMaker = ({
* @returns {Promise<{formulaIdentifier: string, value: Promise<import('./types.js').EndoHost>}>}
*/
const makeHost = async (petName, { introducedNames = {} } = {}) => {
let host = await getNamedAgent(petName);
let host = getNamedAgent(petName);
if (host === undefined) {
const { value, formulaIdentifier } =
// Behold, recursion:
Expand Down Expand Up @@ -428,7 +418,7 @@ export const makeHostMaker = ({
* @returns {Promise<{formulaIdentifier: string, value: Promise<import('./types.js').EndoGuest>}>}
*/
const makeGuest = async (petName, { introducedNames = {} } = {}) => {
let guest = await getNamedAgent(petName);
let guest = getNamedAgent(petName);
if (guest === undefined) {
const { value, formulaIdentifier } =
// Behold, recursion:
Expand Down

0 comments on commit 7483103

Please sign in to comment.