@crdt-sync/core 0.3.1 → 0.3.3
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.
- package/dist/index.d.mts +33 -8
- package/dist/index.d.ts +33 -8
- package/dist/index.js +69 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/pkg/web/crdt_sync.d.ts +134 -13
- package/pkg/web/crdt_sync.js +520 -21
package/dist/index.d.mts
CHANGED
|
@@ -66,7 +66,7 @@ type UpdateHandler = (event: UpdateEvent) => void;
|
|
|
66
66
|
* const unsubscribe = proxy.onChange(() => setTick(t => t + 1));
|
|
67
67
|
* ```
|
|
68
68
|
*/
|
|
69
|
-
declare class CrdtStateProxy {
|
|
69
|
+
declare class CrdtStateProxy<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
70
70
|
private readonly _store;
|
|
71
71
|
private readonly _handlers;
|
|
72
72
|
private readonly _changeHandlers;
|
|
@@ -84,7 +84,7 @@ declare class CrdtStateProxy {
|
|
|
84
84
|
* not been set yet. Assigning a value calls `WasmStateStore.set_register`
|
|
85
85
|
* and fires all `onUpdate` and `onChange` listeners.
|
|
86
86
|
*/
|
|
87
|
-
get state():
|
|
87
|
+
get state(): T;
|
|
88
88
|
/**
|
|
89
89
|
* Register a listener that fires whenever a value is written through
|
|
90
90
|
* `proxy.state` (local writes only).
|
|
@@ -169,11 +169,23 @@ interface WebSocketLike {
|
|
|
169
169
|
* → requestAnimationFrame / setTimeout schedules a batch flush
|
|
170
170
|
* → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame
|
|
171
171
|
*
|
|
172
|
-
* Incoming
|
|
173
|
-
* → WebSocket.onmessage
|
|
174
|
-
* → WasmStateStore.apply_envelope()
|
|
172
|
+
* Incoming SNAPSHOT (first message from server after connection opens)
|
|
173
|
+
* → WebSocket.onmessage { type: 'SNAPSHOT', data: envelopes_json }
|
|
174
|
+
* → WasmStateStore.apply_envelope() for each envelope in the snapshot
|
|
175
|
+
* → offline queue flushed so buffered local writes reach the server
|
|
176
|
+
*
|
|
177
|
+
* Incoming UPDATE (peer delta from server)
|
|
178
|
+
* → WebSocket.onmessage { type: 'UPDATE', data: batch_json }
|
|
179
|
+
* → WasmStateStore.apply_envelope() for each envelope in the batch
|
|
175
180
|
* ```
|
|
176
181
|
*
|
|
182
|
+
* ## Snapshot wait
|
|
183
|
+
*
|
|
184
|
+
* On every (re)connection, the manager buffers outgoing envelopes until the
|
|
185
|
+
* server's `SNAPSHOT` message is received. This ensures the local store is
|
|
186
|
+
* hydrated with the server's consolidated state before the client's own writes
|
|
187
|
+
* are broadcast, preventing stale overwrites.
|
|
188
|
+
*
|
|
177
189
|
* ## Throttling / batching
|
|
178
190
|
*
|
|
179
191
|
* Multiple proxy writes that occur within the same JavaScript task (e.g. a
|
|
@@ -192,7 +204,7 @@ interface WebSocketLike {
|
|
|
192
204
|
* await init();
|
|
193
205
|
* const store = new WasmStateStore('node-1');
|
|
194
206
|
* const proxy = new CrdtStateProxy(store);
|
|
195
|
-
* const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/
|
|
207
|
+
* const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/rooms/robot-42'));
|
|
196
208
|
*
|
|
197
209
|
* // Writes are automatically batched and broadcast to peers.
|
|
198
210
|
* proxy.state.robot = { x: 10, y: 20 };
|
|
@@ -210,8 +222,14 @@ declare class WebSocketManager {
|
|
|
210
222
|
private _pendingEnvelopes;
|
|
211
223
|
/** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
|
|
212
224
|
private _cancelFlush;
|
|
213
|
-
/** Envelopes queued while the socket is not open
|
|
225
|
+
/** Envelopes queued while the socket is not open or snapshot has not yet arrived. */
|
|
214
226
|
private _offlineQueue;
|
|
227
|
+
/**
|
|
228
|
+
* Set to `true` once the server's initial `SNAPSHOT` message has been applied.
|
|
229
|
+
* Outgoing envelopes are held in `_offlineQueue` until this flag is set so that
|
|
230
|
+
* the local store is fully hydrated before the client's writes reach peers.
|
|
231
|
+
*/
|
|
232
|
+
private _snapshotReceived;
|
|
215
233
|
/**
|
|
216
234
|
* Create a `WebSocketManager` and attach it to the given WebSocket.
|
|
217
235
|
*
|
|
@@ -233,9 +251,16 @@ declare class WebSocketManager {
|
|
|
233
251
|
private _scheduleBatchFlush;
|
|
234
252
|
/**
|
|
235
253
|
* Send all pending envelopes as a single JSON-array payload, or move them
|
|
236
|
-
* to the offline queue if the socket is not currently open
|
|
254
|
+
* to the offline queue if the socket is not currently open or the snapshot
|
|
255
|
+
* has not yet been received.
|
|
237
256
|
*/
|
|
238
257
|
private _flushBatch;
|
|
258
|
+
/**
|
|
259
|
+
* Send all offline-queued envelopes in a single batch.
|
|
260
|
+
* Called after the server's SNAPSHOT has been applied so that buffered
|
|
261
|
+
* local writes finally reach peers.
|
|
262
|
+
*/
|
|
263
|
+
private _flushOfflineQueue;
|
|
239
264
|
/**
|
|
240
265
|
* Unsubscribe from proxy updates, discard any buffered envelopes, and close
|
|
241
266
|
* the WebSocket connection.
|
package/dist/index.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ type UpdateHandler = (event: UpdateEvent) => void;
|
|
|
66
66
|
* const unsubscribe = proxy.onChange(() => setTick(t => t + 1));
|
|
67
67
|
* ```
|
|
68
68
|
*/
|
|
69
|
-
declare class CrdtStateProxy {
|
|
69
|
+
declare class CrdtStateProxy<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
70
70
|
private readonly _store;
|
|
71
71
|
private readonly _handlers;
|
|
72
72
|
private readonly _changeHandlers;
|
|
@@ -84,7 +84,7 @@ declare class CrdtStateProxy {
|
|
|
84
84
|
* not been set yet. Assigning a value calls `WasmStateStore.set_register`
|
|
85
85
|
* and fires all `onUpdate` and `onChange` listeners.
|
|
86
86
|
*/
|
|
87
|
-
get state():
|
|
87
|
+
get state(): T;
|
|
88
88
|
/**
|
|
89
89
|
* Register a listener that fires whenever a value is written through
|
|
90
90
|
* `proxy.state` (local writes only).
|
|
@@ -169,11 +169,23 @@ interface WebSocketLike {
|
|
|
169
169
|
* → requestAnimationFrame / setTimeout schedules a batch flush
|
|
170
170
|
* → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame
|
|
171
171
|
*
|
|
172
|
-
* Incoming
|
|
173
|
-
* → WebSocket.onmessage
|
|
174
|
-
* → WasmStateStore.apply_envelope()
|
|
172
|
+
* Incoming SNAPSHOT (first message from server after connection opens)
|
|
173
|
+
* → WebSocket.onmessage { type: 'SNAPSHOT', data: envelopes_json }
|
|
174
|
+
* → WasmStateStore.apply_envelope() for each envelope in the snapshot
|
|
175
|
+
* → offline queue flushed so buffered local writes reach the server
|
|
176
|
+
*
|
|
177
|
+
* Incoming UPDATE (peer delta from server)
|
|
178
|
+
* → WebSocket.onmessage { type: 'UPDATE', data: batch_json }
|
|
179
|
+
* → WasmStateStore.apply_envelope() for each envelope in the batch
|
|
175
180
|
* ```
|
|
176
181
|
*
|
|
182
|
+
* ## Snapshot wait
|
|
183
|
+
*
|
|
184
|
+
* On every (re)connection, the manager buffers outgoing envelopes until the
|
|
185
|
+
* server's `SNAPSHOT` message is received. This ensures the local store is
|
|
186
|
+
* hydrated with the server's consolidated state before the client's own writes
|
|
187
|
+
* are broadcast, preventing stale overwrites.
|
|
188
|
+
*
|
|
177
189
|
* ## Throttling / batching
|
|
178
190
|
*
|
|
179
191
|
* Multiple proxy writes that occur within the same JavaScript task (e.g. a
|
|
@@ -192,7 +204,7 @@ interface WebSocketLike {
|
|
|
192
204
|
* await init();
|
|
193
205
|
* const store = new WasmStateStore('node-1');
|
|
194
206
|
* const proxy = new CrdtStateProxy(store);
|
|
195
|
-
* const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/
|
|
207
|
+
* const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/rooms/robot-42'));
|
|
196
208
|
*
|
|
197
209
|
* // Writes are automatically batched and broadcast to peers.
|
|
198
210
|
* proxy.state.robot = { x: 10, y: 20 };
|
|
@@ -210,8 +222,14 @@ declare class WebSocketManager {
|
|
|
210
222
|
private _pendingEnvelopes;
|
|
211
223
|
/** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
|
|
212
224
|
private _cancelFlush;
|
|
213
|
-
/** Envelopes queued while the socket is not open
|
|
225
|
+
/** Envelopes queued while the socket is not open or snapshot has not yet arrived. */
|
|
214
226
|
private _offlineQueue;
|
|
227
|
+
/**
|
|
228
|
+
* Set to `true` once the server's initial `SNAPSHOT` message has been applied.
|
|
229
|
+
* Outgoing envelopes are held in `_offlineQueue` until this flag is set so that
|
|
230
|
+
* the local store is fully hydrated before the client's writes reach peers.
|
|
231
|
+
*/
|
|
232
|
+
private _snapshotReceived;
|
|
215
233
|
/**
|
|
216
234
|
* Create a `WebSocketManager` and attach it to the given WebSocket.
|
|
217
235
|
*
|
|
@@ -233,9 +251,16 @@ declare class WebSocketManager {
|
|
|
233
251
|
private _scheduleBatchFlush;
|
|
234
252
|
/**
|
|
235
253
|
* Send all pending envelopes as a single JSON-array payload, or move them
|
|
236
|
-
* to the offline queue if the socket is not currently open
|
|
254
|
+
* to the offline queue if the socket is not currently open or the snapshot
|
|
255
|
+
* has not yet been received.
|
|
237
256
|
*/
|
|
238
257
|
private _flushBatch;
|
|
258
|
+
/**
|
|
259
|
+
* Send all offline-queued envelopes in a single batch.
|
|
260
|
+
* Called after the server's SNAPSHOT has been applied so that buffered
|
|
261
|
+
* local writes finally reach peers.
|
|
262
|
+
*/
|
|
263
|
+
private _flushOfflineQueue;
|
|
239
264
|
/**
|
|
240
265
|
* Unsubscribe from proxy updates, discard any buffered envelopes, and close
|
|
241
266
|
* the WebSocket connection.
|
package/dist/index.js
CHANGED
|
@@ -161,8 +161,14 @@ var WebSocketManager = class {
|
|
|
161
161
|
this._pendingEnvelopes = [];
|
|
162
162
|
/** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
|
|
163
163
|
this._cancelFlush = null;
|
|
164
|
-
/** Envelopes queued while the socket is not open
|
|
164
|
+
/** Envelopes queued while the socket is not open or snapshot has not yet arrived. */
|
|
165
165
|
this._offlineQueue = [];
|
|
166
|
+
/**
|
|
167
|
+
* Set to `true` once the server's initial `SNAPSHOT` message has been applied.
|
|
168
|
+
* Outgoing envelopes are held in `_offlineQueue` until this flag is set so that
|
|
169
|
+
* the local store is fully hydrated before the client's writes reach peers.
|
|
170
|
+
*/
|
|
171
|
+
this._snapshotReceived = false;
|
|
166
172
|
this._store = store;
|
|
167
173
|
this._proxy = proxy;
|
|
168
174
|
this._ws = ws;
|
|
@@ -176,16 +182,12 @@ var WebSocketManager = class {
|
|
|
176
182
|
this._scheduleBatchFlush();
|
|
177
183
|
});
|
|
178
184
|
ws.onopen = () => {
|
|
185
|
+
this._snapshotReceived = false;
|
|
179
186
|
this._cancelFlush?.();
|
|
180
187
|
this._cancelFlush = null;
|
|
181
|
-
const offline = this._offlineQueue;
|
|
182
188
|
const pending = this._pendingEnvelopes;
|
|
183
|
-
this._offlineQueue = [];
|
|
184
189
|
this._pendingEnvelopes = [];
|
|
185
|
-
|
|
186
|
-
if (batch.length > 0) {
|
|
187
|
-
ws.send(JSON.stringify(batch));
|
|
188
|
-
}
|
|
190
|
+
this._offlineQueue.push(...pending);
|
|
189
191
|
};
|
|
190
192
|
ws.onmessage = (event) => {
|
|
191
193
|
let parsed;
|
|
@@ -194,9 +196,52 @@ var WebSocketManager = class {
|
|
|
194
196
|
} catch {
|
|
195
197
|
parsed = null;
|
|
196
198
|
}
|
|
199
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) && "type" in parsed) {
|
|
200
|
+
const msg = parsed;
|
|
201
|
+
if (msg.type === "SNAPSHOT") {
|
|
202
|
+
let snapshotEnvelopes;
|
|
203
|
+
try {
|
|
204
|
+
snapshotEnvelopes = JSON.parse(msg.data);
|
|
205
|
+
} catch {
|
|
206
|
+
snapshotEnvelopes = [];
|
|
207
|
+
}
|
|
208
|
+
if (Array.isArray(snapshotEnvelopes)) {
|
|
209
|
+
for (const env of snapshotEnvelopes) {
|
|
210
|
+
if (typeof env === "string") {
|
|
211
|
+
this._store.apply_envelope(env);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
this._snapshotReceived = true;
|
|
216
|
+
this._proxy.notifyRemoteUpdate();
|
|
217
|
+
this._flushOfflineQueue();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (msg.type === "UPDATE") {
|
|
221
|
+
let updateEnvelopes;
|
|
222
|
+
try {
|
|
223
|
+
updateEnvelopes = JSON.parse(msg.data);
|
|
224
|
+
} catch {
|
|
225
|
+
updateEnvelopes = null;
|
|
226
|
+
}
|
|
227
|
+
if (Array.isArray(updateEnvelopes)) {
|
|
228
|
+
for (const env of updateEnvelopes) {
|
|
229
|
+
if (typeof env === "string") {
|
|
230
|
+
this._store.apply_envelope(env);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
} else {
|
|
234
|
+
this._store.apply_envelope(msg.data);
|
|
235
|
+
}
|
|
236
|
+
this._proxy.notifyRemoteUpdate();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
197
240
|
if (Array.isArray(parsed)) {
|
|
198
241
|
for (const env of parsed) {
|
|
199
|
-
|
|
242
|
+
if (typeof env === "string") {
|
|
243
|
+
this._store.apply_envelope(env);
|
|
244
|
+
}
|
|
200
245
|
}
|
|
201
246
|
} else {
|
|
202
247
|
this._store.apply_envelope(event.data);
|
|
@@ -231,18 +276,32 @@ var WebSocketManager = class {
|
|
|
231
276
|
}
|
|
232
277
|
/**
|
|
233
278
|
* Send all pending envelopes as a single JSON-array payload, or move them
|
|
234
|
-
* to the offline queue if the socket is not currently open
|
|
279
|
+
* to the offline queue if the socket is not currently open or the snapshot
|
|
280
|
+
* has not yet been received.
|
|
235
281
|
*/
|
|
236
282
|
_flushBatch() {
|
|
237
283
|
const envelopes = this._pendingEnvelopes.splice(0);
|
|
238
284
|
if (envelopes.length === 0) return;
|
|
239
285
|
const ws = this._ws;
|
|
240
|
-
if (ws.readyState === 1) {
|
|
286
|
+
if (ws.readyState === 1 && this._snapshotReceived) {
|
|
241
287
|
ws.send(JSON.stringify(envelopes));
|
|
242
288
|
} else {
|
|
243
289
|
this._offlineQueue.push(...envelopes);
|
|
244
290
|
}
|
|
245
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Send all offline-queued envelopes in a single batch.
|
|
294
|
+
* Called after the server's SNAPSHOT has been applied so that buffered
|
|
295
|
+
* local writes finally reach peers.
|
|
296
|
+
*/
|
|
297
|
+
_flushOfflineQueue() {
|
|
298
|
+
const offline = this._offlineQueue.splice(0);
|
|
299
|
+
if (offline.length === 0) return;
|
|
300
|
+
const ws = this._ws;
|
|
301
|
+
if (ws.readyState === 1) {
|
|
302
|
+
ws.send(JSON.stringify(offline));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
246
305
|
// ── Public API ────────────────────────────────────────────────────────
|
|
247
306
|
/**
|
|
248
307
|
* Unsubscribe from proxy updates, discard any buffered envelopes, and close
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/CrdtStateProxy.ts","../src/WebSocketManager.ts"],"sourcesContent":["export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';\nexport { default as initWasm, WasmStateStore } from '../pkg/web/crdt_sync.js';\nexport { WebSocketManager } from './WebSocketManager.js';\nexport type { WebSocketLike } from './WebSocketManager.js';\n","/**\n * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.\n *\n * In production, import the real `WasmStateStore` from the compiled Wasm\n * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).\n * In tests the interface can be satisfied by any mock object.\n */\nexport interface WasmStateStore {\n /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */\n set_register(key: string, value_json: string): string;\n /** Read the current value of a named LWW register as a JSON string, or `undefined`. */\n get_register(key: string): string | undefined;\n /** Apply a remote Envelope (serialised as JSON) to this store. */\n apply_envelope(envelope_json: string): void;\n}\n\n/**\n * Payload delivered to every `onUpdate` listener when a property is written\n * through the proxy.\n */\nexport interface UpdateEvent {\n /** The register key that was updated (e.g. `\"speed\"` or `\"robot.speed\"`). */\n key: string;\n /** The new JavaScript value. */\n value: unknown;\n /**\n * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised\n * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.\n */\n envelope: string;\n}\n\n/** Callback type for `onUpdate` listeners. */\nexport type UpdateHandler = (event: UpdateEvent) => void;\n\n/**\n * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend\n * developers a transparent, object-oriented experience.\n *\n * ## Reading state\n *\n * Access any registered key directly on `proxy.state`:\n *\n * ```ts\n * proxy.state.speed // returns the registered value, or undefined\n * proxy.state[\"robot.speed\"] // dot-path keys via bracket notation\n * ```\n *\n * ## Writing state\n *\n * Assign any value — primitives, objects, arrays:\n *\n * ```ts\n * proxy.state.speed = 100;\n * proxy.state.robot = { x: 10, y: 20 }; // store the whole object\n * proxy.state[\"robot.speed\"] = 100; // dot-path key\n * ```\n *\n * ## Listening for changes\n *\n * - `onUpdate(handler)` — fires on **local** writes with the full `UpdateEvent`\n * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.\n * - `onChange(handler)` — fires on **any** change: local writes _and_ remote\n * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.\n *\n * ```ts\n * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));\n * ```\n */\nexport class CrdtStateProxy {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = new Set();\n private readonly _changeHandlers: Set<() => void> = new Set();\n private readonly _state: Record<string, unknown>;\n\n /**\n * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.\n *\n * @param store - The Wasm state store instance to proxy.\n */\n constructor(store: WasmStateStore) {\n this._store = store;\n this._state = this._makeProxy();\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * The proxied state object.\n *\n * Reading a key returns the registered value, or `undefined` if it has\n * not been set yet. Assigning a value calls `WasmStateStore.set_register`\n * and fires all `onUpdate` and `onChange` listeners.\n */\n get state(): Record<string, unknown> {\n return this._state;\n }\n\n /**\n * Register a listener that fires whenever a value is written through\n * `proxy.state` (local writes only).\n *\n * The `UpdateEvent` carries the register key, new value, and the CRDT\n * envelope — forward the envelope to peers via `apply_envelope`.\n *\n * @returns An unsubscribe function.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n /**\n * Register a listener that fires whenever state changes — whether from a\n * local write or an incoming remote update (after `notifyRemoteUpdate`).\n *\n * Use this in UI frameworks to trigger re-renders:\n *\n * ```ts\n * proxy.onChange(() => setTick(t => t + 1));\n * ```\n *\n * @returns An unsubscribe function.\n */\n onChange(handler: () => void): () => void {\n this._changeHandlers.add(handler);\n return () => {\n this._changeHandlers.delete(handler);\n };\n }\n\n /**\n * Notify all `onChange` listeners that remote state has been applied to the\n * store. Called by `WebSocketManager` after every `apply_envelope` so that\n * UI frameworks re-render with the latest state.\n */\n notifyRemoteUpdate(): void {\n this._emitChange();\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Build the state `Proxy`.\n *\n * - **`get` trap**: reads the value from the WASM store via `get_register`.\n * Returns the parsed value, or `undefined` if the key has not been registered.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` and `onChange` listeners.\n *\n * Keys may be dot-separated paths (e.g. `\"robot.speed\"`) when using bracket\n * notation: `proxy.state[\"robot.speed\"] = 100`.\n */\n private _makeProxy(): Record<string, unknown> {\n return new Proxy({} as Record<string, unknown>, {\n get: (_target, prop) => {\n if (typeof prop === 'symbol') return undefined;\n const raw = this._store.get_register(String(prop));\n return raw !== undefined ? JSON.parse(raw) : undefined;\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = String(prop);\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n this._emitChange();\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\n }\n\n /** Notify all `onChange` handlers of a state change. */\n private _emitChange(): void {\n this._changeHandlers.forEach((handler) => handler());\n }\n}\n","import { CrdtStateProxy } from './CrdtStateProxy.js';\nimport type { WasmStateStore } from './CrdtStateProxy.js';\n\n/**\n * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.\n *\n * The real browser `WebSocket` satisfies this interface out-of-the-box.\n * In tests, a plain mock object can be used instead.\n */\nexport interface WebSocketLike {\n /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */\n readonly readyState: number;\n /** Send a UTF-8 string frame to the server. */\n send(data: string): void;\n /** Initiate the closing handshake. */\n close(): void;\n /** Fired when a message frame is received. */\n onmessage: ((event: { data: string }) => void) | null;\n /** Fired when the connection is established. */\n onopen: ((event: unknown) => void) | null;\n /** Fired when the connection is closed. */\n onclose: ((event: unknown) => void) | null;\n /** Fired when an error occurs. */\n onerror: ((event: unknown) => void) | null;\n}\n\n/**\n * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT\n * operation produced locally is broadcast to peers, and every envelope\n * received from a peer is applied to the local store.\n *\n * ## Data flow\n *\n * ```\n * Local write\n * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)\n * → requestAnimationFrame / setTimeout schedules a batch flush\n * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame\n *\n * Incoming message (single envelope or JSON array of envelopes)\n * → WebSocket.onmessage\n * → WasmStateStore.apply_envelope() // merge into local store\n * ```\n *\n * ## Throttling / batching\n *\n * Multiple proxy writes that occur within the same JavaScript task (e.g. a\n * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single\n * JSON array payload on the next animation frame (browser) or the next\n * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps\n * network traffic proportional to frame rate rather than to the raw mutation\n * rate.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy, WebSocketManager } from './index.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/sync'));\n *\n * // Writes are automatically batched and broadcast to peers.\n * proxy.state.robot = { x: 10, y: 20 };\n *\n * // Clean up.\n * manager.disconnect();\n * ```\n */\nexport class WebSocketManager {\n private readonly _store: WasmStateStore;\n private readonly _proxy: CrdtStateProxy;\n private readonly _ws: WebSocketLike;\n private _unsubscribe: (() => void) | null = null;\n /** Envelopes collected in the current frame, waiting for the batch flush. */\n private _pendingEnvelopes: string[] = [];\n /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */\n private _cancelFlush: (() => void) | null = null;\n /** Envelopes queued while the socket is not open, flushed on reconnection. */\n private _offlineQueue: string[] = [];\n\n /**\n * Create a `WebSocketManager` and attach it to the given WebSocket.\n *\n * @param store - The Wasm state store. Incoming peer envelopes will be\n * applied to this store via `apply_envelope`.\n * @param proxy - The CRDT state proxy. Outgoing envelopes produced by\n * `set_register` calls will be read from the proxy's `onUpdate` events.\n * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).\n */\n constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike) {\n this._store = store;\n this._proxy = proxy;\n this._ws = ws;\n this._attach();\n }\n\n // ── Internal setup ────────────────────────────────────────────────────\n\n private _attach(): void {\n const ws = this._ws;\n\n // Collect envelopes and schedule a batch flush once per frame so that\n // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced\n // into a single network payload instead of one message per mutation.\n this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {\n this._pendingEnvelopes.push(envelope);\n this._scheduleBatchFlush();\n });\n\n // On (re)connection, immediately flush pending envelopes together with any\n // envelopes that were queued while the socket was offline.\n ws.onopen = () => {\n // Cancel any scheduled flush — we will drain everything right now.\n this._cancelFlush?.();\n this._cancelFlush = null;\n const offline = this._offlineQueue;\n const pending = this._pendingEnvelopes;\n this._offlineQueue = [];\n this._pendingEnvelopes = [];\n const batch = [...offline, ...pending];\n if (batch.length > 0) {\n ws.send(JSON.stringify(batch));\n }\n };\n\n // Apply envelopes received from peers. Peers may send either a single\n // envelope JSON string or a JSON array of envelope strings (batch format).\n ws.onmessage = (event) => {\n let parsed: unknown;\n try {\n parsed = JSON.parse(event.data);\n } catch {\n parsed = null;\n }\n if (Array.isArray(parsed)) {\n for (const env of parsed) {\n this._store.apply_envelope(env as string);\n }\n } else {\n // Fallback: treat the raw frame data as a single envelope string.\n this._store.apply_envelope(event.data);\n }\n // Notify UI listeners (e.g. React) that remote state has been applied.\n this._proxy.notifyRemoteUpdate();\n };\n\n // On close or error the subscription stays active so that writes made\n // while offline are buffered and flushed when the socket reconnects.\n ws.onclose = () => { /* keep buffering */ };\n ws.onerror = () => { /* keep buffering */ };\n }\n\n /**\n * Schedule a single batch flush for the current frame. Subsequent calls\n * before the flush fires are no-ops (only one flush is ever outstanding).\n *\n * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),\n * falling back to `setTimeout(fn, 0)` in non-browser environments.\n */\n private _scheduleBatchFlush(): void {\n if (this._cancelFlush !== null) return; // already scheduled\n\n const doFlush = () => {\n this._cancelFlush = null;\n this._flushBatch();\n };\n\n if (typeof requestAnimationFrame === 'function') {\n const id = requestAnimationFrame(doFlush);\n this._cancelFlush = () => cancelAnimationFrame(id);\n } else {\n const id = setTimeout(doFlush, 0);\n this._cancelFlush = () => clearTimeout(id);\n }\n }\n\n /**\n * Send all pending envelopes as a single JSON-array payload, or move them\n * to the offline queue if the socket is not currently open.\n */\n private _flushBatch(): void {\n const envelopes = this._pendingEnvelopes.splice(0);\n if (envelopes.length === 0) return;\n\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */) {\n ws.send(JSON.stringify(envelopes));\n } else {\n this._offlineQueue.push(...envelopes);\n }\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * Unsubscribe from proxy updates, discard any buffered envelopes, and close\n * the WebSocket connection.\n *\n * Safe to call more than once.\n */\n disconnect(): void {\n this._unsubscribe?.();\n this._unsubscribe = null;\n this._cancelFlush?.();\n this._cancelFlush = null;\n this._pendingEnvelopes = [];\n this._offlineQueue = [];\n this._ws.close();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqEO,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW1B,YAAY,OAAuB;AATnC,SAAiB,YAAgC,oBAAI,IAAI;AACzD,SAAiB,kBAAmC,oBAAI,IAAI;AAS1D,SAAK,SAAS;AACd,SAAK,SAAS,KAAK,WAAW;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,IAAI,QAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,SAAS,SAAoC;AAC3C,SAAK,UAAU,IAAI,OAAO;AAC1B,WAAO,MAAM;AACX,WAAK,UAAU,OAAO,OAAO;AAAA,IAC/B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,SAAS,SAAiC;AACxC,SAAK,gBAAgB,IAAI,OAAO;AAChC,WAAO,MAAM;AACX,WAAK,gBAAgB,OAAO,OAAO;AAAA,IACrC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,qBAA2B;AACzB,SAAK,YAAY;AAAA,EACnB;AAAA;AAAA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;AAAA;AAAA;AAAA;AAAA,EAMQ,cAAoB;AAC1B,UAAM,YAAY,KAAK,kBAAkB,OAAO,CAAC;AACjD,QAAI,UAAU,WAAW,EAAG;AAE5B,UAAM,KAAK,KAAK;AAChB,QAAI,GAAG,eAAe,GAAc;AAClC,SAAG,KAAK,KAAK,UAAU,SAAS,CAAC;AAAA,IACnC,OAAO;AACL,WAAK,cAAc,KAAK,GAAG,SAAS;AAAA,IACtC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,aAAmB;AACjB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,oBAAoB,CAAC;AAC1B,SAAK,gBAAgB,CAAC;AACtB,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/CrdtStateProxy.ts","../src/WebSocketManager.ts"],"sourcesContent":["export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';\nexport { default as initWasm, WasmStateStore } from '../pkg/web/crdt_sync.js';\nexport { WebSocketManager } from './WebSocketManager.js';\nexport type { WebSocketLike } from './WebSocketManager.js';\n","/**\n * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.\n *\n * In production, import the real `WasmStateStore` from the compiled Wasm\n * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).\n * In tests the interface can be satisfied by any mock object.\n */\nexport interface WasmStateStore {\n /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */\n set_register(key: string, value_json: string): string;\n /** Read the current value of a named LWW register as a JSON string, or `undefined`. */\n get_register(key: string): string | undefined;\n /** Apply a remote Envelope (serialised as JSON) to this store. */\n apply_envelope(envelope_json: string): void;\n}\n\n/**\n * Payload delivered to every `onUpdate` listener when a property is written\n * through the proxy.\n */\nexport interface UpdateEvent {\n /** The register key that was updated (e.g. `\"speed\"` or `\"robot.speed\"`). */\n key: string;\n /** The new JavaScript value. */\n value: unknown;\n /**\n * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised\n * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.\n */\n envelope: string;\n}\n\n/** Callback type for `onUpdate` listeners. */\nexport type UpdateHandler = (event: UpdateEvent) => void;\n\n/**\n * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend\n * developers a transparent, object-oriented experience.\n *\n * ## Reading state\n *\n * Access any registered key directly on `proxy.state`:\n *\n * ```ts\n * proxy.state.speed // returns the registered value, or undefined\n * proxy.state[\"robot.speed\"] // dot-path keys via bracket notation\n * ```\n *\n * ## Writing state\n *\n * Assign any value — primitives, objects, arrays:\n *\n * ```ts\n * proxy.state.speed = 100;\n * proxy.state.robot = { x: 10, y: 20 }; // store the whole object\n * proxy.state[\"robot.speed\"] = 100; // dot-path key\n * ```\n *\n * ## Listening for changes\n *\n * - `onUpdate(handler)` — fires on **local** writes with the full `UpdateEvent`\n * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.\n * - `onChange(handler)` — fires on **any** change: local writes _and_ remote\n * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.\n *\n * ```ts\n * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));\n * ```\n */\nexport class CrdtStateProxy<T extends Record<string, unknown> = Record<string, unknown>> {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = new Set();\n private readonly _changeHandlers: Set<() => void> = new Set();\n private readonly _state: T;\n\n /**\n * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.\n *\n * @param store - The Wasm state store instance to proxy.\n */\n constructor(store: WasmStateStore) {\n this._store = store;\n this._state = this._makeProxy();\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * The proxied state object.\n *\n * Reading a key returns the registered value, or `undefined` if it has\n * not been set yet. Assigning a value calls `WasmStateStore.set_register`\n * and fires all `onUpdate` and `onChange` listeners.\n */\n get state(): T {\n return this._state;\n }\n\n /**\n * Register a listener that fires whenever a value is written through\n * `proxy.state` (local writes only).\n *\n * The `UpdateEvent` carries the register key, new value, and the CRDT\n * envelope — forward the envelope to peers via `apply_envelope`.\n *\n * @returns An unsubscribe function.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n /**\n * Register a listener that fires whenever state changes — whether from a\n * local write or an incoming remote update (after `notifyRemoteUpdate`).\n *\n * Use this in UI frameworks to trigger re-renders:\n *\n * ```ts\n * proxy.onChange(() => setTick(t => t + 1));\n * ```\n *\n * @returns An unsubscribe function.\n */\n onChange(handler: () => void): () => void {\n this._changeHandlers.add(handler);\n return () => {\n this._changeHandlers.delete(handler);\n };\n }\n\n /**\n * Notify all `onChange` listeners that remote state has been applied to the\n * store. Called by `WebSocketManager` after every `apply_envelope` so that\n * UI frameworks re-render with the latest state.\n */\n notifyRemoteUpdate(): void {\n this._emitChange();\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Build the state `Proxy`.\n *\n * - **`get` trap**: reads the value from the WASM store via `get_register`.\n * Returns the parsed value, or `undefined` if the key has not been registered.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` and `onChange` listeners.\n *\n * Keys may be dot-separated paths (e.g. `\"robot.speed\"`) when using bracket\n * notation: `proxy.state[\"robot.speed\"] = 100`.\n */\n private _makeProxy(): T {\n return new Proxy({} as T, {\n get: (_target, prop) => {\n if (typeof prop === 'symbol') return undefined;\n const raw = this._store.get_register(String(prop));\n return raw !== undefined ? JSON.parse(raw) : undefined;\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = String(prop);\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n this._emitChange();\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\n }\n\n /** Notify all `onChange` handlers of a state change. */\n private _emitChange(): void {\n this._changeHandlers.forEach((handler) => handler());\n }\n}\n","import { CrdtStateProxy } from './CrdtStateProxy.js';\nimport type { WasmStateStore } from './CrdtStateProxy.js';\n\n/**\n * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.\n *\n * The real browser `WebSocket` satisfies this interface out-of-the-box.\n * In tests, a plain mock object can be used instead.\n */\nexport interface WebSocketLike {\n /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */\n readonly readyState: number;\n /** Send a UTF-8 string frame to the server. */\n send(data: string): void;\n /** Initiate the closing handshake. */\n close(): void;\n /** Fired when a message frame is received. */\n onmessage: ((event: { data: string }) => void) | null;\n /** Fired when the connection is established. */\n onopen: ((event: unknown) => void) | null;\n /** Fired when the connection is closed. */\n onclose: ((event: unknown) => void) | null;\n /** Fired when an error occurs. */\n onerror: ((event: unknown) => void) | null;\n}\n\n/**\n * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT\n * operation produced locally is broadcast to peers, and every envelope\n * received from a peer is applied to the local store.\n *\n * ## Data flow\n *\n * ```\n * Local write\n * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)\n * → requestAnimationFrame / setTimeout schedules a batch flush\n * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame\n *\n * Incoming SNAPSHOT (first message from server after connection opens)\n * → WebSocket.onmessage { type: 'SNAPSHOT', data: envelopes_json }\n * → WasmStateStore.apply_envelope() for each envelope in the snapshot\n * → offline queue flushed so buffered local writes reach the server\n *\n * Incoming UPDATE (peer delta from server)\n * → WebSocket.onmessage { type: 'UPDATE', data: batch_json }\n * → WasmStateStore.apply_envelope() for each envelope in the batch\n * ```\n *\n * ## Snapshot wait\n *\n * On every (re)connection, the manager buffers outgoing envelopes until the\n * server's `SNAPSHOT` message is received. This ensures the local store is\n * hydrated with the server's consolidated state before the client's own writes\n * are broadcast, preventing stale overwrites.\n *\n * ## Throttling / batching\n *\n * Multiple proxy writes that occur within the same JavaScript task (e.g. a\n * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single\n * JSON array payload on the next animation frame (browser) or the next\n * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps\n * network traffic proportional to frame rate rather than to the raw mutation\n * rate.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy, WebSocketManager } from './index.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/rooms/robot-42'));\n *\n * // Writes are automatically batched and broadcast to peers.\n * proxy.state.robot = { x: 10, y: 20 };\n *\n * // Clean up.\n * manager.disconnect();\n * ```\n */\nexport class WebSocketManager {\n private readonly _store: WasmStateStore;\n private readonly _proxy: CrdtStateProxy;\n private readonly _ws: WebSocketLike;\n private _unsubscribe: (() => void) | null = null;\n /** Envelopes collected in the current frame, waiting for the batch flush. */\n private _pendingEnvelopes: string[] = [];\n /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */\n private _cancelFlush: (() => void) | null = null;\n /** Envelopes queued while the socket is not open or snapshot has not yet arrived. */\n private _offlineQueue: string[] = [];\n /**\n * Set to `true` once the server's initial `SNAPSHOT` message has been applied.\n * Outgoing envelopes are held in `_offlineQueue` until this flag is set so that\n * the local store is fully hydrated before the client's writes reach peers.\n */\n private _snapshotReceived: boolean = false;\n\n /**\n * Create a `WebSocketManager` and attach it to the given WebSocket.\n *\n * @param store - The Wasm state store. Incoming peer envelopes will be\n * applied to this store via `apply_envelope`.\n * @param proxy - The CRDT state proxy. Outgoing envelopes produced by\n * `set_register` calls will be read from the proxy's `onUpdate` events.\n * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).\n */\n constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike) {\n this._store = store;\n this._proxy = proxy;\n this._ws = ws;\n this._attach();\n }\n\n // ── Internal setup ────────────────────────────────────────────────────\n\n private _attach(): void {\n const ws = this._ws;\n\n // Collect envelopes and schedule a batch flush once per frame so that\n // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced\n // into a single network payload instead of one message per mutation.\n this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {\n this._pendingEnvelopes.push(envelope);\n this._scheduleBatchFlush();\n });\n\n // On (re)connection, cancel any pending batch flush and move everything\n // to the offline queue. Outgoing writes are held there until the server's\n // SNAPSHOT message arrives and the local store has been hydrated.\n ws.onopen = () => {\n this._snapshotReceived = false;\n this._cancelFlush?.();\n this._cancelFlush = null;\n const pending = this._pendingEnvelopes;\n this._pendingEnvelopes = [];\n this._offlineQueue.push(...pending);\n // Do not flush yet — wait for the SNAPSHOT before sending local writes.\n };\n\n // Apply messages received from the server.\n // The server sends two kinds of typed messages:\n // { type: 'SNAPSHOT', data: string } — full state on first connect\n // { type: 'UPDATE', data: string } — a peer's envelope batch\n // For backward-compatibility with older relay versions that send raw\n // envelope arrays directly, the legacy format is still handled as a\n // fallback.\n ws.onmessage = (event) => {\n let parsed: unknown;\n try {\n parsed = JSON.parse(event.data);\n } catch {\n parsed = null;\n }\n\n // ── Typed protocol (new server) ──────────────────────────────────────\n if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) && 'type' in (parsed as object)) {\n const msg = parsed as { type: string; data: string };\n\n if (msg.type === 'SNAPSHOT') {\n // Hydrate local store with the server's consolidated state.\n let snapshotEnvelopes: unknown;\n try {\n snapshotEnvelopes = JSON.parse(msg.data);\n } catch {\n snapshotEnvelopes = [];\n }\n if (Array.isArray(snapshotEnvelopes)) {\n for (const env of snapshotEnvelopes) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n }\n // Allow outgoing writes now that we hold the server's state.\n this._snapshotReceived = true;\n this._proxy.notifyRemoteUpdate();\n this._flushOfflineQueue();\n return;\n }\n\n if (msg.type === 'UPDATE') {\n // Apply a peer's batch of envelopes.\n let updateEnvelopes: unknown;\n try {\n updateEnvelopes = JSON.parse(msg.data);\n } catch {\n updateEnvelopes = null;\n }\n if (Array.isArray(updateEnvelopes)) {\n for (const env of updateEnvelopes) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n } else {\n this._store.apply_envelope(msg.data);\n }\n this._proxy.notifyRemoteUpdate();\n return;\n }\n }\n\n // ── Legacy fallback (old relay format) ───────────────────────────────\n // Peers may send either a single envelope JSON string or a JSON array\n // of envelope strings (batch format).\n if (Array.isArray(parsed)) {\n for (const env of parsed) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n } else {\n this._store.apply_envelope(event.data);\n }\n // Notify UI listeners (e.g. React) that remote state has been applied.\n this._proxy.notifyRemoteUpdate();\n };\n\n // On close or error the subscription stays active so that writes made\n // while offline are buffered and flushed when the socket reconnects.\n ws.onclose = () => { /* keep buffering */ };\n ws.onerror = () => { /* keep buffering */ };\n }\n\n /**\n * Schedule a single batch flush for the current frame. Subsequent calls\n * before the flush fires are no-ops (only one flush is ever outstanding).\n *\n * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),\n * falling back to `setTimeout(fn, 0)` in non-browser environments.\n */\n private _scheduleBatchFlush(): void {\n if (this._cancelFlush !== null) return; // already scheduled\n\n const doFlush = () => {\n this._cancelFlush = null;\n this._flushBatch();\n };\n\n if (typeof requestAnimationFrame === 'function') {\n const id = requestAnimationFrame(doFlush);\n this._cancelFlush = () => cancelAnimationFrame(id);\n } else {\n const id = setTimeout(doFlush, 0);\n this._cancelFlush = () => clearTimeout(id);\n }\n }\n\n /**\n * Send all pending envelopes as a single JSON-array payload, or move them\n * to the offline queue if the socket is not currently open or the snapshot\n * has not yet been received.\n */\n private _flushBatch(): void {\n const envelopes = this._pendingEnvelopes.splice(0);\n if (envelopes.length === 0) return;\n\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */ && this._snapshotReceived) {\n ws.send(JSON.stringify(envelopes));\n } else {\n this._offlineQueue.push(...envelopes);\n }\n }\n\n /**\n * Send all offline-queued envelopes in a single batch.\n * Called after the server's SNAPSHOT has been applied so that buffered\n * local writes finally reach peers.\n */\n private _flushOfflineQueue(): void {\n const offline = this._offlineQueue.splice(0);\n if (offline.length === 0) return;\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */) {\n ws.send(JSON.stringify(offline));\n }\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * Unsubscribe from proxy updates, discard any buffered envelopes, and close\n * the WebSocket connection.\n *\n * Safe to call more than once.\n */\n disconnect(): void {\n this._unsubscribe?.();\n this._unsubscribe = null;\n this._cancelFlush?.();\n this._cancelFlush = null;\n this._pendingEnvelopes = [];\n this._offlineQueue = [];\n this._ws.close();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqEO,IAAM,iBAAN,MAAkF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWvF,YAAY,OAAuB;AATnC,SAAiB,YAAgC,oBAAI,IAAI;AACzD,SAAiB,kBAAmC,oBAAI,IAAI;AAS1D,SAAK,SAAS;AACd,SAAK,SAAS,KAAK,WAAW;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,IAAI,QAAW;AACb,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,SAAS,SAAoC;AAC3C,SAAK,UAAU,IAAI,OAAO;AAC1B,WAAO,MAAM;AACX,WAAK,UAAU,OAAO,OAAO;AAAA,IAC/B;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,SAAS,SAAiC;AACxC,SAAK,gBAAgB,IAAI,OAAO;AAChC,WAAO,MA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AAK,KAAK,UAAU,SAAS,CAAC;AAAA,IACnC,OAAO;AACL,WAAK,cAAc,KAAK,GAAG,SAAS;AAAA,IACtC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,qBAA2B;AACjC,UAAM,UAAU,KAAK,cAAc,OAAO,CAAC;AAC3C,QAAI,QAAQ,WAAW,EAAG;AAC1B,UAAM,KAAK,KAAK;AAChB,QAAI,GAAG,eAAe,GAAc;AAClC,SAAG,KAAK,KAAK,UAAU,OAAO,CAAC;AAAA,IACjC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,aAAmB;AACjB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,oBAAoB,CAAC;AAC1B,SAAK,gBAAgB,CAAC;AACtB,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -122,8 +122,14 @@ var WebSocketManager = class {
|
|
|
122
122
|
this._pendingEnvelopes = [];
|
|
123
123
|
/** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
|
|
124
124
|
this._cancelFlush = null;
|
|
125
|
-
/** Envelopes queued while the socket is not open
|
|
125
|
+
/** Envelopes queued while the socket is not open or snapshot has not yet arrived. */
|
|
126
126
|
this._offlineQueue = [];
|
|
127
|
+
/**
|
|
128
|
+
* Set to `true` once the server's initial `SNAPSHOT` message has been applied.
|
|
129
|
+
* Outgoing envelopes are held in `_offlineQueue` until this flag is set so that
|
|
130
|
+
* the local store is fully hydrated before the client's writes reach peers.
|
|
131
|
+
*/
|
|
132
|
+
this._snapshotReceived = false;
|
|
127
133
|
this._store = store;
|
|
128
134
|
this._proxy = proxy;
|
|
129
135
|
this._ws = ws;
|
|
@@ -137,16 +143,12 @@ var WebSocketManager = class {
|
|
|
137
143
|
this._scheduleBatchFlush();
|
|
138
144
|
});
|
|
139
145
|
ws.onopen = () => {
|
|
146
|
+
this._snapshotReceived = false;
|
|
140
147
|
this._cancelFlush?.();
|
|
141
148
|
this._cancelFlush = null;
|
|
142
|
-
const offline = this._offlineQueue;
|
|
143
149
|
const pending = this._pendingEnvelopes;
|
|
144
|
-
this._offlineQueue = [];
|
|
145
150
|
this._pendingEnvelopes = [];
|
|
146
|
-
|
|
147
|
-
if (batch.length > 0) {
|
|
148
|
-
ws.send(JSON.stringify(batch));
|
|
149
|
-
}
|
|
151
|
+
this._offlineQueue.push(...pending);
|
|
150
152
|
};
|
|
151
153
|
ws.onmessage = (event) => {
|
|
152
154
|
let parsed;
|
|
@@ -155,9 +157,52 @@ var WebSocketManager = class {
|
|
|
155
157
|
} catch {
|
|
156
158
|
parsed = null;
|
|
157
159
|
}
|
|
160
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) && "type" in parsed) {
|
|
161
|
+
const msg = parsed;
|
|
162
|
+
if (msg.type === "SNAPSHOT") {
|
|
163
|
+
let snapshotEnvelopes;
|
|
164
|
+
try {
|
|
165
|
+
snapshotEnvelopes = JSON.parse(msg.data);
|
|
166
|
+
} catch {
|
|
167
|
+
snapshotEnvelopes = [];
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(snapshotEnvelopes)) {
|
|
170
|
+
for (const env of snapshotEnvelopes) {
|
|
171
|
+
if (typeof env === "string") {
|
|
172
|
+
this._store.apply_envelope(env);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this._snapshotReceived = true;
|
|
177
|
+
this._proxy.notifyRemoteUpdate();
|
|
178
|
+
this._flushOfflineQueue();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (msg.type === "UPDATE") {
|
|
182
|
+
let updateEnvelopes;
|
|
183
|
+
try {
|
|
184
|
+
updateEnvelopes = JSON.parse(msg.data);
|
|
185
|
+
} catch {
|
|
186
|
+
updateEnvelopes = null;
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(updateEnvelopes)) {
|
|
189
|
+
for (const env of updateEnvelopes) {
|
|
190
|
+
if (typeof env === "string") {
|
|
191
|
+
this._store.apply_envelope(env);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
this._store.apply_envelope(msg.data);
|
|
196
|
+
}
|
|
197
|
+
this._proxy.notifyRemoteUpdate();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
158
201
|
if (Array.isArray(parsed)) {
|
|
159
202
|
for (const env of parsed) {
|
|
160
|
-
|
|
203
|
+
if (typeof env === "string") {
|
|
204
|
+
this._store.apply_envelope(env);
|
|
205
|
+
}
|
|
161
206
|
}
|
|
162
207
|
} else {
|
|
163
208
|
this._store.apply_envelope(event.data);
|
|
@@ -192,18 +237,32 @@ var WebSocketManager = class {
|
|
|
192
237
|
}
|
|
193
238
|
/**
|
|
194
239
|
* Send all pending envelopes as a single JSON-array payload, or move them
|
|
195
|
-
* to the offline queue if the socket is not currently open
|
|
240
|
+
* to the offline queue if the socket is not currently open or the snapshot
|
|
241
|
+
* has not yet been received.
|
|
196
242
|
*/
|
|
197
243
|
_flushBatch() {
|
|
198
244
|
const envelopes = this._pendingEnvelopes.splice(0);
|
|
199
245
|
if (envelopes.length === 0) return;
|
|
200
246
|
const ws = this._ws;
|
|
201
|
-
if (ws.readyState === 1) {
|
|
247
|
+
if (ws.readyState === 1 && this._snapshotReceived) {
|
|
202
248
|
ws.send(JSON.stringify(envelopes));
|
|
203
249
|
} else {
|
|
204
250
|
this._offlineQueue.push(...envelopes);
|
|
205
251
|
}
|
|
206
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Send all offline-queued envelopes in a single batch.
|
|
255
|
+
* Called after the server's SNAPSHOT has been applied so that buffered
|
|
256
|
+
* local writes finally reach peers.
|
|
257
|
+
*/
|
|
258
|
+
_flushOfflineQueue() {
|
|
259
|
+
const offline = this._offlineQueue.splice(0);
|
|
260
|
+
if (offline.length === 0) return;
|
|
261
|
+
const ws = this._ws;
|
|
262
|
+
if (ws.readyState === 1) {
|
|
263
|
+
ws.send(JSON.stringify(offline));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
207
266
|
// ── Public API ────────────────────────────────────────────────────────
|
|
208
267
|
/**
|
|
209
268
|
* Unsubscribe from proxy updates, discard any buffered envelopes, and close
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/CrdtStateProxy.ts","../src/index.ts","../src/WebSocketManager.ts"],"sourcesContent":["/**\n * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.\n *\n * In production, import the real `WasmStateStore` from the compiled Wasm\n * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).\n * In tests the interface can be satisfied by any mock object.\n */\nexport interface WasmStateStore {\n /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */\n set_register(key: string, value_json: string): string;\n /** Read the current value of a named LWW register as a JSON string, or `undefined`. */\n get_register(key: string): string | undefined;\n /** Apply a remote Envelope (serialised as JSON) to this store. */\n apply_envelope(envelope_json: string): void;\n}\n\n/**\n * Payload delivered to every `onUpdate` listener when a property is written\n * through the proxy.\n */\nexport interface UpdateEvent {\n /** The register key that was updated (e.g. `\"speed\"` or `\"robot.speed\"`). */\n key: string;\n /** The new JavaScript value. */\n value: unknown;\n /**\n * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised\n * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.\n */\n envelope: string;\n}\n\n/** Callback type for `onUpdate` listeners. */\nexport type UpdateHandler = (event: UpdateEvent) => void;\n\n/**\n * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend\n * developers a transparent, object-oriented experience.\n *\n * ## Reading state\n *\n * Access any registered key directly on `proxy.state`:\n *\n * ```ts\n * proxy.state.speed // returns the registered value, or undefined\n * proxy.state[\"robot.speed\"] // dot-path keys via bracket notation\n * ```\n *\n * ## Writing state\n *\n * Assign any value — primitives, objects, arrays:\n *\n * ```ts\n * proxy.state.speed = 100;\n * proxy.state.robot = { x: 10, y: 20 }; // store the whole object\n * proxy.state[\"robot.speed\"] = 100; // dot-path key\n * ```\n *\n * ## Listening for changes\n *\n * - `onUpdate(handler)` — fires on **local** writes with the full `UpdateEvent`\n * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.\n * - `onChange(handler)` — fires on **any** change: local writes _and_ remote\n * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.\n *\n * ```ts\n * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));\n * ```\n */\nexport class CrdtStateProxy {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = new Set();\n private readonly _changeHandlers: Set<() => void> = new Set();\n private readonly _state: Record<string, unknown>;\n\n /**\n * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.\n *\n * @param store - The Wasm state store instance to proxy.\n */\n constructor(store: WasmStateStore) {\n this._store = store;\n this._state = this._makeProxy();\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * The proxied state object.\n *\n * Reading a key returns the registered value, or `undefined` if it has\n * not been set yet. Assigning a value calls `WasmStateStore.set_register`\n * and fires all `onUpdate` and `onChange` listeners.\n */\n get state(): Record<string, unknown> {\n return this._state;\n }\n\n /**\n * Register a listener that fires whenever a value is written through\n * `proxy.state` (local writes only).\n *\n * The `UpdateEvent` carries the register key, new value, and the CRDT\n * envelope — forward the envelope to peers via `apply_envelope`.\n *\n * @returns An unsubscribe function.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n /**\n * Register a listener that fires whenever state changes — whether from a\n * local write or an incoming remote update (after `notifyRemoteUpdate`).\n *\n * Use this in UI frameworks to trigger re-renders:\n *\n * ```ts\n * proxy.onChange(() => setTick(t => t + 1));\n * ```\n *\n * @returns An unsubscribe function.\n */\n onChange(handler: () => void): () => void {\n this._changeHandlers.add(handler);\n return () => {\n this._changeHandlers.delete(handler);\n };\n }\n\n /**\n * Notify all `onChange` listeners that remote state has been applied to the\n * store. Called by `WebSocketManager` after every `apply_envelope` so that\n * UI frameworks re-render with the latest state.\n */\n notifyRemoteUpdate(): void {\n this._emitChange();\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Build the state `Proxy`.\n *\n * - **`get` trap**: reads the value from the WASM store via `get_register`.\n * Returns the parsed value, or `undefined` if the key has not been registered.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` and `onChange` listeners.\n *\n * Keys may be dot-separated paths (e.g. `\"robot.speed\"`) when using bracket\n * notation: `proxy.state[\"robot.speed\"] = 100`.\n */\n private _makeProxy(): Record<string, unknown> {\n return new Proxy({} as Record<string, unknown>, {\n get: (_target, prop) => {\n if (typeof prop === 'symbol') return undefined;\n const raw = this._store.get_register(String(prop));\n return raw !== undefined ? JSON.parse(raw) : undefined;\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = String(prop);\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n this._emitChange();\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\n }\n\n /** Notify all `onChange` handlers of a state change. */\n private _emitChange(): void {\n this._changeHandlers.forEach((handler) => handler());\n }\n}\n","export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';\nexport { default as initWasm, WasmStateStore } from '../pkg/web/crdt_sync.js';\nexport { WebSocketManager } from './WebSocketManager.js';\nexport type { WebSocketLike } from './WebSocketManager.js';\n","import { CrdtStateProxy } from './CrdtStateProxy.js';\nimport type { WasmStateStore } from './CrdtStateProxy.js';\n\n/**\n * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.\n *\n * The real browser `WebSocket` satisfies this interface out-of-the-box.\n * In tests, a plain mock object can be used instead.\n */\nexport interface WebSocketLike {\n /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */\n readonly readyState: number;\n /** Send a UTF-8 string frame to the server. */\n send(data: string): void;\n /** Initiate the closing handshake. */\n close(): void;\n /** Fired when a message frame is received. */\n onmessage: ((event: { data: string }) => void) | null;\n /** Fired when the connection is established. */\n onopen: ((event: unknown) => void) | null;\n /** Fired when the connection is closed. */\n onclose: ((event: unknown) => void) | null;\n /** Fired when an error occurs. */\n onerror: ((event: unknown) => void) | null;\n}\n\n/**\n * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT\n * operation produced locally is broadcast to peers, and every envelope\n * received from a peer is applied to the local store.\n *\n * ## Data flow\n *\n * ```\n * Local write\n * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)\n * → requestAnimationFrame / setTimeout schedules a batch flush\n * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame\n *\n * Incoming message (single envelope or JSON array of envelopes)\n * → WebSocket.onmessage\n * → WasmStateStore.apply_envelope() // merge into local store\n * ```\n *\n * ## Throttling / batching\n *\n * Multiple proxy writes that occur within the same JavaScript task (e.g. a\n * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single\n * JSON array payload on the next animation frame (browser) or the next\n * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps\n * network traffic proportional to frame rate rather than to the raw mutation\n * rate.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy, WebSocketManager } from './index.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/sync'));\n *\n * // Writes are automatically batched and broadcast to peers.\n * proxy.state.robot = { x: 10, y: 20 };\n *\n * // Clean up.\n * manager.disconnect();\n * ```\n */\nexport class WebSocketManager {\n private readonly _store: WasmStateStore;\n private readonly _proxy: CrdtStateProxy;\n private readonly _ws: WebSocketLike;\n private _unsubscribe: (() => void) | null = null;\n /** Envelopes collected in the current frame, waiting for the batch flush. */\n private _pendingEnvelopes: string[] = [];\n /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */\n private _cancelFlush: (() => void) | null = null;\n /** Envelopes queued while the socket is not open, flushed on reconnection. */\n private _offlineQueue: string[] = [];\n\n /**\n * Create a `WebSocketManager` and attach it to the given WebSocket.\n *\n * @param store - The Wasm state store. Incoming peer envelopes will be\n * applied to this store via `apply_envelope`.\n * @param proxy - The CRDT state proxy. Outgoing envelopes produced by\n * `set_register` calls will be read from the proxy's `onUpdate` events.\n * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).\n */\n constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike) {\n this._store = store;\n this._proxy = proxy;\n this._ws = ws;\n this._attach();\n }\n\n // ── Internal setup ────────────────────────────────────────────────────\n\n private _attach(): void {\n const ws = this._ws;\n\n // Collect envelopes and schedule a batch flush once per frame so that\n // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced\n // into a single network payload instead of one message per mutation.\n this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {\n this._pendingEnvelopes.push(envelope);\n this._scheduleBatchFlush();\n });\n\n // On (re)connection, immediately flush pending envelopes together with any\n // envelopes that were queued while the socket was offline.\n ws.onopen = () => {\n // Cancel any scheduled flush — we will drain everything right now.\n this._cancelFlush?.();\n this._cancelFlush = null;\n const offline = this._offlineQueue;\n const pending = this._pendingEnvelopes;\n this._offlineQueue = [];\n this._pendingEnvelopes = [];\n const batch = [...offline, ...pending];\n if (batch.length > 0) {\n ws.send(JSON.stringify(batch));\n }\n };\n\n // Apply envelopes received from peers. Peers may send either a single\n // envelope JSON string or a JSON array of envelope strings (batch format).\n ws.onmessage = (event) => {\n let parsed: unknown;\n try {\n parsed = JSON.parse(event.data);\n } catch {\n parsed = null;\n }\n if (Array.isArray(parsed)) {\n for (const env of parsed) {\n this._store.apply_envelope(env as string);\n }\n } else {\n // Fallback: treat the raw frame data as a single envelope string.\n this._store.apply_envelope(event.data);\n }\n // Notify UI listeners (e.g. React) that remote state has been applied.\n this._proxy.notifyRemoteUpdate();\n };\n\n // On close or error the subscription stays active so that writes made\n // while offline are buffered and flushed when the socket reconnects.\n ws.onclose = () => { /* keep buffering */ };\n ws.onerror = () => { /* keep buffering */ };\n }\n\n /**\n * Schedule a single batch flush for the current frame. Subsequent calls\n * before the flush fires are no-ops (only one flush is ever outstanding).\n *\n * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),\n * falling back to `setTimeout(fn, 0)` in non-browser environments.\n */\n private _scheduleBatchFlush(): void {\n if (this._cancelFlush !== null) return; // already scheduled\n\n const doFlush = () => {\n this._cancelFlush = null;\n this._flushBatch();\n };\n\n if (typeof requestAnimationFrame === 'function') {\n const id = requestAnimationFrame(doFlush);\n this._cancelFlush = () => cancelAnimationFrame(id);\n } else {\n const id = setTimeout(doFlush, 0);\n this._cancelFlush = () => clearTimeout(id);\n }\n }\n\n /**\n * Send all pending envelopes as a single JSON-array payload, or move them\n * to the offline queue if the socket is not currently open.\n */\n private _flushBatch(): void {\n const envelopes = this._pendingEnvelopes.splice(0);\n if (envelopes.length === 0) return;\n\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */) {\n ws.send(JSON.stringify(envelopes));\n } else {\n this._offlineQueue.push(...envelopes);\n }\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * Unsubscribe from proxy updates, discard any buffered envelopes, and close\n * the WebSocket connection.\n *\n * Safe to call more than once.\n */\n disconnect(): void {\n this._unsubscribe?.();\n this._unsubscribe = null;\n this._cancelFlush?.();\n this._cancelFlush = null;\n this._pendingEnvelopes = [];\n this._offlineQueue = [];\n this._ws.close();\n }\n}\n"],"mappings":";AAqEO,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW1B,YAAY,OAAuB;AATnC,SAAiB,YAA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":["default"]}
|
|
1
|
+
{"version":3,"sources":["../src/CrdtStateProxy.ts","../src/index.ts","../src/WebSocketManager.ts"],"sourcesContent":["/**\n * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.\n *\n * In production, import the real `WasmStateStore` from the compiled Wasm\n * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).\n * In tests the interface can be satisfied by any mock object.\n */\nexport interface WasmStateStore {\n /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */\n set_register(key: string, value_json: string): string;\n /** Read the current value of a named LWW register as a JSON string, or `undefined`. */\n get_register(key: string): string | undefined;\n /** Apply a remote Envelope (serialised as JSON) to this store. */\n apply_envelope(envelope_json: string): void;\n}\n\n/**\n * Payload delivered to every `onUpdate` listener when a property is written\n * through the proxy.\n */\nexport interface UpdateEvent {\n /** The register key that was updated (e.g. `\"speed\"` or `\"robot.speed\"`). */\n key: string;\n /** The new JavaScript value. */\n value: unknown;\n /**\n * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised\n * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.\n */\n envelope: string;\n}\n\n/** Callback type for `onUpdate` listeners. */\nexport type UpdateHandler = (event: UpdateEvent) => void;\n\n/**\n * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend\n * developers a transparent, object-oriented experience.\n *\n * ## Reading state\n *\n * Access any registered key directly on `proxy.state`:\n *\n * ```ts\n * proxy.state.speed // returns the registered value, or undefined\n * proxy.state[\"robot.speed\"] // dot-path keys via bracket notation\n * ```\n *\n * ## Writing state\n *\n * Assign any value — primitives, objects, arrays:\n *\n * ```ts\n * proxy.state.speed = 100;\n * proxy.state.robot = { x: 10, y: 20 }; // store the whole object\n * proxy.state[\"robot.speed\"] = 100; // dot-path key\n * ```\n *\n * ## Listening for changes\n *\n * - `onUpdate(handler)` — fires on **local** writes with the full `UpdateEvent`\n * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.\n * - `onChange(handler)` — fires on **any** change: local writes _and_ remote\n * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.\n *\n * ```ts\n * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));\n * ```\n */\nexport class CrdtStateProxy<T extends Record<string, unknown> = Record<string, unknown>> {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = new Set();\n private readonly _changeHandlers: Set<() => void> = new Set();\n private readonly _state: T;\n\n /**\n * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.\n *\n * @param store - The Wasm state store instance to proxy.\n */\n constructor(store: WasmStateStore) {\n this._store = store;\n this._state = this._makeProxy();\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * The proxied state object.\n *\n * Reading a key returns the registered value, or `undefined` if it has\n * not been set yet. Assigning a value calls `WasmStateStore.set_register`\n * and fires all `onUpdate` and `onChange` listeners.\n */\n get state(): T {\n return this._state;\n }\n\n /**\n * Register a listener that fires whenever a value is written through\n * `proxy.state` (local writes only).\n *\n * The `UpdateEvent` carries the register key, new value, and the CRDT\n * envelope — forward the envelope to peers via `apply_envelope`.\n *\n * @returns An unsubscribe function.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n /**\n * Register a listener that fires whenever state changes — whether from a\n * local write or an incoming remote update (after `notifyRemoteUpdate`).\n *\n * Use this in UI frameworks to trigger re-renders:\n *\n * ```ts\n * proxy.onChange(() => setTick(t => t + 1));\n * ```\n *\n * @returns An unsubscribe function.\n */\n onChange(handler: () => void): () => void {\n this._changeHandlers.add(handler);\n return () => {\n this._changeHandlers.delete(handler);\n };\n }\n\n /**\n * Notify all `onChange` listeners that remote state has been applied to the\n * store. Called by `WebSocketManager` after every `apply_envelope` so that\n * UI frameworks re-render with the latest state.\n */\n notifyRemoteUpdate(): void {\n this._emitChange();\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Build the state `Proxy`.\n *\n * - **`get` trap**: reads the value from the WASM store via `get_register`.\n * Returns the parsed value, or `undefined` if the key has not been registered.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` and `onChange` listeners.\n *\n * Keys may be dot-separated paths (e.g. `\"robot.speed\"`) when using bracket\n * notation: `proxy.state[\"robot.speed\"] = 100`.\n */\n private _makeProxy(): T {\n return new Proxy({} as T, {\n get: (_target, prop) => {\n if (typeof prop === 'symbol') return undefined;\n const raw = this._store.get_register(String(prop));\n return raw !== undefined ? JSON.parse(raw) : undefined;\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = String(prop);\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n this._emitChange();\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\n }\n\n /** Notify all `onChange` handlers of a state change. */\n private _emitChange(): void {\n this._changeHandlers.forEach((handler) => handler());\n }\n}\n","export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';\nexport { default as initWasm, WasmStateStore } from '../pkg/web/crdt_sync.js';\nexport { WebSocketManager } from './WebSocketManager.js';\nexport type { WebSocketLike } from './WebSocketManager.js';\n","import { CrdtStateProxy } from './CrdtStateProxy.js';\nimport type { WasmStateStore } from './CrdtStateProxy.js';\n\n/**\n * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.\n *\n * The real browser `WebSocket` satisfies this interface out-of-the-box.\n * In tests, a plain mock object can be used instead.\n */\nexport interface WebSocketLike {\n /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */\n readonly readyState: number;\n /** Send a UTF-8 string frame to the server. */\n send(data: string): void;\n /** Initiate the closing handshake. */\n close(): void;\n /** Fired when a message frame is received. */\n onmessage: ((event: { data: string }) => void) | null;\n /** Fired when the connection is established. */\n onopen: ((event: unknown) => void) | null;\n /** Fired when the connection is closed. */\n onclose: ((event: unknown) => void) | null;\n /** Fired when an error occurs. */\n onerror: ((event: unknown) => void) | null;\n}\n\n/**\n * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT\n * operation produced locally is broadcast to peers, and every envelope\n * received from a peer is applied to the local store.\n *\n * ## Data flow\n *\n * ```\n * Local write\n * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)\n * → requestAnimationFrame / setTimeout schedules a batch flush\n * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame\n *\n * Incoming SNAPSHOT (first message from server after connection opens)\n * → WebSocket.onmessage { type: 'SNAPSHOT', data: envelopes_json }\n * → WasmStateStore.apply_envelope() for each envelope in the snapshot\n * → offline queue flushed so buffered local writes reach the server\n *\n * Incoming UPDATE (peer delta from server)\n * → WebSocket.onmessage { type: 'UPDATE', data: batch_json }\n * → WasmStateStore.apply_envelope() for each envelope in the batch\n * ```\n *\n * ## Snapshot wait\n *\n * On every (re)connection, the manager buffers outgoing envelopes until the\n * server's `SNAPSHOT` message is received. This ensures the local store is\n * hydrated with the server's consolidated state before the client's own writes\n * are broadcast, preventing stale overwrites.\n *\n * ## Throttling / batching\n *\n * Multiple proxy writes that occur within the same JavaScript task (e.g. a\n * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single\n * JSON array payload on the next animation frame (browser) or the next\n * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps\n * network traffic proportional to frame rate rather than to the raw mutation\n * rate.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy, WebSocketManager } from './index.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/rooms/robot-42'));\n *\n * // Writes are automatically batched and broadcast to peers.\n * proxy.state.robot = { x: 10, y: 20 };\n *\n * // Clean up.\n * manager.disconnect();\n * ```\n */\nexport class WebSocketManager {\n private readonly _store: WasmStateStore;\n private readonly _proxy: CrdtStateProxy;\n private readonly _ws: WebSocketLike;\n private _unsubscribe: (() => void) | null = null;\n /** Envelopes collected in the current frame, waiting for the batch flush. */\n private _pendingEnvelopes: string[] = [];\n /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */\n private _cancelFlush: (() => void) | null = null;\n /** Envelopes queued while the socket is not open or snapshot has not yet arrived. */\n private _offlineQueue: string[] = [];\n /**\n * Set to `true` once the server's initial `SNAPSHOT` message has been applied.\n * Outgoing envelopes are held in `_offlineQueue` until this flag is set so that\n * the local store is fully hydrated before the client's writes reach peers.\n */\n private _snapshotReceived: boolean = false;\n\n /**\n * Create a `WebSocketManager` and attach it to the given WebSocket.\n *\n * @param store - The Wasm state store. Incoming peer envelopes will be\n * applied to this store via `apply_envelope`.\n * @param proxy - The CRDT state proxy. Outgoing envelopes produced by\n * `set_register` calls will be read from the proxy's `onUpdate` events.\n * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).\n */\n constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike) {\n this._store = store;\n this._proxy = proxy;\n this._ws = ws;\n this._attach();\n }\n\n // ── Internal setup ────────────────────────────────────────────────────\n\n private _attach(): void {\n const ws = this._ws;\n\n // Collect envelopes and schedule a batch flush once per frame so that\n // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced\n // into a single network payload instead of one message per mutation.\n this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {\n this._pendingEnvelopes.push(envelope);\n this._scheduleBatchFlush();\n });\n\n // On (re)connection, cancel any pending batch flush and move everything\n // to the offline queue. Outgoing writes are held there until the server's\n // SNAPSHOT message arrives and the local store has been hydrated.\n ws.onopen = () => {\n this._snapshotReceived = false;\n this._cancelFlush?.();\n this._cancelFlush = null;\n const pending = this._pendingEnvelopes;\n this._pendingEnvelopes = [];\n this._offlineQueue.push(...pending);\n // Do not flush yet — wait for the SNAPSHOT before sending local writes.\n };\n\n // Apply messages received from the server.\n // The server sends two kinds of typed messages:\n // { type: 'SNAPSHOT', data: string } — full state on first connect\n // { type: 'UPDATE', data: string } — a peer's envelope batch\n // For backward-compatibility with older relay versions that send raw\n // envelope arrays directly, the legacy format is still handled as a\n // fallback.\n ws.onmessage = (event) => {\n let parsed: unknown;\n try {\n parsed = JSON.parse(event.data);\n } catch {\n parsed = null;\n }\n\n // ── Typed protocol (new server) ──────────────────────────────────────\n if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) && 'type' in (parsed as object)) {\n const msg = parsed as { type: string; data: string };\n\n if (msg.type === 'SNAPSHOT') {\n // Hydrate local store with the server's consolidated state.\n let snapshotEnvelopes: unknown;\n try {\n snapshotEnvelopes = JSON.parse(msg.data);\n } catch {\n snapshotEnvelopes = [];\n }\n if (Array.isArray(snapshotEnvelopes)) {\n for (const env of snapshotEnvelopes) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n }\n // Allow outgoing writes now that we hold the server's state.\n this._snapshotReceived = true;\n this._proxy.notifyRemoteUpdate();\n this._flushOfflineQueue();\n return;\n }\n\n if (msg.type === 'UPDATE') {\n // Apply a peer's batch of envelopes.\n let updateEnvelopes: unknown;\n try {\n updateEnvelopes = JSON.parse(msg.data);\n } catch {\n updateEnvelopes = null;\n }\n if (Array.isArray(updateEnvelopes)) {\n for (const env of updateEnvelopes) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n } else {\n this._store.apply_envelope(msg.data);\n }\n this._proxy.notifyRemoteUpdate();\n return;\n }\n }\n\n // ── Legacy fallback (old relay format) ───────────────────────────────\n // Peers may send either a single envelope JSON string or a JSON array\n // of envelope strings (batch format).\n if (Array.isArray(parsed)) {\n for (const env of parsed) {\n if (typeof env === 'string') {\n this._store.apply_envelope(env);\n }\n }\n } else {\n this._store.apply_envelope(event.data);\n }\n // Notify UI listeners (e.g. React) that remote state has been applied.\n this._proxy.notifyRemoteUpdate();\n };\n\n // On close or error the subscription stays active so that writes made\n // while offline are buffered and flushed when the socket reconnects.\n ws.onclose = () => { /* keep buffering */ };\n ws.onerror = () => { /* keep buffering */ };\n }\n\n /**\n * Schedule a single batch flush for the current frame. Subsequent calls\n * before the flush fires are no-ops (only one flush is ever outstanding).\n *\n * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),\n * falling back to `setTimeout(fn, 0)` in non-browser environments.\n */\n private _scheduleBatchFlush(): void {\n if (this._cancelFlush !== null) return; // already scheduled\n\n const doFlush = () => {\n this._cancelFlush = null;\n this._flushBatch();\n };\n\n if (typeof requestAnimationFrame === 'function') {\n const id = requestAnimationFrame(doFlush);\n this._cancelFlush = () => cancelAnimationFrame(id);\n } else {\n const id = setTimeout(doFlush, 0);\n this._cancelFlush = () => clearTimeout(id);\n }\n }\n\n /**\n * Send all pending envelopes as a single JSON-array payload, or move them\n * to the offline queue if the socket is not currently open or the snapshot\n * has not yet been received.\n */\n private _flushBatch(): void {\n const envelopes = this._pendingEnvelopes.splice(0);\n if (envelopes.length === 0) return;\n\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */ && this._snapshotReceived) {\n ws.send(JSON.stringify(envelopes));\n } else {\n this._offlineQueue.push(...envelopes);\n }\n }\n\n /**\n * Send all offline-queued envelopes in a single batch.\n * Called after the server's SNAPSHOT has been applied so that buffered\n * local writes finally reach peers.\n */\n private _flushOfflineQueue(): void {\n const offline = this._offlineQueue.splice(0);\n if (offline.length === 0) return;\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */) {\n ws.send(JSON.stringify(offline));\n }\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * Unsubscribe from proxy updates, discard any buffered envelopes, and close\n * the WebSocket connection.\n *\n * Safe to call more than once.\n */\n disconnect(): void {\n this._unsubscribe?.();\n this._unsubscribe = null;\n this._cancelFlush?.();\n this._cancelFlush = null;\n this._pendingEnvelopes = [];\n this._offlineQueue = [];\n this._ws.close();\n }\n}\n"],"mappings":";AAqEO,IAA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names":["default"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crdt-sync/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "TypeScript proxy wrapper for the crdt-sync Wasm StateStore",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"build:wasm:bundler": "wasm-pack build ../.. --target bundler --out-dir packages/core/pkg/bundler",
|
|
29
29
|
"build:wasm:web": "wasm-pack build ../.. --target web --out-dir packages/core/pkg/web",
|
|
30
30
|
"build:wasm": "npm run build:wasm:bundler && npm run build:wasm:web && rm -f pkg/bundler/.gitignore pkg/web/.gitignore",
|
|
31
|
+
"prepublishOnly": "npm run build:wasm && npm run build",
|
|
31
32
|
"test": "jest",
|
|
32
33
|
"lint": "tsc --noEmit"
|
|
33
34
|
},
|