Skip to content

Commit

Permalink
refactor(daemon): Standardize on 'name change' instead of 'diff'
Browse files Browse the repository at this point in the history
Replaces all references to "diff" in pubsub type names to "name change"
to match the names of the pubsub APIs (e.g. `followNameChanges`). Also
removes an extraneous `if` condition.
  • Loading branch information
rekmarks committed Apr 17, 2024
1 parent 0415ffe commit 37bb657
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions packages/daemon/src/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
16 changes: 8 additions & 8 deletions packages/daemon/src/pet-sitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/pet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down
18 changes: 9 additions & 9 deletions packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PetStoreNameDiff>;
export type NameChangesTopic = Topic<PetStoreNameChange>;

export type IdChangesTopic = Topic<PetStoreIdDiff>;
export type IdChangesTopic = Topic<PetStoreIdNameChange>;

export interface PetStore {
has(petName: string): boolean;
Expand All @@ -380,15 +380,15 @@ 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<PetStoreNameDiff, undefined, undefined>;
followNameChanges(): AsyncGenerator<PetStoreNameChange, undefined, undefined>;
/**
* 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.
* @throws If attempting to follow an id with no names.
*/
followIdNameChanges(
id: string,
): AsyncGenerator<PetStoreIdDiff, undefined, undefined>;
): AsyncGenerator<PetStoreIdNameChange, undefined, undefined>;
write(petName: string, id: string): Promise<void>;
remove(petName: string): Promise<void>;
rename(fromPetName: string, toPetName: string): Promise<void>;
Expand All @@ -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[] };

Expand All @@ -413,12 +413,12 @@ export interface NameHub {
reverseLocate(locator: string): Promise<string[]>;
followLocatorNameChanges(
locator: string,
): AsyncGenerator<LocatorDiff, undefined, undefined>;
): AsyncGenerator<LocatorNameChange, undefined, undefined>;
list(...petNamePath: string[]): Promise<Array<string>>;
listIdentifiers(...petNamePath: string[]): Promise<Array<string>>;
followNameChanges(
...petNamePath: string[]
): AsyncGenerator<PetStoreNameDiff, undefined, undefined>;
): AsyncGenerator<PetStoreNameChange, undefined, undefined>;
lookup(...petNamePath: string[]): Promise<unknown>;
reverseLookup(value: unknown): Array<string>;
write(petNamePath: string[], id: string): Promise<void>;
Expand Down

0 comments on commit 37bb657

Please sign in to comment.