@@ -4,46 +4,45 @@ import { Result } from "./Result.js";
44import { createId , Id } from "./Type.js" ;
55
66/**
7- * A registry for one-time callback functions .
7+ * Request-response correlation for callbacks across boundaries .
88 *
99 * Stores callbacks with unique IDs and executes them once with an optional
10- * argument. Executed callbacks are automatically removed from the registry .
10+ * argument. Executed callbacks are automatically removed.
1111 *
12- * This is useful for correlating asynchronous operations across boundaries
13- * where callback functions cannot be passed directly (e.g., web workers).
12+ * This is useful for correlating asynchronous request-response operations
13+ * across boundaries where callback functions cannot be passed directly (e.g.,
14+ * web workers, message queues).
1415 *
1516 * The `execute` method intentionally does not use try-catch or {@link Result}
16- * because it's the callback's responsibility to handle its own errors. The
17- * registry is just a correlation mechanism and should not interfere with error
18- * handling or debugging by masking the original error location.
17+ * because it's the callback's responsibility to handle its own errors.
1918 *
2019 * ### Example
2120 *
2221 * ```ts
2322 * // No-argument callbacks
24- * const registry = createCallbackRegistry (deps);
25- * const id = registry .register(() => console.log("called"));
26- * registry .execute(id);
23+ * const callbacks = createCallbacks (deps);
24+ * const id = callbacks .register(() => console.log("called"));
25+ * callbacks .execute(id);
2726 *
2827 * // With argument callbacks
29- * const stringRegistry = createCallbackRegistry <string>(deps);
30- * const id = stringRegistry .register((value) => {
28+ * const stringCallbacks = createCallbacks <string>(deps);
29+ * const id = stringCallbacks .register((value) => {
3130 * console.log(value);
3231 * });
33- * stringRegistry .execute(id, "hello");
32+ * stringCallbacks .execute(id, "hello");
3433 *
3534 * // Promise.withResolvers pattern
36- * const promiseRegistry = createCallbackRegistry <string>(deps);
35+ * const promiseCallbacks = createCallbacks <string>(deps);
3736 * const { promise, resolve } = Promise.withResolvers<string>();
38- * const id = promiseRegistry .register(resolve);
39- * promiseRegistry .execute(id, "resolved value");
37+ * const id = promiseCallbacks .register(resolve);
38+ * promiseCallbacks .execute(id, "resolved value");
4039 * await promise; // "resolved value"
4140 * ```
4241 *
4342 * @template T - The type of argument passed to callbacks (defaults to undefined
4443 * for no-argument callbacks)
4544 */
46- export interface CallbackRegistry < T = undefined > {
45+ export interface Callbacks < T = undefined > {
4746 /** Registers a callback function and returns a unique ID. */
4847 readonly register : ( callback : ( arg : T ) => void ) => CallbackId ;
4948
@@ -53,12 +52,13 @@ export interface CallbackRegistry<T = undefined> {
5352 : ( id : CallbackId , arg : T ) => undefined ;
5453}
5554
55+ /** Unique identifier for a callback in {@link Callbacks}. */
5656export type CallbackId = Id & Brand < "Callback" > ;
5757
58- /** Creates a new {@link CallbackRegistry} for one-time callback functions . */
59- export const createCallbackRegistry = < T = undefined > (
58+ /** Creates a new {@link Callbacks} . */
59+ export const createCallbacks = < T = undefined > (
6060 deps : RandomBytesDep ,
61- ) : CallbackRegistry < T > => {
61+ ) : Callbacks < T > => {
6262 const callbackMap = new Map < CallbackId , ( arg : T ) => void > ( ) ;
6363
6464 return {
@@ -70,15 +70,14 @@ export const createCallbackRegistry = <T = undefined>(
7070
7171 execute : ( id : CallbackId , ...args : T extends undefined ? [ ] : [ T ] ) => {
7272 const callback = callbackMap . get ( id ) ;
73- if ( callback ) {
74- callbackMap . delete ( id ) ;
75- if ( args . length === 0 ) {
76- // Called without argument (undefined case)
77- ( callback as ( ) => void ) ( ) ;
78- } else {
79- callback ( args [ 0 ] ) ;
80- }
73+ if ( ! callback ) return ;
74+ callbackMap . delete ( id ) ;
75+ if ( args . length === 0 ) {
76+ // Called without argument (undefined case)
77+ ( callback as ( ) => void ) ( ) ;
78+ } else {
79+ callback ( args [ 0 ] ) ;
8180 }
8281 } ,
83- } as CallbackRegistry < T > ;
82+ } as Callbacks < T > ;
8483} ;
0 commit comments