Skip to content

Commit 05fe5d5

Browse files
committed
Rename CallbackRegistry and RefCountedResourceManager
1 parent 44635f6 commit 05fe5d5

13 files changed

Lines changed: 243 additions & 243 deletions

File tree

.changeset/warm-squids-look.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@evolu/common": patch
3+
---
4+
5+
Renaming
6+
7+
- `CallbackRegistry``Callbacks`
8+
- `createCallbackRegistry``createCallbacks`
9+
- `RefCountedResourceManager``Resources`
10+
- `createRefCountedResourceManager``createResources`
11+
- `ResourceManagerConfig``ResourcesConfig`
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,45 @@ import { Result } from "./Result.js";
44
import { 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}. */
5656
export 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
};

packages/common/src/Evolu/Db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isNonEmptyArray, NonEmptyReadonlyArray } from "../Array.js";
2-
import { CallbackId } from "../CallbackRegistry.js";
2+
import { CallbackId } from "../Callbacks.js";
33
import { ConsoleConfig, ConsoleDep } from "../Console.js";
44
import {
55
createSymmetricCrypto,

packages/common/src/Evolu/Evolu.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pack } from "msgpackr";
22
import { isNonEmptyArray, isNonEmptyReadonlyArray } from "../Array.js";
33
import { assert, assertNonEmptyArray } from "../Assert.js";
4-
import { createCallbackRegistry } from "../CallbackRegistry.js";
4+
import { createCallbacks } from "../Callbacks.js";
55
import { ConsoleDep } from "../Console.js";
66
import { RandomBytesDep, SymmetricCryptoDecryptError } from "../Crypto.js";
77
import { eqArrayNumber } from "../Eq.js";
@@ -549,9 +549,8 @@ const createEvoluInstance =
549549

550550
const subscribedQueries = createSubscribedQueries(rowsStore);
551551
const loadingPromises = createLoadingPromises(subscribedQueries);
552-
const onCompleteRegistry = createCallbackRegistry(deps);
553-
const exportRegistry =
554-
createCallbackRegistry<Uint8Array<ArrayBuffer>>(deps);
552+
const onCompleteCallbacks = createCallbacks(deps);
553+
const exportCallbacks = createCallbacks<Uint8Array<ArrayBuffer>>(deps);
555554

556555
const dbWorker = deps.createDbWorker(dbConfig.name);
557556

@@ -601,7 +600,7 @@ const createEvoluInstance =
601600
}
602601

603602
for (const id of message.onCompleteIds) {
604-
onCompleteRegistry.execute(id);
603+
onCompleteCallbacks.execute(id);
605604
}
606605
break;
607606
}
@@ -628,13 +627,13 @@ const createEvoluInstance =
628627
if (message.reload) {
629628
deps.reloadApp(reloadUrl);
630629
} else {
631-
onCompleteRegistry.execute(message.onCompleteId);
630+
onCompleteCallbacks.execute(message.onCompleteId);
632631
}
633632
break;
634633
}
635634

636635
case "onExport": {
637-
exportRegistry.execute(
636+
exportCallbacks.execute(
638637
message.onCompleteId,
639638
message.file as Uint8Array<ArrayBuffer>,
640639
);
@@ -748,11 +747,11 @@ const createEvoluInstance =
748747

749748
const processMutationQueue = () => {
750749
const changes: Array<MutationChange> = [];
751-
const onCompleteCallbacks = [];
750+
const onCompletes = [];
752751

753752
for (const [change, onComplete] of mutateMicrotaskQueue) {
754753
if (change !== null) changes.push(change);
755-
if (onComplete) onCompleteCallbacks.push(onComplete);
754+
if (onComplete) onCompletes.push(onComplete);
756755
}
757756

758757
const queueLength = mutateMicrotaskQueue.length;
@@ -764,10 +763,7 @@ const createEvoluInstance =
764763
return;
765764
}
766765

767-
const onCompleteIds = onCompleteCallbacks.map(
768-
onCompleteRegistry.register,
769-
);
770-
766+
const onCompleteIds = onCompletes.map(onCompleteCallbacks.register);
771767
loadingPromises.releaseUnsubscribedOnMutation();
772768

773769
if (!isNonEmptyArray(changes)) return;
@@ -846,7 +842,7 @@ const createEvoluInstance =
846842

847843
resetAppOwner: (options) => {
848844
const { promise, resolve } = Promise.withResolvers<undefined>();
849-
const onCompleteId = onCompleteRegistry.register(resolve);
845+
const onCompleteId = onCompleteCallbacks.register(resolve);
850846
dbWorker.postMessage({
851847
type: "reset",
852848
onCompleteId,
@@ -857,7 +853,7 @@ const createEvoluInstance =
857853

858854
restoreAppOwner: (mnemonic, options) => {
859855
const { promise, resolve } = Promise.withResolvers<undefined>();
860-
const onCompleteId = onCompleteRegistry.register(resolve);
856+
const onCompleteId = onCompleteCallbacks.register(resolve);
861857
dbWorker.postMessage({
862858
type: "reset",
863859
onCompleteId,
@@ -880,7 +876,7 @@ const createEvoluInstance =
880876
exportDatabase: () => {
881877
const { promise, resolve } =
882878
Promise.withResolvers<Uint8Array<ArrayBuffer>>();
883-
const onCompleteId = exportRegistry.register(resolve);
879+
const onCompleteId = exportCallbacks.register(resolve);
884880
dbWorker.postMessage({ type: "export", onCompleteId });
885881
return promise;
886882
},

packages/common/src/Evolu/Sync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createTransferableError, TransferableError } from "../Error.js";
1212
import { constFalse } from "../Function.js";
1313
import { objectToEntries } from "../Object.js";
1414
import { RandomDep } from "../Random.js";
15-
import { createRefCountedResourceManager } from "../RefCountedResourceManager.js";
15+
import { createResources } from "../Resources.js";
1616
import { err, ok, Result } from "../Result.js";
1717
import { sql, SqliteDep, SqliteError, SqliteValue } from "../Sqlite.js";
1818
import { AbortError, createMutex } from "../Task.js";
@@ -253,7 +253,7 @@ export const createSync =
253253
});
254254
};
255255

256-
const transports = createRefCountedResourceManager<
256+
const transports = createResources<
257257
WebSocket,
258258
TransportKey,
259259
OwnerTransport,

packages/common/src/Instances.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface Instances<K extends string, T extends Disposable>
3838
readonly delete: (key: K) => boolean;
3939
}
4040

41-
/** Creates an {@link Instances} manager. */
41+
/** Creates an {@link Instances}. */
4242
export const createInstances = <
4343
K extends string,
4444
T extends Disposable,

0 commit comments

Comments
 (0)