S2 take once in a bundle too, Go and TypeScript

A bundle holder takes an ask once through the runtime's create with the
ask's bound as the key's time to live: stdio.KeptState.TakeOnce and
TypeScript takeOnce; stdio.TakenState and TakenKey are the one name, which
process refers to.

Builds: design 47 H2 §775, §820; H-A-359
This commit is contained in:
2026-11-21 23:12:58 +01:00
parent 423de0e8a2
commit 25b3030a93
5 changed files with 125 additions and 14 deletions
+35
View File
@@ -133,6 +133,41 @@ function withinOneCall(key: string, value: unknown): void {
}
}
/** The state a holder of a kept call keeps the ids of the asks it took in (design 47 H2 §775, §820): Go's `stdio.TakenState`. */
export const TAKEN_STATE = "taken";
/** The key an ask taken from `seat` is kept under in TAKEN_STATE: `<seat>.<ask>`. */
export function takenKey(seat: string, ask: string): string {
return `${seat}.${ask}`;
}
/** The code of the runtime's refusal of a create whose key has a value (Go's `stdio.CodeChanged`). */
const CODE_CHANGED = -32010;
/**
* Whether this holder takes the ask `ask` of `seat` now (design 47 H2 §775; H-A-359), as Go's `KeptState.TakeOnce`: the
* first time it is given it is created under takenKey with the ask's bound as its time to live (the runtime's
* `mesh/state.create`) and this resolves true; given again within the bound, false, and the holder acts once.
* `stateName` is the holder's TAKEN_STATE, declared `key-ttl`.
*/
export async function takeOnce(seat: string, ask: string, boundMs: number, stateName = TAKEN_STATE): Promise<boolean> {
if (!ask || /[.*> ]/.test(ask)) throw new Error(`taking an ask of ${seat}: ${JSON.stringify(ask)} is not an ask id: an ask id holds no dot and no wildcard, naming one key and no other`);
if (!seat || /[.*> ]/.test(seat)) throw new Error(`taking the ask ${ask}: ${JSON.stringify(seat)} is not a seat's name`);
refusedUnannounced(stateName);
try {
await boundChannel().ask("mesh/state.create", {
state: stateName,
key: takenKey(seat, ask),
value: { seat, ask },
ttl_seconds: Math.max(1, Math.floor(boundMs / 1000)),
});
return true;
} catch (e) {
if ((e as { code?: number }).code === CODE_CHANGED) return false;
throw e;
}
}
/** A state this module keeps (by its local name) or reads (`<module>.<name>`). */
export function state<T = unknown>(name: string): State<T> {
const bound = (): StateChannel => {