Skip to content

Commit

Permalink
refactor(daemon): Encapsulate controller provision within introduceNa…
Browse files Browse the repository at this point in the history
…mesToParty
  • Loading branch information
rekmarks committed Mar 7, 2024
1 parent 70d2069 commit 9623f98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,18 @@ export const makeHostMaker = ({
};

/**
* @param {import('./types.js').Controller} newController
* @param {Record<string,string>} introducedNames
* @param {string} formulaIdentifier - The guest or host formula identifier.
* @param {Record<string,string>} introducedNames - The names to introduce.
* @returns {Promise<void>}
*/
const introduceNamesToNewHostOrGuest = async (
newController,
const introduceNamesToParty = async (
formulaIdentifier,
introducedNames,
) => {
const { petStore: newPetStore } = await newController.internal;
/** @type {import('./types.js').Controller<any, any>} */
const controller =
provideControllerForFormulaIdentifier(formulaIdentifier);
const { petStore: newPetStore } = await controller.internal;
await Promise.all(
Object.entries(introducedNames).map(async ([parentName, childName]) => {
const introducedFormulaIdentifier =
Expand Down Expand Up @@ -158,15 +161,14 @@ export const makeHostMaker = ({
);
}

const newGuestController =
/** @type {import('./types.js').Controller<>} */ (
provideControllerForFormulaIdentifier(formulaIdentifier)
);
if (introducedNames !== undefined) {
// TODO: move to hook
// can use provideValueForFormulaIdentifier on the guest formula
introduceNamesToNewHostOrGuest(newGuestController, introducedNames);
introduceNamesToParty(formulaIdentifier, introducedNames);
}
const newGuestController =
/** @type {import('./types.js').Controller<any, any>} */ (
provideControllerForFormulaIdentifier(formulaIdentifier)
);
return {
formulaIdentifier,
value: /** @type {Promise<import('./types.js').EndoGuest>} */ (
Expand Down Expand Up @@ -466,13 +468,14 @@ export const makeHostMaker = ({
)}`,
);
}

if (introducedNames !== undefined) {
introduceNamesToParty(formulaIdentifier, introducedNames);
}
const newHostController =
/** @type {import('./types.js').Controller<>} */ (
provideControllerForFormulaIdentifier(formulaIdentifier)
);
if (introducedNames !== undefined) {
introduceNamesToNewHostOrGuest(newHostController, introducedNames);
}
return {
formulaIdentifier,
value: /** @type {Promise<import('./types.js').EndoHost>} */ (
Expand Down
4 changes: 2 additions & 2 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,14 @@ export interface EndoHost extends EndoDirectory {
addPeerInfo(peerInfo: PeerInfo): Promise<void>;
}

export interface InternalEndoHost {
export interface InternalEndoParty {
receive: Mail['receive'];
respond: Mail['respond'];
petStore: PetStore;
}

export interface EndoHostController
extends Controller<FarRef<EndoHost>, InternalEndoHost> {}
extends Controller<FarRef<EndoHost>, InternalEndoParty> {}

export type EndoInspector<Record = string> = {
lookup: (petName: Record) => Promise<unknown>;
Expand Down

0 comments on commit 9623f98

Please sign in to comment.