|
1 | 1 | import { AsyncLocalStorage } from 'node:async_hooks'; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * What one daemon runtime owns of a kind: a module (or a package) keeps its state in slots of an |
5 | | - * async-local store the runtime enters, so two runtimes in one process never share it. Outside a |
6 | | - * runtime (a client process, a unit test) the store's own standalone state answers. |
| 4 | + * What one daemon runtime owns: a handle over the process's one slot store. The runtime enters |
| 5 | + * it once (through {@link RuntimeStateHandle.run}) and disposes it once at close, so two runtimes |
| 6 | + * in one process never share per-runtime state. Outside a runtime (a client process, a unit |
| 7 | + * test) the store's own standalone slots answer. |
7 | 8 | */ |
8 | 9 | export type RuntimeStateHandle = Readonly<{ |
9 | 10 | /** Runs `fn` as the runtime that owns this state. */ |
10 | 11 | run<T>(fn: () => T): T; |
11 | | - /** Binds this state for the rest of the current async context, after the runtime is composed. */ |
12 | | - enter(): void; |
13 | 12 | /** Releases what the state holds: each slot's disposer runs, then the slots are forgotten. */ |
14 | 13 | dispose(): void; |
15 | 14 | }>; |
16 | 15 |
|
17 | | -export type RuntimeSlots = Readonly<{ |
18 | | - /** One piece of state the current runtime owns, created on first use. */ |
19 | | - slot<T>(key: symbol, create: () => T, dispose?: (value: T) => void): T; |
20 | | - /** The state of one new runtime: enter it before anything reads a slot. */ |
21 | | - createHandle(): RuntimeStateHandle; |
22 | | -}>; |
23 | | - |
24 | 16 | type Slot = { value: unknown; dispose: ((value: never) => void) | undefined }; |
25 | 17 |
|
26 | | -/** The single owner of the "entered store, else standalone store" pattern; callers declare slots. */ |
27 | | -export function defineRuntimeSlots(): RuntimeSlots { |
28 | | - const storage = new AsyncLocalStorage<Map<symbol, Slot>>(); |
29 | | - const standalone = new Map<symbol, Slot>(); |
| 18 | +// One store per process: modules declare only slot keys, and each runtime enters exactly one |
| 19 | +// handle of this store. |
| 20 | +const storage = new AsyncLocalStorage<Map<symbol, Slot>>(); |
| 21 | +const standalone = new Map<symbol, Slot>(); |
| 22 | + |
| 23 | +/** One piece of state the current runtime owns, created on first use. */ |
| 24 | +export function runtimeSlot<T>(key: symbol, create: () => T, dispose?: (value: T) => void): T { |
| 25 | + const slots = storage.getStore() ?? standalone; |
| 26 | + if (!slots.has(key)) slots.set(key, { value: create(), dispose }); |
| 27 | + return slots.get(key)!.value as never; |
| 28 | +} |
| 29 | + |
| 30 | +/** The state of one new runtime: enter it before anything reads a slot. */ |
| 31 | +export function createRuntimeStateHandle(): RuntimeStateHandle { |
| 32 | + const owned = new Map<symbol, Slot>(); |
30 | 33 | return { |
31 | | - slot: (key, create, dispose) => { |
32 | | - const slots = storage.getStore() ?? standalone; |
33 | | - if (!slots.has(key)) slots.set(key, { value: create(), dispose }); |
34 | | - return slots.get(key)!.value as never; |
35 | | - }, |
36 | | - createHandle: () => { |
37 | | - const owned = new Map<symbol, Slot>(); |
38 | | - return { |
39 | | - run: (fn) => storage.run(owned, fn), |
40 | | - enter: () => storage.enterWith(owned), |
41 | | - dispose: () => { |
42 | | - for (const { value, dispose } of owned.values()) dispose?.(value as never); |
43 | | - owned.clear(); |
44 | | - }, |
45 | | - }; |
| 34 | + run: (fn) => storage.run(owned, fn), |
| 35 | + dispose: () => { |
| 36 | + for (const { value, dispose } of owned.values()) dispose?.(value as never); |
| 37 | + owned.clear(); |
46 | 38 | }, |
47 | 39 | }; |
48 | 40 | } |
0 commit comments