diff --git a/packages/daemon/src/directory.js b/packages/daemon/src/directory.js index e7983403c7..584eae059e 100644 --- a/packages/daemon/src/directory.js +++ b/packages/daemon/src/directory.js @@ -108,16 +108,16 @@ export const makeDirectoryMaker = ({ locator, ) { const id = idFromLocator(locator); - for await (const idDiff of petStore.followIdNameChanges(id)) { + for await (const idNameChange of petStore.followIdNameChanges(id)) { /** @type {any} */ - const locatorDiff = { - ...idDiff, - ...(Object.hasOwn(idDiff, 'add') + const locatorNameChange = { + ...idNameChange, + ...(Object.hasOwn(idNameChange, 'add') ? { add: locator } : { remove: locator }), }; - yield /** @type {import('./types.js').LocatorDiff} */ locatorDiff; + yield /** @type {import('./types.js').LocatorNameChange} */ locatorNameChange; } }; diff --git a/packages/daemon/src/pet-sitter.js b/packages/daemon/src/pet-sitter.js index c260e564dd..d90abae29c 100644 --- a/packages/daemon/src/pet-sitter.js +++ b/packages/daemon/src/pet-sitter.js @@ -63,17 +63,17 @@ export const makePetSitter = (petStore, specialNames) => { const followIdNameChanges = async function* currentAndSubsequentIds(id) { const subscription = petStore.followIdNameChanges(id); - const [idSpecialName] = Object.entries(specialNames) + const idSpecialNames = Object.entries(specialNames) .filter(([_, specialId]) => specialId === id) .map(([specialName, _]) => specialName); - if (typeof idSpecialName === 'string') { - // The first published event contains the existing names for the id, if any. - const { value: existingNames } = await subscription.next(); - existingNames?.names?.unshift(idSpecialName); - existingNames?.names?.sort(); - yield /** @type {import('./types.js').PetStoreIdDiff} */ (existingNames); - } + // The first published event contains the existing names for the id, if any. + const { value: existingNames } = await subscription.next(); + existingNames?.names?.unshift(...idSpecialNames); + existingNames?.names?.sort(); + yield /** @type {import('./types.js').PetStoreIdNameChange} */ ( + existingNames + ); yield* subscription; }; diff --git a/packages/daemon/src/pet-store.js b/packages/daemon/src/pet-store.js index ad5645715e..a8048bd250 100644 --- a/packages/daemon/src/pet-store.js +++ b/packages/daemon/src/pet-store.js @@ -31,7 +31,7 @@ export const makePetStoreMaker = (filePowers, config) => { * Publishes an id change to its subscribers, if any. * * @param {string} id - The id to publish a change for. - * @param {import('./types.js').PetStoreIdDiff} payload - The payload to publish. + * @param {import('./types.js').PetStoreIdNameChange} payload - The payload to publish. */ const publishIdChangeToSubscribers = (id, payload) => { const idTopic = idsToTopics.get(id); @@ -133,7 +133,7 @@ export const makePetStoreMaker = (filePowers, config) => { /** @type {string} */ (idsToPetNames.getKey(name)), ); - yield /** @type {import('./types.js').PetStoreNameDiff} */ ({ + yield /** @type {import('./types.js').PetStoreNameChange} */ ({ add: name, value: idRecord, }); @@ -152,7 +152,7 @@ export const makePetStoreMaker = (filePowers, config) => { const subscription = idTopic.subscribe(); const existingNames = idsToPetNames.getAllFor(id).sort(); - yield /** @type {import('./types.js').PetStoreIdDiff} */ ({ + yield /** @type {import('./types.js').PetStoreIdNameChange} */ ({ add: parseId(id), names: existingNames, }); diff --git a/packages/daemon/src/types.d.ts b/packages/daemon/src/types.d.ts index 3867477d1b..8d8d0b021e 100644 --- a/packages/daemon/src/types.d.ts +++ b/packages/daemon/src/types.d.ts @@ -360,17 +360,17 @@ export interface Handle { export type MakeSha512 = () => Sha512; -export type PetStoreNameDiff = +export type PetStoreNameChange = | { add: string; value: IdRecord } | { remove: string }; -export type PetStoreIdDiff = +export type PetStoreIdNameChange = | { add: IdRecord; names: string[] } | { remove: IdRecord; names?: string[] }; -export type NameChangesTopic = Topic; +export type NameChangesTopic = Topic; -export type IdChangesTopic = Topic; +export type IdChangesTopic = Topic; export interface PetStore { has(petName: string): boolean; @@ -380,7 +380,7 @@ export interface PetStore { * Subscribe to all name changes. First publishes all existing names in alphabetical order. * Then publishes diffs as names are added and removed. */ - followNameChanges(): AsyncGenerator; + followNameChanges(): AsyncGenerator; /** * Subscribe to name changes for the specified id. First publishes the existing names for the id. * Then publishes diffs as names are added and removed, or if the id is itself removed. @@ -388,7 +388,7 @@ export interface PetStore { */ followIdNameChanges( id: string, - ): AsyncGenerator; + ): AsyncGenerator; write(petName: string, id: string): Promise; remove(petName: string): Promise; rename(fromPetName: string, toPetName: string): Promise; @@ -402,7 +402,7 @@ export interface PetStore { /** * `add` and `remove` are locators. */ -export type LocatorDiff = +export type LocatorNameChange = | { add: string; names: string[] } | { remove: string; names?: string[] }; @@ -413,12 +413,12 @@ export interface NameHub { reverseLocate(locator: string): Promise; followLocatorNameChanges( locator: string, - ): AsyncGenerator; + ): AsyncGenerator; list(...petNamePath: string[]): Promise>; listIdentifiers(...petNamePath: string[]): Promise>; followNameChanges( ...petNamePath: string[] - ): AsyncGenerator; + ): AsyncGenerator; lookup(...petNamePath: string[]): Promise; reverseLookup(value: unknown): Array; write(petNamePath: string[], id: string): Promise;