Skip to content

Commit

Permalink
fix(daemon): enhance types for pet-store #2071
Browse files Browse the repository at this point in the history
fix(daemon): enhance types for pet-store
  • Loading branch information
kumavis authored Feb 16, 2024
2 parents 3656123 + 5be3da2 commit ed0788b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
35 changes: 12 additions & 23 deletions packages/daemon/src/pet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,19 @@ export const makePetStoreMaker = (filePowers, locator) => {
}),
);

/** @param {string} petName */
/** @type {import('./types.js').PetStore['has']} */
const has = petName => {
assertValidName(petName);
return petNames.has(petName);
};

/**
* @param {string} petName
* @returns {string | undefined}
*/
/** @type {import('./types.js').PetStore['identifyLocal']} */
const identifyLocal = petName => {
assertValidName(petName);
return petNames.get(petName);
};

/**
* @param {string} petName
* @param {string} formulaIdentifier
*/
/** @type {import('./types.js').PetStore['write']} */
const write = async (petName, formulaIdentifier) => {
assertValidName(petName);
if (!validFormulaPattern.test(formulaIdentifier)) {
Expand Down Expand Up @@ -127,38 +121,39 @@ export const makePetStoreMaker = (filePowers, locator) => {
};

// Returns in an Array format.
/** @type {import('./types.js').PetStore['list']} */
const list = () => harden([...petNames.keys()].sort());
// Returns in an object operations format ({ add, value } or { remove }).
/** @type {import('./types.js').PetStore['follow']} */
const follow = async () =>
makeIteratorRef(
(async function* currentAndSubsequentNames() {
const changes = changesTopic.subscribe();
for (const name of [...petNames.keys()].sort()) {
const formulaIdentifierRecord =
formulaIdentifierRecordForName(name);
yield /** type {{ add:string, value: import('./types.js').FormulaIdentifierRecord }} */ {
yield /** @type {{ add: string, value: import('./types.js').FormulaIdentifierRecord }} */ ({
add: name,
value: formulaIdentifierRecord,
};
});
}
yield* changes;
})(),
);

// Returns in Object.fromEntries format.
/** @returns {Array<[string, import('./types.js').FormulaIdentifierRecord]>} */
/** @type {import('./types.js').PetStore['listEntries']} */
const listEntries = () =>
harden(
[...petNames.keys()].sort().map(name => {
return [name, formulaIdentifierRecordForName(name)];
}),
);
// Provided as an alias for follow, with naming symmetry to listEntries.
/** @type {import('./types.js').PetStore['follow']} */
const followEntries = follow;

/**
* @param {string} petName
*/
/** @type {import('./types.js').PetStore['remove']} */
const remove = async petName => {
assertValidName(petName);
const formulaIdentifier = petNames.get(petName);
Expand All @@ -183,10 +178,7 @@ export const makePetStoreMaker = (filePowers, locator) => {
// TODO consider tracking historical pet names for formulas
};

/**
* @param {string} fromName
* @param {string} toName
*/
/** @type {import('./types.js').PetStore['rename']} */
const rename = async (fromName, toName) => {
assertValidName(fromName);
assertValidName(toName);
Expand Down Expand Up @@ -244,9 +236,7 @@ export const makePetStoreMaker = (filePowers, locator) => {
// TODO consider retaining a backlog of overwritten names for recovery
};

/**
* @param {string} formulaIdentifier
*/
/** @type {import('./types.js').PetStore['reverseLookup']} */
const reverseLookup = formulaIdentifier => {
if (!validFormulaPattern.test(formulaIdentifier)) {
throw new Error(`Invalid formula identifier ${q(formulaIdentifier)}`);
Expand All @@ -258,7 +248,6 @@ export const makePetStoreMaker = (filePowers, locator) => {
return harden([...formulaPetNames]);
};

/** @type {import('./types.js').PetStore} */
const petStore = {
has,
identifyLocal,
Expand Down
8 changes: 7 additions & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ export interface PetStore {
has(petName: string): boolean;
identifyLocal(petName: string): string | undefined;
list(): Array<string>;
follow(): Promise<FarRef<Reader<{ add: string } | { remove: string }>>>;
follow(): Promise<
FarRef<
Reader<
{ add: string; value: FormulaIdentifierRecord } | { remove: string }
>
>
>;
listEntries(): Array<[string, FormulaIdentifierRecord]>;
followEntries(): Promise<
FarRef<
Expand Down

0 comments on commit ed0788b

Please sign in to comment.