@crdt-sync/core 0.3.1 → 0.3.2
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 +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- 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).
|
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).
|
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;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B,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 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,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;AA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AEL,aAAK,OAAO,eAAe,MAAM,IAAI;AAAA,MACvC;AAEA,WAAK,OAAO,mBAAmB;AAAA,IACjC;AAIA,OAAG,UAAU,MAAM;AAAA,IAAuB;AAC1C,OAAG,UAAU,MAAM;AAAA,IAAuB;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,sBAA4B;AAClC,QAAI,KAAK,iBAAiB,KAAM;AAEhC,UAAM,UAAU,MAAM;AACpB,WAAK,eAAe;AACpB,WAAK,YAAY;AAAA,IACnB;AAEA,QAAI,OAAO,0BAA0B,YAAY;AAC/C,YAAM,KAAK,sBAAsB,OAAO;AACxC,WAAK,eAAe,MAAM,qBAAqB,EAAE;AAAA,IACnD,OAAO;AACL,YAAM,KAAK,WAAW,SAAS,CAAC;AAChC,WAAK,eAAe,MAAM,aAAa,EAAE;AAAA,IAC3C;AAAA,EACF;AAAA;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":[]}
|
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,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,W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;AAAA;AAAA;AAAA,EASQ,sBAA4B;AAClC,QAAI,KAAK,iBAAiB,KAAM;AAEhC,UAAM,UAAU,MAAM;AACpB,WAAK,eAAe;AACpB,WAAK,YAAY;AAAA,IACnB;AAEA,QAAI,OAAO,0BAA0B,YAAY;AAC/C,YAAM,KAAK,sBAAsB,OAAO;AACxC,WAAK,eAAe,MAAM,qBAAqB,EAAE;AAAA,IACnD,OAAO;AACL,YAAM,KAAK,WAAW,SAAS,CAAC;AAChC,WAAK,eAAe,MAAM,aAAa,EAAE;AAAA,IAC3C;AAAA,EACF;AAAA;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":["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 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,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,MAAM;AACX,WAAK,gBAAgB,OAAO,OAAO;AAAA,IACrC;AAAA,EACF;AAAA;AA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O;AACL,YAAM,KAAK,WAAW,SAAS,CAAC;AAChC,WAAK,eAAe,MAAM,aAAa,EAAE;AAAA,IAC3C;AAAA,EACF;AAAA;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":["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.2",
|
|
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
|
},
|
package/pkg/web/crdt_sync.d.ts
CHANGED
|
@@ -1,24 +1,145 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* A WebAssembly-compatible wrapper around [`StateStore`].
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
7
|
+
* Methods accept and return JSON-encoded strings at the Wasm boundary so that
|
|
8
|
+
* [`Envelope`] payloads and CRDT values can be exchanged between Rust and
|
|
9
|
+
* JavaScript without sharing memory structures directly.
|
|
8
10
|
*/
|
|
9
|
-
|
|
10
11
|
export class WasmStateStore {
|
|
12
|
+
free(): void;
|
|
13
|
+
[Symbol.dispose](): void;
|
|
14
|
+
/**
|
|
15
|
+
* Apply a remote [`Envelope`] (serialised as a JSON string) to this store.
|
|
16
|
+
*
|
|
17
|
+
* Throws a JavaScript error if the JSON cannot be deserialised.
|
|
18
|
+
*/
|
|
19
|
+
apply_envelope(envelope_json: string): void;
|
|
20
|
+
/**
|
|
21
|
+
* Return the current Lamport clock value.
|
|
22
|
+
*
|
|
23
|
+
* Returned as `f64` because JavaScript's `Number` type cannot safely
|
|
24
|
+
* represent all `u64` values. Values up to `2^53 − 1`
|
|
25
|
+
* (`Number.MAX_SAFE_INTEGER`) are represented exactly. For distributed
|
|
26
|
+
* systems that could conceivably tick the clock beyond that threshold,
|
|
27
|
+
* treat the returned value as approximate or use `BigInt` on the JS side.
|
|
28
|
+
*/
|
|
29
|
+
clock(): number;
|
|
30
|
+
/**
|
|
31
|
+
* Read the current value of a named LWW register as a JSON string.
|
|
32
|
+
*
|
|
33
|
+
* Returns `undefined` in JavaScript if the key has never been written.
|
|
34
|
+
*/
|
|
35
|
+
get_register(key: string): string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new `WasmStateStore` for the given node identifier.
|
|
38
|
+
*/
|
|
11
39
|
constructor(node_id: string);
|
|
40
|
+
/**
|
|
41
|
+
* Delete the element at visible `index` in the named RGA sequence.
|
|
42
|
+
*
|
|
43
|
+
* Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
|
|
44
|
+
* `index` is out of bounds.
|
|
45
|
+
*/
|
|
46
|
+
seq_delete(key: string, index: number): string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Insert a JSON-encoded element at `index` in the named RGA sequence.
|
|
49
|
+
*
|
|
50
|
+
* Returns the resulting [`Envelope`] as a JSON string.
|
|
51
|
+
*/
|
|
52
|
+
seq_insert(key: string, index: number, value_json: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Return all visible elements of the named sequence as a JSON array string.
|
|
55
|
+
*
|
|
56
|
+
* Throws a JavaScript error if serialisation fails.
|
|
57
|
+
*/
|
|
58
|
+
seq_items(key: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Return the number of visible elements in the named sequence.
|
|
61
|
+
*/
|
|
62
|
+
seq_len(key: string): number;
|
|
63
|
+
/**
|
|
64
|
+
* Add a JSON-encoded element to the named OR-Set.
|
|
65
|
+
*
|
|
66
|
+
* Returns the resulting [`Envelope`] as a JSON string.
|
|
67
|
+
*/
|
|
68
|
+
set_add(key: string, value_json: string): string;
|
|
69
|
+
/**
|
|
70
|
+
* Returns `true` if the named OR-Set contains the JSON-encoded `value`.
|
|
71
|
+
*/
|
|
72
|
+
set_contains(key: string, value_json: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Return all elements of the named OR-Set as a JSON array string.
|
|
75
|
+
*
|
|
76
|
+
* Throws a JavaScript error if serialisation fails.
|
|
77
|
+
*/
|
|
78
|
+
set_items(key: string): string;
|
|
79
|
+
/**
|
|
80
|
+
* Write a JSON-encoded value to the named LWW register.
|
|
81
|
+
*
|
|
82
|
+
* Returns the resulting [`Envelope`] serialised as a JSON string, ready
|
|
83
|
+
* to broadcast to peer nodes.
|
|
84
|
+
*/
|
|
12
85
|
set_register(key: string, value_json: string): string;
|
|
13
|
-
|
|
14
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Remove a JSON-encoded element from the named OR-Set.
|
|
88
|
+
*
|
|
89
|
+
* Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
|
|
90
|
+
* the element was not present in the set.
|
|
91
|
+
*
|
|
92
|
+
* Throws a JavaScript error if `value_json` is not valid JSON.
|
|
93
|
+
*/
|
|
94
|
+
set_remove(key: string, value_json: string): string | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
98
|
+
|
|
99
|
+
export interface InitOutput {
|
|
100
|
+
readonly memory: WebAssembly.Memory;
|
|
101
|
+
readonly __wbg_wasmstatestore_free: (a: number, b: number) => void;
|
|
102
|
+
readonly wasmstatestore_apply_envelope: (a: number, b: number, c: number) => [number, number];
|
|
103
|
+
readonly wasmstatestore_clock: (a: number) => number;
|
|
104
|
+
readonly wasmstatestore_get_register: (a: number, b: number, c: number) => [number, number];
|
|
105
|
+
readonly wasmstatestore_new: (a: number, b: number) => number;
|
|
106
|
+
readonly wasmstatestore_seq_delete: (a: number, b: number, c: number, d: number) => [number, number];
|
|
107
|
+
readonly wasmstatestore_seq_insert: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
|
|
108
|
+
readonly wasmstatestore_seq_items: (a: number, b: number, c: number) => [number, number, number, number];
|
|
109
|
+
readonly wasmstatestore_seq_len: (a: number, b: number, c: number) => number;
|
|
110
|
+
readonly wasmstatestore_set_add: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
111
|
+
readonly wasmstatestore_set_contains: (a: number, b: number, c: number, d: number, e: number) => number;
|
|
112
|
+
readonly wasmstatestore_set_items: (a: number, b: number, c: number) => [number, number, number, number];
|
|
113
|
+
readonly wasmstatestore_set_register: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
114
|
+
readonly wasmstatestore_set_remove: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
|
|
115
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
116
|
+
readonly __externref_table_alloc: () => number;
|
|
117
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
118
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
119
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
120
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
121
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
122
|
+
readonly __wbindgen_start: () => void;
|
|
15
123
|
}
|
|
16
124
|
|
|
17
|
-
export type
|
|
18
|
-
| RequestInfo
|
|
19
|
-
| URL
|
|
20
|
-
| Response
|
|
21
|
-
| BufferSource
|
|
22
|
-
| WebAssembly.Module;
|
|
125
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
23
126
|
|
|
24
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
129
|
+
* a precompiled `WebAssembly.Module`.
|
|
130
|
+
*
|
|
131
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
132
|
+
*
|
|
133
|
+
* @returns {InitOutput}
|
|
134
|
+
*/
|
|
135
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
139
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
140
|
+
*
|
|
141
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
142
|
+
*
|
|
143
|
+
* @returns {Promise<InitOutput>}
|
|
144
|
+
*/
|
|
145
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/pkg/web/crdt_sync.js
CHANGED
|
@@ -1,30 +1,529 @@
|
|
|
1
|
+
/* @ts-self-types="./crdt_sync.d.ts" */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* This file is a placeholder that allows the package to be imported in
|
|
5
|
-
* environments where the real compiled WebAssembly is not yet available
|
|
6
|
-
* (e.g. test environments that mock the module, or bundler/SSR setups).
|
|
4
|
+
* A WebAssembly-compatible wrapper around [`StateStore`].
|
|
7
5
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* Methods accept and return JSON-encoded strings at the Wasm boundary so that
|
|
7
|
+
* [`Envelope`] payloads and CRDT values can be exchanged between Rust and
|
|
8
|
+
* JavaScript without sharing memory structures directly.
|
|
10
9
|
*/
|
|
11
|
-
|
|
12
10
|
export class WasmStateStore {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
__destroy_into_raw() {
|
|
12
|
+
const ptr = this.__wbg_ptr;
|
|
13
|
+
this.__wbg_ptr = 0;
|
|
14
|
+
WasmStateStoreFinalization.unregister(this);
|
|
15
|
+
return ptr;
|
|
16
|
+
}
|
|
17
|
+
free() {
|
|
18
|
+
const ptr = this.__destroy_into_raw();
|
|
19
|
+
wasm.__wbg_wasmstatestore_free(ptr, 0);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Apply a remote [`Envelope`] (serialised as a JSON string) to this store.
|
|
23
|
+
*
|
|
24
|
+
* Throws a JavaScript error if the JSON cannot be deserialised.
|
|
25
|
+
* @param {string} envelope_json
|
|
26
|
+
*/
|
|
27
|
+
apply_envelope(envelope_json) {
|
|
28
|
+
const ptr0 = passStringToWasm0(envelope_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
29
|
+
const len0 = WASM_VECTOR_LEN;
|
|
30
|
+
const ret = wasm.wasmstatestore_apply_envelope(this.__wbg_ptr, ptr0, len0);
|
|
31
|
+
if (ret[1]) {
|
|
32
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return the current Lamport clock value.
|
|
37
|
+
*
|
|
38
|
+
* Returned as `f64` because JavaScript's `Number` type cannot safely
|
|
39
|
+
* represent all `u64` values. Values up to `2^53 − 1`
|
|
40
|
+
* (`Number.MAX_SAFE_INTEGER`) are represented exactly. For distributed
|
|
41
|
+
* systems that could conceivably tick the clock beyond that threshold,
|
|
42
|
+
* treat the returned value as approximate or use `BigInt` on the JS side.
|
|
43
|
+
* @returns {number}
|
|
44
|
+
*/
|
|
45
|
+
clock() {
|
|
46
|
+
const ret = wasm.wasmstatestore_clock(this.__wbg_ptr);
|
|
47
|
+
return ret;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Read the current value of a named LWW register as a JSON string.
|
|
51
|
+
*
|
|
52
|
+
* Returns `undefined` in JavaScript if the key has never been written.
|
|
53
|
+
* @param {string} key
|
|
54
|
+
* @returns {string | undefined}
|
|
55
|
+
*/
|
|
56
|
+
get_register(key) {
|
|
57
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
58
|
+
const len0 = WASM_VECTOR_LEN;
|
|
59
|
+
const ret = wasm.wasmstatestore_get_register(this.__wbg_ptr, ptr0, len0);
|
|
60
|
+
let v2;
|
|
61
|
+
if (ret[0] !== 0) {
|
|
62
|
+
v2 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
63
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
64
|
+
}
|
|
65
|
+
return v2;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create a new `WasmStateStore` for the given node identifier.
|
|
69
|
+
* @param {string} node_id
|
|
70
|
+
*/
|
|
71
|
+
constructor(node_id) {
|
|
72
|
+
const ptr0 = passStringToWasm0(node_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
73
|
+
const len0 = WASM_VECTOR_LEN;
|
|
74
|
+
const ret = wasm.wasmstatestore_new(ptr0, len0);
|
|
75
|
+
this.__wbg_ptr = ret >>> 0;
|
|
76
|
+
WasmStateStoreFinalization.register(this, this.__wbg_ptr, this);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Delete the element at visible `index` in the named RGA sequence.
|
|
81
|
+
*
|
|
82
|
+
* Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
|
|
83
|
+
* `index` is out of bounds.
|
|
84
|
+
* @param {string} key
|
|
85
|
+
* @param {number} index
|
|
86
|
+
* @returns {string | undefined}
|
|
87
|
+
*/
|
|
88
|
+
seq_delete(key, index) {
|
|
89
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
90
|
+
const len0 = WASM_VECTOR_LEN;
|
|
91
|
+
const ret = wasm.wasmstatestore_seq_delete(this.__wbg_ptr, ptr0, len0, index);
|
|
92
|
+
let v2;
|
|
93
|
+
if (ret[0] !== 0) {
|
|
94
|
+
v2 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
95
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
96
|
+
}
|
|
97
|
+
return v2;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Insert a JSON-encoded element at `index` in the named RGA sequence.
|
|
101
|
+
*
|
|
102
|
+
* Returns the resulting [`Envelope`] as a JSON string.
|
|
103
|
+
* @param {string} key
|
|
104
|
+
* @param {number} index
|
|
105
|
+
* @param {string} value_json
|
|
106
|
+
* @returns {string}
|
|
107
|
+
*/
|
|
108
|
+
seq_insert(key, index, value_json) {
|
|
109
|
+
let deferred4_0;
|
|
110
|
+
let deferred4_1;
|
|
111
|
+
try {
|
|
112
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
113
|
+
const len0 = WASM_VECTOR_LEN;
|
|
114
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
115
|
+
const len1 = WASM_VECTOR_LEN;
|
|
116
|
+
const ret = wasm.wasmstatestore_seq_insert(this.__wbg_ptr, ptr0, len0, index, ptr1, len1);
|
|
117
|
+
var ptr3 = ret[0];
|
|
118
|
+
var len3 = ret[1];
|
|
119
|
+
if (ret[3]) {
|
|
120
|
+
ptr3 = 0; len3 = 0;
|
|
121
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
122
|
+
}
|
|
123
|
+
deferred4_0 = ptr3;
|
|
124
|
+
deferred4_1 = len3;
|
|
125
|
+
return getStringFromWasm0(ptr3, len3);
|
|
126
|
+
} finally {
|
|
127
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Return all visible elements of the named sequence as a JSON array string.
|
|
132
|
+
*
|
|
133
|
+
* Throws a JavaScript error if serialisation fails.
|
|
134
|
+
* @param {string} key
|
|
135
|
+
* @returns {string}
|
|
136
|
+
*/
|
|
137
|
+
seq_items(key) {
|
|
138
|
+
let deferred3_0;
|
|
139
|
+
let deferred3_1;
|
|
140
|
+
try {
|
|
141
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
142
|
+
const len0 = WASM_VECTOR_LEN;
|
|
143
|
+
const ret = wasm.wasmstatestore_seq_items(this.__wbg_ptr, ptr0, len0);
|
|
144
|
+
var ptr2 = ret[0];
|
|
145
|
+
var len2 = ret[1];
|
|
146
|
+
if (ret[3]) {
|
|
147
|
+
ptr2 = 0; len2 = 0;
|
|
148
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
149
|
+
}
|
|
150
|
+
deferred3_0 = ptr2;
|
|
151
|
+
deferred3_1 = len2;
|
|
152
|
+
return getStringFromWasm0(ptr2, len2);
|
|
153
|
+
} finally {
|
|
154
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Return the number of visible elements in the named sequence.
|
|
159
|
+
* @param {string} key
|
|
160
|
+
* @returns {number}
|
|
161
|
+
*/
|
|
162
|
+
seq_len(key) {
|
|
163
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
164
|
+
const len0 = WASM_VECTOR_LEN;
|
|
165
|
+
const ret = wasm.wasmstatestore_seq_len(this.__wbg_ptr, ptr0, len0);
|
|
166
|
+
return ret >>> 0;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Add a JSON-encoded element to the named OR-Set.
|
|
170
|
+
*
|
|
171
|
+
* Returns the resulting [`Envelope`] as a JSON string.
|
|
172
|
+
* @param {string} key
|
|
173
|
+
* @param {string} value_json
|
|
174
|
+
* @returns {string}
|
|
175
|
+
*/
|
|
176
|
+
set_add(key, value_json) {
|
|
177
|
+
let deferred4_0;
|
|
178
|
+
let deferred4_1;
|
|
179
|
+
try {
|
|
180
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
181
|
+
const len0 = WASM_VECTOR_LEN;
|
|
182
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
183
|
+
const len1 = WASM_VECTOR_LEN;
|
|
184
|
+
const ret = wasm.wasmstatestore_set_add(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
185
|
+
var ptr3 = ret[0];
|
|
186
|
+
var len3 = ret[1];
|
|
187
|
+
if (ret[3]) {
|
|
188
|
+
ptr3 = 0; len3 = 0;
|
|
189
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
190
|
+
}
|
|
191
|
+
deferred4_0 = ptr3;
|
|
192
|
+
deferred4_1 = len3;
|
|
193
|
+
return getStringFromWasm0(ptr3, len3);
|
|
194
|
+
} finally {
|
|
195
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns `true` if the named OR-Set contains the JSON-encoded `value`.
|
|
200
|
+
* @param {string} key
|
|
201
|
+
* @param {string} value_json
|
|
202
|
+
* @returns {boolean}
|
|
203
|
+
*/
|
|
204
|
+
set_contains(key, value_json) {
|
|
205
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
206
|
+
const len0 = WASM_VECTOR_LEN;
|
|
207
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
208
|
+
const len1 = WASM_VECTOR_LEN;
|
|
209
|
+
const ret = wasm.wasmstatestore_set_contains(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
210
|
+
return ret !== 0;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Return all elements of the named OR-Set as a JSON array string.
|
|
214
|
+
*
|
|
215
|
+
* Throws a JavaScript error if serialisation fails.
|
|
216
|
+
* @param {string} key
|
|
217
|
+
* @returns {string}
|
|
218
|
+
*/
|
|
219
|
+
set_items(key) {
|
|
220
|
+
let deferred3_0;
|
|
221
|
+
let deferred3_1;
|
|
222
|
+
try {
|
|
223
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
224
|
+
const len0 = WASM_VECTOR_LEN;
|
|
225
|
+
const ret = wasm.wasmstatestore_set_items(this.__wbg_ptr, ptr0, len0);
|
|
226
|
+
var ptr2 = ret[0];
|
|
227
|
+
var len2 = ret[1];
|
|
228
|
+
if (ret[3]) {
|
|
229
|
+
ptr2 = 0; len2 = 0;
|
|
230
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
231
|
+
}
|
|
232
|
+
deferred3_0 = ptr2;
|
|
233
|
+
deferred3_1 = len2;
|
|
234
|
+
return getStringFromWasm0(ptr2, len2);
|
|
235
|
+
} finally {
|
|
236
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Write a JSON-encoded value to the named LWW register.
|
|
241
|
+
*
|
|
242
|
+
* Returns the resulting [`Envelope`] serialised as a JSON string, ready
|
|
243
|
+
* to broadcast to peer nodes.
|
|
244
|
+
* @param {string} key
|
|
245
|
+
* @param {string} value_json
|
|
246
|
+
* @returns {string}
|
|
247
|
+
*/
|
|
248
|
+
set_register(key, value_json) {
|
|
249
|
+
let deferred4_0;
|
|
250
|
+
let deferred4_1;
|
|
251
|
+
try {
|
|
252
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
253
|
+
const len0 = WASM_VECTOR_LEN;
|
|
254
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
255
|
+
const len1 = WASM_VECTOR_LEN;
|
|
256
|
+
const ret = wasm.wasmstatestore_set_register(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
257
|
+
var ptr3 = ret[0];
|
|
258
|
+
var len3 = ret[1];
|
|
259
|
+
if (ret[3]) {
|
|
260
|
+
ptr3 = 0; len3 = 0;
|
|
261
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
262
|
+
}
|
|
263
|
+
deferred4_0 = ptr3;
|
|
264
|
+
deferred4_1 = len3;
|
|
265
|
+
return getStringFromWasm0(ptr3, len3);
|
|
266
|
+
} finally {
|
|
267
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Remove a JSON-encoded element from the named OR-Set.
|
|
272
|
+
*
|
|
273
|
+
* Returns the resulting [`Envelope`] as a JSON string, or `undefined` if
|
|
274
|
+
* the element was not present in the set.
|
|
275
|
+
*
|
|
276
|
+
* Throws a JavaScript error if `value_json` is not valid JSON.
|
|
277
|
+
* @param {string} key
|
|
278
|
+
* @param {string} value_json
|
|
279
|
+
* @returns {string | undefined}
|
|
280
|
+
*/
|
|
281
|
+
set_remove(key, value_json) {
|
|
282
|
+
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
283
|
+
const len0 = WASM_VECTOR_LEN;
|
|
284
|
+
const ptr1 = passStringToWasm0(value_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
285
|
+
const len1 = WASM_VECTOR_LEN;
|
|
286
|
+
const ret = wasm.wasmstatestore_set_remove(this.__wbg_ptr, ptr0, len0, ptr1, len1);
|
|
287
|
+
if (ret[3]) {
|
|
288
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
289
|
+
}
|
|
290
|
+
let v3;
|
|
291
|
+
if (ret[0] !== 0) {
|
|
292
|
+
v3 = getStringFromWasm0(ret[0], ret[1]).slice();
|
|
293
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
294
|
+
}
|
|
295
|
+
return v3;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (Symbol.dispose) WasmStateStore.prototype[Symbol.dispose] = WasmStateStore.prototype.free;
|
|
299
|
+
|
|
300
|
+
function __wbg_get_imports() {
|
|
301
|
+
const import0 = {
|
|
302
|
+
__proto__: null,
|
|
303
|
+
__wbg___wbindgen_throw_df03e93053e0f4bc: function(arg0, arg1) {
|
|
304
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
305
|
+
},
|
|
306
|
+
__wbg_getRandomValues_3dda8830c2565714: function() { return handleError(function (arg0, arg1) {
|
|
307
|
+
globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
|
|
308
|
+
}, arguments); },
|
|
309
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
310
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
311
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
312
|
+
return ret;
|
|
313
|
+
},
|
|
314
|
+
__wbindgen_init_externref_table: function() {
|
|
315
|
+
const table = wasm.__wbindgen_externrefs;
|
|
316
|
+
const offset = table.grow(4);
|
|
317
|
+
table.set(0, undefined);
|
|
318
|
+
table.set(offset + 0, undefined);
|
|
319
|
+
table.set(offset + 1, null);
|
|
320
|
+
table.set(offset + 2, true);
|
|
321
|
+
table.set(offset + 3, false);
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
return {
|
|
325
|
+
__proto__: null,
|
|
326
|
+
"./crdt_sync_bg.js": import0,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const WasmStateStoreFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
331
|
+
? { register: () => {}, unregister: () => {} }
|
|
332
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_wasmstatestore_free(ptr >>> 0, 1));
|
|
333
|
+
|
|
334
|
+
function addToExternrefTable0(obj) {
|
|
335
|
+
const idx = wasm.__externref_table_alloc();
|
|
336
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
337
|
+
return idx;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
341
|
+
ptr = ptr >>> 0;
|
|
342
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function getStringFromWasm0(ptr, len) {
|
|
346
|
+
ptr = ptr >>> 0;
|
|
347
|
+
return decodeText(ptr, len);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let cachedUint8ArrayMemory0 = null;
|
|
351
|
+
function getUint8ArrayMemory0() {
|
|
352
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
353
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
18
354
|
}
|
|
355
|
+
return cachedUint8ArrayMemory0;
|
|
356
|
+
}
|
|
19
357
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
358
|
+
function handleError(f, args) {
|
|
359
|
+
try {
|
|
360
|
+
return f.apply(this, args);
|
|
361
|
+
} catch (e) {
|
|
362
|
+
const idx = addToExternrefTable0(e);
|
|
363
|
+
wasm.__wbindgen_exn_store(idx);
|
|
364
|
+
}
|
|
23
365
|
}
|
|
24
366
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
367
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
368
|
+
if (realloc === undefined) {
|
|
369
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
370
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
371
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
372
|
+
WASM_VECTOR_LEN = buf.length;
|
|
373
|
+
return ptr;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
let len = arg.length;
|
|
377
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
378
|
+
|
|
379
|
+
const mem = getUint8ArrayMemory0();
|
|
380
|
+
|
|
381
|
+
let offset = 0;
|
|
382
|
+
|
|
383
|
+
for (; offset < len; offset++) {
|
|
384
|
+
const code = arg.charCodeAt(offset);
|
|
385
|
+
if (code > 0x7F) break;
|
|
386
|
+
mem[ptr + offset] = code;
|
|
387
|
+
}
|
|
388
|
+
if (offset !== len) {
|
|
389
|
+
if (offset !== 0) {
|
|
390
|
+
arg = arg.slice(offset);
|
|
391
|
+
}
|
|
392
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
393
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
394
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
395
|
+
|
|
396
|
+
offset += ret.written;
|
|
397
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
WASM_VECTOR_LEN = offset;
|
|
401
|
+
return ptr;
|
|
30
402
|
}
|
|
403
|
+
|
|
404
|
+
function takeFromExternrefTable0(idx) {
|
|
405
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
406
|
+
wasm.__externref_table_dealloc(idx);
|
|
407
|
+
return value;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
411
|
+
cachedTextDecoder.decode();
|
|
412
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
413
|
+
let numBytesDecoded = 0;
|
|
414
|
+
function decodeText(ptr, len) {
|
|
415
|
+
numBytesDecoded += len;
|
|
416
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
417
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
418
|
+
cachedTextDecoder.decode();
|
|
419
|
+
numBytesDecoded = len;
|
|
420
|
+
}
|
|
421
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const cachedTextEncoder = new TextEncoder();
|
|
425
|
+
|
|
426
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
427
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
428
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
429
|
+
view.set(buf);
|
|
430
|
+
return {
|
|
431
|
+
read: arg.length,
|
|
432
|
+
written: buf.length
|
|
433
|
+
};
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
let WASM_VECTOR_LEN = 0;
|
|
438
|
+
|
|
439
|
+
let wasmModule, wasm;
|
|
440
|
+
function __wbg_finalize_init(instance, module) {
|
|
441
|
+
wasm = instance.exports;
|
|
442
|
+
wasmModule = module;
|
|
443
|
+
cachedUint8ArrayMemory0 = null;
|
|
444
|
+
wasm.__wbindgen_start();
|
|
445
|
+
return wasm;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
async function __wbg_load(module, imports) {
|
|
449
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
450
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
451
|
+
try {
|
|
452
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
453
|
+
} catch (e) {
|
|
454
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
455
|
+
|
|
456
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
457
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
458
|
+
|
|
459
|
+
} else { throw e; }
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const bytes = await module.arrayBuffer();
|
|
464
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
465
|
+
} else {
|
|
466
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
467
|
+
|
|
468
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
469
|
+
return { instance, module };
|
|
470
|
+
} else {
|
|
471
|
+
return instance;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function expectedResponseType(type) {
|
|
476
|
+
switch (type) {
|
|
477
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
478
|
+
}
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function initSync(module) {
|
|
484
|
+
if (wasm !== undefined) return wasm;
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
if (module !== undefined) {
|
|
488
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
489
|
+
({module} = module)
|
|
490
|
+
} else {
|
|
491
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const imports = __wbg_get_imports();
|
|
496
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
497
|
+
module = new WebAssembly.Module(module);
|
|
498
|
+
}
|
|
499
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
500
|
+
return __wbg_finalize_init(instance, module);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async function __wbg_init(module_or_path) {
|
|
504
|
+
if (wasm !== undefined) return wasm;
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
if (module_or_path !== undefined) {
|
|
508
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
509
|
+
({module_or_path} = module_or_path)
|
|
510
|
+
} else {
|
|
511
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (module_or_path === undefined) {
|
|
516
|
+
module_or_path = new URL('crdt_sync_bg.wasm', import.meta.url);
|
|
517
|
+
}
|
|
518
|
+
const imports = __wbg_get_imports();
|
|
519
|
+
|
|
520
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
521
|
+
module_or_path = fetch(module_or_path);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
525
|
+
|
|
526
|
+
return __wbg_finalize_init(instance, module);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export { initSync, __wbg_init as default };
|