@crdt-sync/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ /**
2
+ * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.
3
+ *
4
+ * In production, import the real `WasmStateStore` from the compiled Wasm
5
+ * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).
6
+ * In tests the interface can be satisfied by any mock object.
7
+ */
8
+ export interface WasmStateStore {
9
+ /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */
10
+ set_register(key: string, value_json: string): string;
11
+ /** Read the current value of a named LWW register as a JSON string, or `undefined`. */
12
+ get_register(key: string): string | undefined;
13
+ /** Apply a remote Envelope (serialised as JSON) to this store. */
14
+ apply_envelope(envelope_json: string): void;
15
+ }
16
+ /**
17
+ * Payload delivered to every `onUpdate` listener when a property is written
18
+ * through the proxy.
19
+ */
20
+ export interface UpdateEvent {
21
+ /** Dot-separated key path that was updated (e.g. `"robot.speed"`). */
22
+ key: string;
23
+ /** The new JavaScript value. */
24
+ value: unknown;
25
+ /**
26
+ * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised
27
+ * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.
28
+ */
29
+ envelope: string;
30
+ }
31
+ /** Callback type for `onUpdate` listeners. */
32
+ export type UpdateHandler = (event: UpdateEvent) => void;
33
+ /**
34
+ * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend
35
+ * developers a **magical, object-oriented** experience.
36
+ *
37
+ * ## How it works
38
+ *
39
+ * 1. **JS `Proxy` interception** – accessing a nested path on `state` returns
40
+ * another `Proxy`. Assigning a value anywhere in the tree intercepts the
41
+ * write and forwards it to the underlying Wasm store via
42
+ * `set_register(dotPath, JSON.stringify(value))`.
43
+ *
44
+ * 2. **Wasm call** – the interceptor immediately calls
45
+ * `WasmStateStore.set_register()` so the CRDT operation is recorded and
46
+ * returns an `Envelope` JSON string ready for broadcasting.
47
+ *
48
+ * 3. **Event emitter** – every write fires all `onUpdate` listeners with the
49
+ * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other
50
+ * UI frameworks to react to state changes.
51
+ *
52
+ * ## Usage
53
+ *
54
+ * ```ts
55
+ * import init, { WasmStateStore } from './crdt_sync.js';
56
+ * import { CrdtStateProxy } from './CrdtStateProxy.js';
57
+ *
58
+ * await init();
59
+ * const store = new WasmStateStore('node-1');
60
+ * const proxy = new CrdtStateProxy(store);
61
+ *
62
+ * // Register a listener (e.g. trigger a React re-render).
63
+ * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {
64
+ * console.log(`${key} =`, value);
65
+ * broadcast(envelope); // send to peers
66
+ * });
67
+ *
68
+ * // Write through the proxy — the interceptor handles everything.
69
+ * proxy.state.speed = 100;
70
+ * proxy.state.robot.x = 42;
71
+ *
72
+ * // Clean up when done.
73
+ * unsubscribe();
74
+ * ```
75
+ */
76
+ export declare class CrdtStateProxy {
77
+ private readonly _store;
78
+ private readonly _handlers;
79
+ private readonly _state;
80
+ /**
81
+ * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
82
+ *
83
+ * @param store - The Wasm state store instance to proxy.
84
+ */
85
+ constructor(store: WasmStateStore);
86
+ /**
87
+ * The proxied state object.
88
+ *
89
+ * Assigning any property (or nested property) on this object will
90
+ * automatically call `WasmStateStore.set_register` and fire `onUpdate`
91
+ * listeners.
92
+ *
93
+ * ```ts
94
+ * proxy.state.speed = 100; // key: "speed"
95
+ * proxy.state.robot.speed = 100; // key: "robot.speed"
96
+ * ```
97
+ */
98
+ get state(): Record<string, unknown>;
99
+ /**
100
+ * Register a listener that is called whenever a property is written through
101
+ * `proxy.state`.
102
+ *
103
+ * @param handler - Callback receiving an `UpdateEvent`.
104
+ * @returns An unsubscribe function — call it to remove the listener.
105
+ */
106
+ onUpdate(handler: UpdateHandler): () => void;
107
+ /**
108
+ * Recursively build a `Proxy` for the given dot-path `prefix`.
109
+ *
110
+ * - **`get` trap**: returns a child proxy for the nested path so that deep
111
+ * assignments like `proxy.state.robot.speed = 100` work correctly.
112
+ * - **`set` trap**: serialises the value, calls `set_register`, and fires
113
+ * all `onUpdate` listeners.
114
+ */
115
+ private _makeProxy;
116
+ /** Dispatch an `UpdateEvent` to all registered handlers. */
117
+ private _emit;
118
+ }
119
+ //# sourceMappingURL=CrdtStateProxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CrdtStateProxy.d.ts","sourceRoot":"","sources":["../src/CrdtStateProxy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,uFAAuF;IACvF,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IACtD,uFAAuF;IACvF,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC9C,kEAAkE;IAClE,cAAc,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,sEAAsE;IACtE,GAAG,EAAE,MAAM,CAAC;IACZ,gCAAgC;IAChC,KAAK,EAAE,OAAO,CAAC;IACf;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,8CAA8C;AAC9C,MAAM,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAiC;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0B;IAEjD;;;;OAIG;gBACS,KAAK,EAAE,cAAc;IAOjC;;;;;;;;;;;OAWG;IACH,IAAI,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEnC;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,IAAI;IAS5C;;;;;;;OAOG;IACH,OAAO,CAAC,UAAU;IAqBlB,4DAA4D;IAC5D,OAAO,CAAC,KAAK;CAGd"}
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CrdtStateProxy = void 0;
4
+ /**
5
+ * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend
6
+ * developers a **magical, object-oriented** experience.
7
+ *
8
+ * ## How it works
9
+ *
10
+ * 1. **JS `Proxy` interception** – accessing a nested path on `state` returns
11
+ * another `Proxy`. Assigning a value anywhere in the tree intercepts the
12
+ * write and forwards it to the underlying Wasm store via
13
+ * `set_register(dotPath, JSON.stringify(value))`.
14
+ *
15
+ * 2. **Wasm call** – the interceptor immediately calls
16
+ * `WasmStateStore.set_register()` so the CRDT operation is recorded and
17
+ * returns an `Envelope` JSON string ready for broadcasting.
18
+ *
19
+ * 3. **Event emitter** – every write fires all `onUpdate` listeners with the
20
+ * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other
21
+ * UI frameworks to react to state changes.
22
+ *
23
+ * ## Usage
24
+ *
25
+ * ```ts
26
+ * import init, { WasmStateStore } from './crdt_sync.js';
27
+ * import { CrdtStateProxy } from './CrdtStateProxy.js';
28
+ *
29
+ * await init();
30
+ * const store = new WasmStateStore('node-1');
31
+ * const proxy = new CrdtStateProxy(store);
32
+ *
33
+ * // Register a listener (e.g. trigger a React re-render).
34
+ * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {
35
+ * console.log(`${key} =`, value);
36
+ * broadcast(envelope); // send to peers
37
+ * });
38
+ *
39
+ * // Write through the proxy — the interceptor handles everything.
40
+ * proxy.state.speed = 100;
41
+ * proxy.state.robot.x = 42;
42
+ *
43
+ * // Clean up when done.
44
+ * unsubscribe();
45
+ * ```
46
+ */
47
+ class CrdtStateProxy {
48
+ /**
49
+ * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
50
+ *
51
+ * @param store - The Wasm state store instance to proxy.
52
+ */
53
+ constructor(store) {
54
+ this._handlers = new Set();
55
+ this._store = store;
56
+ this._state = this._makeProxy('');
57
+ }
58
+ // ── Public API ────────────────────────────────────────────────────────
59
+ /**
60
+ * The proxied state object.
61
+ *
62
+ * Assigning any property (or nested property) on this object will
63
+ * automatically call `WasmStateStore.set_register` and fire `onUpdate`
64
+ * listeners.
65
+ *
66
+ * ```ts
67
+ * proxy.state.speed = 100; // key: "speed"
68
+ * proxy.state.robot.speed = 100; // key: "robot.speed"
69
+ * ```
70
+ */
71
+ get state() {
72
+ return this._state;
73
+ }
74
+ /**
75
+ * Register a listener that is called whenever a property is written through
76
+ * `proxy.state`.
77
+ *
78
+ * @param handler - Callback receiving an `UpdateEvent`.
79
+ * @returns An unsubscribe function — call it to remove the listener.
80
+ */
81
+ onUpdate(handler) {
82
+ this._handlers.add(handler);
83
+ return () => {
84
+ this._handlers.delete(handler);
85
+ };
86
+ }
87
+ // ── Internal helpers ──────────────────────────────────────────────────
88
+ /**
89
+ * Recursively build a `Proxy` for the given dot-path `prefix`.
90
+ *
91
+ * - **`get` trap**: returns a child proxy for the nested path so that deep
92
+ * assignments like `proxy.state.robot.speed = 100` work correctly.
93
+ * - **`set` trap**: serialises the value, calls `set_register`, and fires
94
+ * all `onUpdate` listeners.
95
+ */
96
+ _makeProxy(prefix) {
97
+ const children = {};
98
+ return new Proxy({}, {
99
+ get: (_target, prop) => {
100
+ const key = prefix ? `${prefix}.${prop}` : prop;
101
+ if (!(prop in children)) {
102
+ children[prop] = this._makeProxy(key);
103
+ }
104
+ return children[prop];
105
+ },
106
+ set: (_target, prop, value) => {
107
+ const key = prefix ? `${prefix}.${prop}` : prop;
108
+ const envelope = this._store.set_register(key, JSON.stringify(value));
109
+ this._emit({ key, value, envelope });
110
+ return true;
111
+ },
112
+ });
113
+ }
114
+ /** Dispatch an `UpdateEvent` to all registered handlers. */
115
+ _emit(event) {
116
+ this._handlers.forEach((handler) => handler(event));
117
+ }
118
+ }
119
+ exports.CrdtStateProxy = CrdtStateProxy;
120
+ //# sourceMappingURL=CrdtStateProxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CrdtStateProxy.js","sourceRoot":"","sources":["../src/CrdtStateProxy.ts"],"names":[],"mappings":";;;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAa,cAAc;IAKzB;;;;OAIG;IACH,YAAY,KAAqB;QARhB,cAAS,GAAuB,IAAI,GAAG,EAAE,CAAC;QASzD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACpC,CAAC;IAED,yEAAyE;IAEzE;;;;;;;;;;;OAWG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,OAAsB;QAC7B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC5B,OAAO,GAAG,EAAE;YACV,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACjC,CAAC,CAAC;IACJ,CAAC;IAED,yEAAyE;IAEzE;;;;;;;OAOG;IACK,UAAU,CAAC,MAAc;QAC/B,MAAM,QAAQ,GAA4C,EAAE,CAAC;QAE7D,OAAO,IAAI,KAAK,CAAC,EAA6B,EAAE;YAC9C,GAAG,EAAE,CAAC,OAAO,EAAE,IAAY,EAAE,EAAE;gBAC7B,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;gBAChD,IAAI,CAAC,CAAC,IAAI,IAAI,QAAQ,CAAC,EAAE,CAAC;oBACxB,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;gBACxC,CAAC;gBACD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;YACxB,CAAC;YAED,GAAG,EAAE,CAAC,OAAO,EAAE,IAAY,EAAE,KAAc,EAAE,EAAE;gBAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;gBAChD,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;gBACtE,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;gBACrC,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,4DAA4D;IACpD,KAAK,CAAC,KAAkB;QAC9B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IACtD,CAAC;CACF;AAlFD,wCAkFC"}
@@ -0,0 +1,115 @@
1
+ import { CrdtStateProxy } from './CrdtStateProxy.js';
2
+ import type { WasmStateStore } from './CrdtStateProxy.js';
3
+ /**
4
+ * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.
5
+ *
6
+ * The real browser `WebSocket` satisfies this interface out-of-the-box.
7
+ * In tests, a plain mock object can be used instead.
8
+ */
9
+ export interface WebSocketLike {
10
+ /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */
11
+ readonly readyState: number;
12
+ /** Send a UTF-8 string frame to the server. */
13
+ send(data: string): void;
14
+ /** Initiate the closing handshake. */
15
+ close(): void;
16
+ /** Fired when a message frame is received. */
17
+ onmessage: ((event: {
18
+ data: string;
19
+ }) => void) | null;
20
+ /** Fired when the connection is established. */
21
+ onopen: ((event: unknown) => void) | null;
22
+ /** Fired when the connection is closed. */
23
+ onclose: ((event: unknown) => void) | null;
24
+ /** Fired when an error occurs. */
25
+ onerror: ((event: unknown) => void) | null;
26
+ }
27
+ /**
28
+ * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT
29
+ * operation produced locally is broadcast to peers, and every envelope
30
+ * received from a peer is applied to the local store.
31
+ *
32
+ * ## Data flow
33
+ *
34
+ * ```
35
+ * Local write
36
+ * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)
37
+ * → requestAnimationFrame / setTimeout schedules a batch flush
38
+ * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame
39
+ *
40
+ * Incoming message (single envelope or JSON array of envelopes)
41
+ * → WebSocket.onmessage
42
+ * → WasmStateStore.apply_envelope() // merge into local store
43
+ * ```
44
+ *
45
+ * ## Throttling / batching
46
+ *
47
+ * Multiple proxy writes that occur within the same JavaScript task (e.g. a
48
+ * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single
49
+ * JSON array payload on the next animation frame (browser) or the next
50
+ * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps
51
+ * network traffic proportional to frame rate rather than to the raw mutation
52
+ * rate.
53
+ *
54
+ * ## Usage
55
+ *
56
+ * ```ts
57
+ * import init, { WasmStateStore } from './crdt_sync.js';
58
+ * import { CrdtStateProxy, WebSocketManager } from './index.js';
59
+ *
60
+ * await init();
61
+ * const store = new WasmStateStore('node-1');
62
+ * const proxy = new CrdtStateProxy(store);
63
+ * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/sync'));
64
+ *
65
+ * // Writes are automatically batched and broadcast to peers.
66
+ * proxy.state.robot = { x: 10, y: 20 };
67
+ *
68
+ * // Clean up.
69
+ * manager.disconnect();
70
+ * ```
71
+ */
72
+ export declare class WebSocketManager {
73
+ private readonly _store;
74
+ private readonly _proxy;
75
+ private readonly _ws;
76
+ private _unsubscribe;
77
+ /** Envelopes collected in the current frame, waiting for the batch flush. */
78
+ private _pendingEnvelopes;
79
+ /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
80
+ private _cancelFlush;
81
+ /** Envelopes queued while the socket is not open, flushed on reconnection. */
82
+ private _offlineQueue;
83
+ /**
84
+ * Create a `WebSocketManager` and attach it to the given WebSocket.
85
+ *
86
+ * @param store - The Wasm state store. Incoming peer envelopes will be
87
+ * applied to this store via `apply_envelope`.
88
+ * @param proxy - The CRDT state proxy. Outgoing envelopes produced by
89
+ * `set_register` calls will be read from the proxy's `onUpdate` events.
90
+ * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).
91
+ */
92
+ constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike);
93
+ private _attach;
94
+ /**
95
+ * Schedule a single batch flush for the current frame. Subsequent calls
96
+ * before the flush fires are no-ops (only one flush is ever outstanding).
97
+ *
98
+ * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),
99
+ * falling back to `setTimeout(fn, 0)` in non-browser environments.
100
+ */
101
+ private _scheduleBatchFlush;
102
+ /**
103
+ * Send all pending envelopes as a single JSON-array payload, or move them
104
+ * to the offline queue if the socket is not currently open.
105
+ */
106
+ private _flushBatch;
107
+ /**
108
+ * Unsubscribe from proxy updates, discard any buffered envelopes, and close
109
+ * the WebSocket connection.
110
+ *
111
+ * Safe to call more than once.
112
+ */
113
+ disconnect(): void;
114
+ }
115
+ //# sourceMappingURL=WebSocketManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebSocketManager.d.ts","sourceRoot":"","sources":["../src/WebSocketManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,oFAAoF;IACpF,QAAQ,CAAC,UAAU,EAAE,MAAM,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
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketManager = void 0;
4
+ /**
5
+ * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT
6
+ * operation produced locally is broadcast to peers, and every envelope
7
+ * received from a peer is applied to the local store.
8
+ *
9
+ * ## Data flow
10
+ *
11
+ * ```
12
+ * Local write
13
+ * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)
14
+ * → requestAnimationFrame / setTimeout schedules a batch flush
15
+ * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame
16
+ *
17
+ * Incoming message (single envelope or JSON array of envelopes)
18
+ * → WebSocket.onmessage
19
+ * → WasmStateStore.apply_envelope() // merge into local store
20
+ * ```
21
+ *
22
+ * ## Throttling / batching
23
+ *
24
+ * Multiple proxy writes that occur within the same JavaScript task (e.g. a
25
+ * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single
26
+ * JSON array payload on the next animation frame (browser) or the next
27
+ * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps
28
+ * network traffic proportional to frame rate rather than to the raw mutation
29
+ * rate.
30
+ *
31
+ * ## Usage
32
+ *
33
+ * ```ts
34
+ * import init, { WasmStateStore } from './crdt_sync.js';
35
+ * import { CrdtStateProxy, WebSocketManager } from './index.js';
36
+ *
37
+ * await init();
38
+ * const store = new WasmStateStore('node-1');
39
+ * const proxy = new CrdtStateProxy(store);
40
+ * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/sync'));
41
+ *
42
+ * // Writes are automatically batched and broadcast to peers.
43
+ * proxy.state.robot = { x: 10, y: 20 };
44
+ *
45
+ * // Clean up.
46
+ * manager.disconnect();
47
+ * ```
48
+ */
49
+ class WebSocketManager {
50
+ /**
51
+ * Create a `WebSocketManager` and attach it to the given WebSocket.
52
+ *
53
+ * @param store - The Wasm state store. Incoming peer envelopes will be
54
+ * applied to this store via `apply_envelope`.
55
+ * @param proxy - The CRDT state proxy. Outgoing envelopes produced by
56
+ * `set_register` calls will be read from the proxy's `onUpdate` events.
57
+ * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).
58
+ */
59
+ constructor(store, proxy, ws) {
60
+ this._unsubscribe = null;
61
+ /** Envelopes collected in the current frame, waiting for the batch flush. */
62
+ this._pendingEnvelopes = [];
63
+ /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
64
+ this._cancelFlush = null;
65
+ /** Envelopes queued while the socket is not open, flushed on reconnection. */
66
+ this._offlineQueue = [];
67
+ this._store = store;
68
+ this._proxy = proxy;
69
+ this._ws = ws;
70
+ this._attach();
71
+ }
72
+ // ── Internal setup ────────────────────────────────────────────────────
73
+ _attach() {
74
+ const ws = this._ws;
75
+ // Collect envelopes and schedule a batch flush once per frame so that
76
+ // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced
77
+ // into a single network payload instead of one message per mutation.
78
+ this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {
79
+ this._pendingEnvelopes.push(envelope);
80
+ this._scheduleBatchFlush();
81
+ });
82
+ // On (re)connection, immediately flush pending envelopes together with any
83
+ // envelopes that were queued while the socket was offline.
84
+ ws.onopen = () => {
85
+ // Cancel any scheduled flush — we will drain everything right now.
86
+ this._cancelFlush?.();
87
+ this._cancelFlush = null;
88
+ const offline = this._offlineQueue;
89
+ const pending = this._pendingEnvelopes;
90
+ this._offlineQueue = [];
91
+ this._pendingEnvelopes = [];
92
+ const batch = [...offline, ...pending];
93
+ if (batch.length > 0) {
94
+ ws.send(JSON.stringify(batch));
95
+ }
96
+ };
97
+ // Apply envelopes received from peers. Peers may send either a single
98
+ // envelope JSON string or a JSON array of envelope strings (batch format).
99
+ ws.onmessage = (event) => {
100
+ let parsed;
101
+ try {
102
+ parsed = JSON.parse(event.data);
103
+ }
104
+ catch {
105
+ parsed = null;
106
+ }
107
+ if (Array.isArray(parsed)) {
108
+ for (const env of parsed) {
109
+ this._store.apply_envelope(env);
110
+ }
111
+ }
112
+ else {
113
+ // Fallback: treat the raw frame data as a single envelope string.
114
+ this._store.apply_envelope(event.data);
115
+ }
116
+ };
117
+ // On close or error the subscription stays active so that writes made
118
+ // while offline are buffered and flushed when the socket reconnects.
119
+ ws.onclose = () => { };
120
+ ws.onerror = () => { };
121
+ }
122
+ /**
123
+ * Schedule a single batch flush for the current frame. Subsequent calls
124
+ * before the flush fires are no-ops (only one flush is ever outstanding).
125
+ *
126
+ * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),
127
+ * falling back to `setTimeout(fn, 0)` in non-browser environments.
128
+ */
129
+ _scheduleBatchFlush() {
130
+ if (this._cancelFlush !== null)
131
+ return; // already scheduled
132
+ const doFlush = () => {
133
+ this._cancelFlush = null;
134
+ this._flushBatch();
135
+ };
136
+ if (typeof requestAnimationFrame === 'function') {
137
+ const id = requestAnimationFrame(doFlush);
138
+ this._cancelFlush = () => cancelAnimationFrame(id);
139
+ }
140
+ else {
141
+ const id = setTimeout(doFlush, 0);
142
+ this._cancelFlush = () => clearTimeout(id);
143
+ }
144
+ }
145
+ /**
146
+ * Send all pending envelopes as a single JSON-array payload, or move them
147
+ * to the offline queue if the socket is not currently open.
148
+ */
149
+ _flushBatch() {
150
+ const envelopes = this._pendingEnvelopes.splice(0);
151
+ if (envelopes.length === 0)
152
+ return;
153
+ const ws = this._ws;
154
+ if (ws.readyState === 1 /* OPEN */) {
155
+ ws.send(JSON.stringify(envelopes));
156
+ }
157
+ else {
158
+ this._offlineQueue.push(...envelopes);
159
+ }
160
+ }
161
+ // ── Public API ────────────────────────────────────────────────────────
162
+ /**
163
+ * Unsubscribe from proxy updates, discard any buffered envelopes, and close
164
+ * the WebSocket connection.
165
+ *
166
+ * Safe to call more than once.
167
+ */
168
+ disconnect() {
169
+ this._unsubscribe?.();
170
+ this._unsubscribe = null;
171
+ this._cancelFlush?.();
172
+ this._cancelFlush = null;
173
+ this._pendingEnvelopes = [];
174
+ this._offlineQueue = [];
175
+ this._ws.close();
176
+ }
177
+ }
178
+ exports.WebSocketManager = WebSocketManager;
179
+ //# sourceMappingURL=WebSocketManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebSocketManager.js","sourceRoot":"","sources":["../src/WebSocketManager.ts"],"names":[],"mappings":";;;AA0BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,MAAa,gBAAgB;IAY3B;;;;;;;;OAQG;IACH,YAAY,KAAqB,EAAE,KAAqB,EAAE,EAAiB;QAjBnE,iBAAY,GAAwB,IAAI,CAAC;QACjD,6EAA6E;QACrE,sBAAiB,GAAa,EAAE,CAAC;QACzC,8EAA8E;QACtE,iBAAY,GAAwB,IAAI,CAAC;QACjD,8EAA8E;QACtE,kBAAa,GAAa,EAAE,CAAC;QAYnC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACd,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAED,yEAAyE;IAEjE,OAAO;QACb,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC;QAEpB,sEAAsE;QACtE,sEAAsE;QACtE,qEAAqE;QACrE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;YACxD,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACtC,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;QAEH,2EAA2E;QAC3E,2DAA2D;QAC3D,EAAE,CAAC,MAAM,GAAG,GAAG,EAAE;YACf,mEAAmE;YACnE,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;YACtB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;YACzB,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC;YACnC,MAAM,OAAO,GAAG,IAAI,CAA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EAAE,CAAC;QACxB,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;IACnB,CAAC;CACF;AA3ID,4CA2IC"}
@@ -0,0 +1,5 @@
1
+ export { CrdtStateProxy } from './CrdtStateProxy.js';
2
+ export type { WasmStateStore, UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';
3
+ export { WebSocketManager } from './WebSocketManager.js';
4
+ export type { WebSocketLike } from './WebSocketManager.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACtF,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketManager = exports.CrdtStateProxy = void 0;
4
+ var CrdtStateProxy_js_1 = require("./CrdtStateProxy.js");
5
+ Object.defineProperty(exports, "CrdtStateProxy", { enumerable: true, get: function () { return CrdtStateProxy_js_1.CrdtStateProxy; } });
6
+ var WebSocketManager_js_1 = require("./WebSocketManager.js");
7
+ Object.defineProperty(exports, "WebSocketManager", { enumerable: true, get: function () { return WebSocketManager_js_1.WebSocketManager; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AAEvB,6DAAyD;AAAhD,uHAAA,gBAAgB,OAAA"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@crdt-sync/core",
3
+ "version": "0.1.0",
4
+ "description": "TypeScript proxy wrapper for the crdt-sync Wasm StateStore",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "pkg"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "build:wasm:bundler": "wasm-pack build ../.. --target bundler --out-dir packages/core/pkg/bundler",
14
+ "build:wasm:web": "wasm-pack build ../.. --target web --out-dir packages/core/pkg/web",
15
+ "build:wasm": "npm run build:wasm:bundler && npm run build:wasm:web",
16
+ "test": "jest",
17
+ "lint": "tsc --noEmit"
18
+ },
19
+ "license": "MIT",
20
+ "devDependencies": {
21
+ "@types/jest": "^29.5.12",
22
+ "jest": "^29.7.0",
23
+ "ts-jest": "^29.2.4",
24
+ "typescript": "^5.4.5"
25
+ },
26
+ "jest": {
27
+ "preset": "ts-jest",
28
+ "testEnvironment": "node",
29
+ "testMatch": [
30
+ "**/__tests__/**/*.test.ts"
31
+ ]
32
+ }
33
+ }