@crdt-sync/core 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +233 -0
- package/dist/index.d.ts +233 -5
- package/dist/index.js +220 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +194 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +12 -3
- package/pkg/bundler/README.md +346 -0
- package/pkg/bundler/crdt_sync.d.ts +95 -0
- package/pkg/bundler/crdt_sync.js +9 -0
- package/pkg/bundler/crdt_sync_bg.js +430 -0
- package/pkg/bundler/crdt_sync_bg.wasm +0 -0
- package/pkg/bundler/crdt_sync_bg.wasm.d.ts +25 -0
- package/pkg/bundler/package.json +30 -0
- package/pkg/web/README.md +346 -0
- package/pkg/web/crdt_sync.d.ts +145 -0
- package/pkg/web/crdt_sync.js +529 -0
- package/pkg/web/crdt_sync_bg.wasm +0 -0
- package/pkg/web/crdt_sync_bg.wasm.d.ts +25 -0
- package/pkg/web/package.json +28 -0
- package/dist/CrdtStateProxy.d.ts +0 -119
- package/dist/CrdtStateProxy.d.ts.map +0 -1
- package/dist/CrdtStateProxy.js +0 -120
- package/dist/CrdtStateProxy.js.map +0 -1
- package/dist/WebSocketManager.d.ts +0 -115
- package/dist/WebSocketManager.d.ts.map +0 -1
- package/dist/WebSocketManager.js +0 -179
- package/dist/WebSocketManager.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yDAAqD;AAA5C,mHAAA,cAAc,OAAA;AAEvB,6DAAyD;AAAhD,uHAAA,gBAAgB,OAAA"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/CrdtStateProxy.ts","../src/WebSocketManager.ts"],"sourcesContent":["export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { WasmStateStore, UpdateEvent, UpdateHandler } from './CrdtStateProxy.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 /** Dot-separated key path that was updated (e.g. `\"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 **magical, object-oriented** experience.\n *\n * ## How it works\n *\n * 1. **JS `Proxy` interception** – accessing a nested path on `state` returns\n * another `Proxy`. Assigning a value anywhere in the tree intercepts the\n * write and forwards it to the underlying Wasm store via\n * `set_register(dotPath, JSON.stringify(value))`.\n *\n * 2. **Wasm call** – the interceptor immediately calls\n * `WasmStateStore.set_register()` so the CRDT operation is recorded and\n * returns an `Envelope` JSON string ready for broadcasting.\n *\n * 3. **Event emitter** – every write fires all `onUpdate` listeners with the\n * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other\n * UI frameworks to react to state changes.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy } from './CrdtStateProxy.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n *\n * // Register a listener (e.g. trigger a React re-render).\n * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {\n * console.log(`${key} =`, value);\n * broadcast(envelope); // send to peers\n * });\n *\n * // Write through the proxy — the interceptor handles everything.\n * proxy.state.speed = 100;\n * proxy.state.robot.x = 42;\n *\n * // Clean up when done.\n * unsubscribe();\n * ```\n */\nexport class CrdtStateProxy {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = 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 * Assigning any property (or nested property) on this object will\n * automatically call `WasmStateStore.set_register` and fire `onUpdate`\n * listeners.\n *\n * ```ts\n * proxy.state.speed = 100; // key: \"speed\"\n * proxy.state.robot.speed = 100; // key: \"robot.speed\"\n * ```\n */\n get state(): Record<string, unknown> {\n return this._state;\n }\n\n /**\n * Register a listener that is called whenever a property is written through\n * `proxy.state`.\n *\n * @param handler - Callback receiving an `UpdateEvent`.\n * @returns An unsubscribe function — call it to remove the listener.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Recursively build a `Proxy` for the given dot-path `prefix`.\n *\n * - **`get` trap**: returns a child proxy for the nested path so that deep\n * assignments like `proxy.state.robot.speed = 100` work correctly.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` listeners.\n */\n private _makeProxy(prefix: string): Record<string, unknown> {\n const children: Record<string, Record<string, unknown>> = {};\n\n return new Proxy({} as Record<string, unknown>, {\n get: (_target, prop: string) => {\n const key = prefix ? `${prefix}.${prop}` : prop;\n if (!(prop in children)) {\n children[prop] = this._makeProxy(key);\n }\n return children[prop];\n },\n\n set: (_target, prop: string, value: unknown) => {\n const key = prefix ? `${prefix}.${prop}` : prop;\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all registered handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\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 };\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;;;AC8EO,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU1B,YAAY,OAAuB;AARnC,SAAiB,YAAgC,oBAAI,IAAI;AASvD,SAAK,SAAS;AACd,SAAK,SAAS,KAAK,WAAW,EAAE;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,IAAI,QAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,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,EAYQ,WAAW,QAAyC;AAC1D,UAAM,WAAoD,CAAC;AAE3D,WAAO,IAAI,MAAM,CAAC,GAA8B;AAAA,MAC9C,KAAK,CAAC,SAAS,SAAiB;AAC9B,cAAM,MAAM,SAAS,GAAG,MAAM,IAAI,IAAI,KAAK;AAC3C,YAAI,EAAE,QAAQ,WAAW;AACvB,mBAAS,IAAI,IAAI,KAAK,WAAW,GAAG;AAAA,QACtC;AACA,eAAO,SA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SAAK,gBAAgB,CAAC;AACtB,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// src/CrdtStateProxy.ts
|
|
2
|
+
var CrdtStateProxy = class {
|
|
3
|
+
/**
|
|
4
|
+
* Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
|
|
5
|
+
*
|
|
6
|
+
* @param store - The Wasm state store instance to proxy.
|
|
7
|
+
*/
|
|
8
|
+
constructor(store) {
|
|
9
|
+
this._handlers = /* @__PURE__ */ new Set();
|
|
10
|
+
this._store = store;
|
|
11
|
+
this._state = this._makeProxy("");
|
|
12
|
+
}
|
|
13
|
+
// ── Public API ────────────────────────────────────────────────────────
|
|
14
|
+
/**
|
|
15
|
+
* The proxied state object.
|
|
16
|
+
*
|
|
17
|
+
* Assigning any property (or nested property) on this object will
|
|
18
|
+
* automatically call `WasmStateStore.set_register` and fire `onUpdate`
|
|
19
|
+
* listeners.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* proxy.state.speed = 100; // key: "speed"
|
|
23
|
+
* proxy.state.robot.speed = 100; // key: "robot.speed"
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
get state() {
|
|
27
|
+
return this._state;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Register a listener that is called whenever a property is written through
|
|
31
|
+
* `proxy.state`.
|
|
32
|
+
*
|
|
33
|
+
* @param handler - Callback receiving an `UpdateEvent`.
|
|
34
|
+
* @returns An unsubscribe function — call it to remove the listener.
|
|
35
|
+
*/
|
|
36
|
+
onUpdate(handler) {
|
|
37
|
+
this._handlers.add(handler);
|
|
38
|
+
return () => {
|
|
39
|
+
this._handlers.delete(handler);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// ── Internal helpers ──────────────────────────────────────────────────
|
|
43
|
+
/**
|
|
44
|
+
* Recursively build a `Proxy` for the given dot-path `prefix`.
|
|
45
|
+
*
|
|
46
|
+
* - **`get` trap**: returns a child proxy for the nested path so that deep
|
|
47
|
+
* assignments like `proxy.state.robot.speed = 100` work correctly.
|
|
48
|
+
* - **`set` trap**: serialises the value, calls `set_register`, and fires
|
|
49
|
+
* all `onUpdate` listeners.
|
|
50
|
+
*/
|
|
51
|
+
_makeProxy(prefix) {
|
|
52
|
+
const children = {};
|
|
53
|
+
return new Proxy({}, {
|
|
54
|
+
get: (_target, prop) => {
|
|
55
|
+
const key = prefix ? `${prefix}.${prop}` : prop;
|
|
56
|
+
if (!(prop in children)) {
|
|
57
|
+
children[prop] = this._makeProxy(key);
|
|
58
|
+
}
|
|
59
|
+
return children[prop];
|
|
60
|
+
},
|
|
61
|
+
set: (_target, prop, value) => {
|
|
62
|
+
const key = prefix ? `${prefix}.${prop}` : prop;
|
|
63
|
+
const envelope = this._store.set_register(key, JSON.stringify(value));
|
|
64
|
+
this._emit({ key, value, envelope });
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** Dispatch an `UpdateEvent` to all registered handlers. */
|
|
70
|
+
_emit(event) {
|
|
71
|
+
this._handlers.forEach((handler) => handler(event));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/WebSocketManager.ts
|
|
76
|
+
var WebSocketManager = class {
|
|
77
|
+
/**
|
|
78
|
+
* Create a `WebSocketManager` and attach it to the given WebSocket.
|
|
79
|
+
*
|
|
80
|
+
* @param store - The Wasm state store. Incoming peer envelopes will be
|
|
81
|
+
* applied to this store via `apply_envelope`.
|
|
82
|
+
* @param proxy - The CRDT state proxy. Outgoing envelopes produced by
|
|
83
|
+
* `set_register` calls will be read from the proxy's `onUpdate` events.
|
|
84
|
+
* @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).
|
|
85
|
+
*/
|
|
86
|
+
constructor(store, proxy, ws) {
|
|
87
|
+
this._unsubscribe = null;
|
|
88
|
+
/** Envelopes collected in the current frame, waiting for the batch flush. */
|
|
89
|
+
this._pendingEnvelopes = [];
|
|
90
|
+
/** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */
|
|
91
|
+
this._cancelFlush = null;
|
|
92
|
+
/** Envelopes queued while the socket is not open, flushed on reconnection. */
|
|
93
|
+
this._offlineQueue = [];
|
|
94
|
+
this._store = store;
|
|
95
|
+
this._proxy = proxy;
|
|
96
|
+
this._ws = ws;
|
|
97
|
+
this._attach();
|
|
98
|
+
}
|
|
99
|
+
// ── Internal setup ────────────────────────────────────────────────────
|
|
100
|
+
_attach() {
|
|
101
|
+
const ws = this._ws;
|
|
102
|
+
this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {
|
|
103
|
+
this._pendingEnvelopes.push(envelope);
|
|
104
|
+
this._scheduleBatchFlush();
|
|
105
|
+
});
|
|
106
|
+
ws.onopen = () => {
|
|
107
|
+
this._cancelFlush?.();
|
|
108
|
+
this._cancelFlush = null;
|
|
109
|
+
const offline = this._offlineQueue;
|
|
110
|
+
const pending = this._pendingEnvelopes;
|
|
111
|
+
this._offlineQueue = [];
|
|
112
|
+
this._pendingEnvelopes = [];
|
|
113
|
+
const batch = [...offline, ...pending];
|
|
114
|
+
if (batch.length > 0) {
|
|
115
|
+
ws.send(JSON.stringify(batch));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
ws.onmessage = (event) => {
|
|
119
|
+
let parsed;
|
|
120
|
+
try {
|
|
121
|
+
parsed = JSON.parse(event.data);
|
|
122
|
+
} catch {
|
|
123
|
+
parsed = null;
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(parsed)) {
|
|
126
|
+
for (const env of parsed) {
|
|
127
|
+
this._store.apply_envelope(env);
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
this._store.apply_envelope(event.data);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
ws.onclose = () => {
|
|
134
|
+
};
|
|
135
|
+
ws.onerror = () => {
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Schedule a single batch flush for the current frame. Subsequent calls
|
|
140
|
+
* before the flush fires are no-ops (only one flush is ever outstanding).
|
|
141
|
+
*
|
|
142
|
+
* Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),
|
|
143
|
+
* falling back to `setTimeout(fn, 0)` in non-browser environments.
|
|
144
|
+
*/
|
|
145
|
+
_scheduleBatchFlush() {
|
|
146
|
+
if (this._cancelFlush !== null) return;
|
|
147
|
+
const doFlush = () => {
|
|
148
|
+
this._cancelFlush = null;
|
|
149
|
+
this._flushBatch();
|
|
150
|
+
};
|
|
151
|
+
if (typeof requestAnimationFrame === "function") {
|
|
152
|
+
const id = requestAnimationFrame(doFlush);
|
|
153
|
+
this._cancelFlush = () => cancelAnimationFrame(id);
|
|
154
|
+
} else {
|
|
155
|
+
const id = setTimeout(doFlush, 0);
|
|
156
|
+
this._cancelFlush = () => clearTimeout(id);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Send all pending envelopes as a single JSON-array payload, or move them
|
|
161
|
+
* to the offline queue if the socket is not currently open.
|
|
162
|
+
*/
|
|
163
|
+
_flushBatch() {
|
|
164
|
+
const envelopes = this._pendingEnvelopes.splice(0);
|
|
165
|
+
if (envelopes.length === 0) return;
|
|
166
|
+
const ws = this._ws;
|
|
167
|
+
if (ws.readyState === 1) {
|
|
168
|
+
ws.send(JSON.stringify(envelopes));
|
|
169
|
+
} else {
|
|
170
|
+
this._offlineQueue.push(...envelopes);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// ── Public API ────────────────────────────────────────────────────────
|
|
174
|
+
/**
|
|
175
|
+
* Unsubscribe from proxy updates, discard any buffered envelopes, and close
|
|
176
|
+
* the WebSocket connection.
|
|
177
|
+
*
|
|
178
|
+
* Safe to call more than once.
|
|
179
|
+
*/
|
|
180
|
+
disconnect() {
|
|
181
|
+
this._unsubscribe?.();
|
|
182
|
+
this._unsubscribe = null;
|
|
183
|
+
this._cancelFlush?.();
|
|
184
|
+
this._cancelFlush = null;
|
|
185
|
+
this._pendingEnvelopes = [];
|
|
186
|
+
this._offlineQueue = [];
|
|
187
|
+
this._ws.close();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
export {
|
|
191
|
+
CrdtStateProxy,
|
|
192
|
+
WebSocketManager
|
|
193
|
+
};
|
|
194
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/CrdtStateProxy.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 /** Dot-separated key path that was updated (e.g. `\"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 **magical, object-oriented** experience.\n *\n * ## How it works\n *\n * 1. **JS `Proxy` interception** – accessing a nested path on `state` returns\n * another `Proxy`. Assigning a value anywhere in the tree intercepts the\n * write and forwards it to the underlying Wasm store via\n * `set_register(dotPath, JSON.stringify(value))`.\n *\n * 2. **Wasm call** – the interceptor immediately calls\n * `WasmStateStore.set_register()` so the CRDT operation is recorded and\n * returns an `Envelope` JSON string ready for broadcasting.\n *\n * 3. **Event emitter** – every write fires all `onUpdate` listeners with the\n * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other\n * UI frameworks to react to state changes.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy } from './CrdtStateProxy.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n *\n * // Register a listener (e.g. trigger a React re-render).\n * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {\n * console.log(`${key} =`, value);\n * broadcast(envelope); // send to peers\n * });\n *\n * // Write through the proxy — the interceptor handles everything.\n * proxy.state.speed = 100;\n * proxy.state.robot.x = 42;\n *\n * // Clean up when done.\n * unsubscribe();\n * ```\n */\nexport class CrdtStateProxy {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = 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 * Assigning any property (or nested property) on this object will\n * automatically call `WasmStateStore.set_register` and fire `onUpdate`\n * listeners.\n *\n * ```ts\n * proxy.state.speed = 100; // key: \"speed\"\n * proxy.state.robot.speed = 100; // key: \"robot.speed\"\n * ```\n */\n get state(): Record<string, unknown> {\n return this._state;\n }\n\n /**\n * Register a listener that is called whenever a property is written through\n * `proxy.state`.\n *\n * @param handler - Callback receiving an `UpdateEvent`.\n * @returns An unsubscribe function — call it to remove the listener.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Recursively build a `Proxy` for the given dot-path `prefix`.\n *\n * - **`get` trap**: returns a child proxy for the nested path so that deep\n * assignments like `proxy.state.robot.speed = 100` work correctly.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` listeners.\n */\n private _makeProxy(prefix: string): Record<string, unknown> {\n const children: Record<string, Record<string, unknown>> = {};\n\n return new Proxy({} as Record<string, unknown>, {\n get: (_target, prop: string) => {\n const key = prefix ? `${prefix}.${prop}` : prop;\n if (!(prop in children)) {\n children[prop] = this._makeProxy(key);\n }\n return children[prop];\n },\n\n set: (_target, prop: string, value: unknown) => {\n const key = prefix ? `${prefix}.${prop}` : prop;\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all registered handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\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 };\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":";AA8EO,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU1B,YAAY,OAAuB;AARnC,SAAiB,YAAgC,oBAAI,IAAI;AASvD,SAAK,SAAS;AACd,SAAK,SAAS,KAAK,WAAW,EAAE;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,IAAI,QAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,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,EAYQ,WAAW,QAAyC;AAC1D,UAAM,WAAoD,CAAC;AAE3D,WAAO,IAA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K,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/package.json
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crdt-sync/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "TypeScript proxy wrapper for the crdt-sync Wasm StateStore",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
7
15
|
"files": [
|
|
8
16
|
"dist",
|
|
9
17
|
"pkg"
|
|
10
18
|
],
|
|
11
19
|
"scripts": {
|
|
12
|
-
"build": "
|
|
20
|
+
"build": "tsup",
|
|
13
21
|
"build:wasm:bundler": "wasm-pack build ../.. --target bundler --out-dir packages/core/pkg/bundler",
|
|
14
22
|
"build:wasm:web": "wasm-pack build ../.. --target web --out-dir packages/core/pkg/web",
|
|
15
|
-
"build:wasm": "npm run build:wasm:bundler && npm run build:wasm:web",
|
|
23
|
+
"build:wasm": "npm run build:wasm:bundler && npm run build:wasm:web && rm -f pkg/bundler/.gitignore pkg/web/.gitignore",
|
|
16
24
|
"test": "jest",
|
|
17
25
|
"lint": "tsc --noEmit"
|
|
18
26
|
},
|
|
@@ -21,6 +29,7 @@
|
|
|
21
29
|
"@types/jest": "^29.5.12",
|
|
22
30
|
"jest": "^29.7.0",
|
|
23
31
|
"ts-jest": "^29.2.4",
|
|
32
|
+
"tsup": "^8.0.0",
|
|
24
33
|
"typescript": "^5.4.5"
|
|
25
34
|
},
|
|
26
35
|
"jest": {
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
# CRDT-sync
|
|
2
|
+
|
|
3
|
+
[](https://crates.io/crates/crdt-sync)
|
|
4
|
+
[](https://docs.rs/crdt-sync)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A **generic state synchronization engine** built in Rust, based on
|
|
8
|
+
**CRDTs (Conflict-free Replicated Data Types)**.
|
|
9
|
+
|
|
10
|
+
Designed to keep a UI (digital twin) and a backend (AI/robotics logic) in
|
|
11
|
+
**perfect harmony** — even when multiple agents write concurrently or the
|
|
12
|
+
network drops for a moment. No locks, no merge conflicts, no data loss.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
| CRDT | Type | Use case |
|
|
19
|
+
|------|------|----------|
|
|
20
|
+
| [`LWWRegister`] | Last-Writer-Wins Register | Scalar key-value properties (`robot.x = 10`) |
|
|
21
|
+
| [`ORSet`] | Observed-Remove Set | Collections of unique items |
|
|
22
|
+
| [`RGA`] | Replicated Growable Array | Ordered sequences / lists |
|
|
23
|
+
| [`StateStore`] | Composite sync engine | Hosts all CRDTs under one roof with Lamport clocks and network `Envelope`s |
|
|
24
|
+
| [`StateProxy`] | State observation proxy | Intercepts field mutations and auto-queues CRDT operations (no manual `Envelope` handling) |
|
|
25
|
+
| [`crdt_state!`] | Typed state proxy macro | Generates a typed proxy struct from a field declaration; each field gets `set_<field>` / `get_<field>` methods with compile-time type safety |
|
|
26
|
+
|
|
27
|
+
### Logical Clocks
|
|
28
|
+
|
|
29
|
+
Physical wall-clock time (NTP) is unreliable for distributed synchronization.
|
|
30
|
+
This library provides two implementations that track causal ordering without
|
|
31
|
+
relying on system time:
|
|
32
|
+
|
|
33
|
+
| Clock | Module | Use case |
|
|
34
|
+
|-------|--------|----------|
|
|
35
|
+
| [`LamportClock`] | `lamport_clock` | Scalar logical clock; used internally by `StateStore` for total-order timestamps |
|
|
36
|
+
| [`VectorClock`] | `vector_clock` | Per-node vector clock; detects **concurrent** events in addition to causal ordering |
|
|
37
|
+
|
|
38
|
+
All CRDTs are **operation-based (CmRDT)** and satisfy:
|
|
39
|
+
- **Commutativity** – apply operations in any order, always converge.
|
|
40
|
+
- **Idempotency** – replaying an operation is safe.
|
|
41
|
+
- **Causal buffering** (RGA) – out-of-order delivery is handled automatically.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
Add to your `Cargo.toml`:
|
|
48
|
+
|
|
49
|
+
```toml
|
|
50
|
+
[dependencies]
|
|
51
|
+
crdt-sync = "0.1"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Quick Start
|
|
57
|
+
|
|
58
|
+
### LWW-Register (Last-Writer-Wins)
|
|
59
|
+
|
|
60
|
+
```rust
|
|
61
|
+
use crdt_sync::LWWRegister;
|
|
62
|
+
|
|
63
|
+
let mut node_a: LWWRegister<f64> = LWWRegister::new("node-A");
|
|
64
|
+
let mut node_b: LWWRegister<f64> = LWWRegister::new("node-B");
|
|
65
|
+
|
|
66
|
+
// node-A writes robot.x
|
|
67
|
+
let op = node_a.set_and_apply(10.0, 1);
|
|
68
|
+
|
|
69
|
+
// Broadcast op to node-B
|
|
70
|
+
node_b.apply(op);
|
|
71
|
+
|
|
72
|
+
assert_eq!(node_b.get(), Some(&10.0));
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### OR-Set (Observed-Remove Set)
|
|
76
|
+
|
|
77
|
+
```rust
|
|
78
|
+
use crdt_sync::ORSet;
|
|
79
|
+
|
|
80
|
+
let mut node_a: ORSet<String> = ORSet::new("node-A");
|
|
81
|
+
let mut node_b: ORSet<String> = ORSet::new("node-B");
|
|
82
|
+
|
|
83
|
+
// Add a robot to the fleet
|
|
84
|
+
let op = node_a.add("robot-1".to_string());
|
|
85
|
+
node_a.apply(op.clone());
|
|
86
|
+
node_b.apply(op);
|
|
87
|
+
|
|
88
|
+
assert!(node_b.contains(&"robot-1".to_string()));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### RGA (Replicated Growable Array)
|
|
92
|
+
|
|
93
|
+
```rust
|
|
94
|
+
use crdt_sync::RGA;
|
|
95
|
+
|
|
96
|
+
let mut node_a: RGA<char> = RGA::new("node-A");
|
|
97
|
+
let mut node_b: RGA<char> = RGA::new("node-B");
|
|
98
|
+
|
|
99
|
+
// node-A builds a sequence
|
|
100
|
+
let op1 = node_a.insert(0, 'H');
|
|
101
|
+
let op2 = node_a.insert(1, 'i');
|
|
102
|
+
|
|
103
|
+
// node-B receives operations in reverse order — handled automatically
|
|
104
|
+
node_b.apply(op2);
|
|
105
|
+
node_b.apply(op1);
|
|
106
|
+
|
|
107
|
+
assert_eq!(node_a.to_vec(), node_b.to_vec()); // ['H', 'i']
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### StateStore (Composite Sync Engine)
|
|
111
|
+
|
|
112
|
+
The `StateStore` is the recommended high-level API. It:
|
|
113
|
+
- Manages named LWW registers, OR-Sets and RGAs in one place.
|
|
114
|
+
- Assigns **Lamport timestamps** automatically (via the built-in `LamportClock`).
|
|
115
|
+
- Produces **`Envelope`** messages ready to send over a network channel.
|
|
116
|
+
|
|
117
|
+
```rust
|
|
118
|
+
use crdt_sync::StateStore;
|
|
119
|
+
|
|
120
|
+
let mut node_a = StateStore::new("node-A");
|
|
121
|
+
let mut node_b = StateStore::new("node-B");
|
|
122
|
+
|
|
123
|
+
// Write a scalar property
|
|
124
|
+
let env = node_a.set_register("robot.x", 42.0_f64);
|
|
125
|
+
node_b.apply_envelope(env);
|
|
126
|
+
assert_eq!(node_b.get_register::<f64>("robot.x"), Some(42.0));
|
|
127
|
+
|
|
128
|
+
// Add to a set
|
|
129
|
+
let env = node_a.set_add("fleet", "unit-1");
|
|
130
|
+
node_b.apply_envelope(env);
|
|
131
|
+
assert!(node_b.set_contains("fleet", &"unit-1"));
|
|
132
|
+
|
|
133
|
+
// Append to a sequence
|
|
134
|
+
let env1 = node_a.seq_insert("log", 0, "boot");
|
|
135
|
+
let env2 = node_a.seq_insert("log", 1, "ready");
|
|
136
|
+
node_b.apply_envelope(env2);
|
|
137
|
+
node_b.apply_envelope(env1); // out-of-order — still converges
|
|
138
|
+
assert_eq!(
|
|
139
|
+
node_a.seq_items::<String>("log"),
|
|
140
|
+
node_b.seq_items::<String>("log"),
|
|
141
|
+
);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### LamportClock
|
|
145
|
+
|
|
146
|
+
```rust
|
|
147
|
+
use crdt_sync::LamportClock;
|
|
148
|
+
|
|
149
|
+
let mut node_a = LamportClock::new();
|
|
150
|
+
let mut node_b = LamportClock::new();
|
|
151
|
+
|
|
152
|
+
// node_a produces and sends an event
|
|
153
|
+
let ts = node_a.tick(); // ts = 1
|
|
154
|
+
|
|
155
|
+
// node_b receives it, then produces its own event
|
|
156
|
+
node_b.update(ts); // b advances to max(0, 1) + 1 = 2
|
|
157
|
+
let ts_b = node_b.tick(); // ts_b = 3
|
|
158
|
+
|
|
159
|
+
assert!(ts_b > ts);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### VectorClock
|
|
163
|
+
|
|
164
|
+
```rust
|
|
165
|
+
use crdt_sync::VectorClock;
|
|
166
|
+
|
|
167
|
+
let mut a = VectorClock::new("A");
|
|
168
|
+
let mut b = VectorClock::new("B");
|
|
169
|
+
|
|
170
|
+
let v_a = a.increment(); // A sends {A:1}
|
|
171
|
+
let v_b = b.increment(); // B sends {B:1} – concurrent with v_a
|
|
172
|
+
|
|
173
|
+
// Neither causally precedes the other
|
|
174
|
+
assert!(v_a.concurrent_with(&v_b));
|
|
175
|
+
|
|
176
|
+
// B receives A's event
|
|
177
|
+
b.update(&v_a);
|
|
178
|
+
let v_b2 = b.increment(); // {A:1, B:2} – causally after v_a
|
|
179
|
+
|
|
180
|
+
assert!(v_a.happened_before(&v_b2));
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
### StateProxy (State Observation)
|
|
186
|
+
|
|
187
|
+
`StateProxy` is the Rust equivalent of JavaScript Proxy-based state observation.
|
|
188
|
+
It wraps a `StateStore` and **intercepts every field mutation**, automatically
|
|
189
|
+
converting it into a CRDT operation and queuing it for broadcast. Developers
|
|
190
|
+
work with plain `set` / `get` calls and never touch `Envelope` values directly.
|
|
191
|
+
|
|
192
|
+
```rust
|
|
193
|
+
use crdt_sync::state_store::StateStore;
|
|
194
|
+
use crdt_sync::proxy::StateProxy;
|
|
195
|
+
|
|
196
|
+
let mut store_a = StateStore::new("node-A");
|
|
197
|
+
let mut store_b = StateStore::new("node-B");
|
|
198
|
+
|
|
199
|
+
// Use the proxy – no manual Envelope handling required.
|
|
200
|
+
let ops = {
|
|
201
|
+
let mut proxy = store_a.proxy(); // or StateProxy::new(&mut store_a)
|
|
202
|
+
proxy
|
|
203
|
+
.set("robot.x", 10.0_f64) // scalar field
|
|
204
|
+
.set("robot.y", 20.0_f64)
|
|
205
|
+
.set_add("fleet", "unit-1") // set field
|
|
206
|
+
.seq_push("log", "boot"); // sequence field
|
|
207
|
+
|
|
208
|
+
proxy.drain_pending() // collect queued ops for broadcast
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Broadcast to all peers.
|
|
212
|
+
for env in ops {
|
|
213
|
+
store_b.apply_envelope(env);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
assert_eq!(store_b.get_register::<f64>("robot.x"), Some(10.0));
|
|
217
|
+
assert!(store_b.set_contains("fleet", &"unit-1"));
|
|
218
|
+
assert_eq!(store_b.seq_items::<String>("log"), vec!["boot"]);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
### `crdt_state!` Macro (Typed State Proxy)
|
|
224
|
+
|
|
225
|
+
The `crdt_state!` macro generates a **typed proxy struct** from a plain struct-like
|
|
226
|
+
field declaration. Instead of using string keys (`proxy.set("x", 10.0)`), you get
|
|
227
|
+
compile-time-checked `set_x` / `get_x` methods for every declared field.
|
|
228
|
+
|
|
229
|
+
```rust
|
|
230
|
+
use crdt_sync::state_store::StateStore;
|
|
231
|
+
use crdt_sync::crdt_state;
|
|
232
|
+
|
|
233
|
+
// Declare a typed state proxy struct.
|
|
234
|
+
crdt_state! {
|
|
235
|
+
pub struct RobotState {
|
|
236
|
+
x: f64,
|
|
237
|
+
y: f64,
|
|
238
|
+
speed: f64,
|
|
239
|
+
name: String,
|
|
240
|
+
active: bool,
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
let mut store_a = StateStore::new("node-A");
|
|
245
|
+
let mut store_b = StateStore::new("node-B");
|
|
246
|
+
|
|
247
|
+
// Mutations are intercepted; no Envelope handling required.
|
|
248
|
+
let ops = {
|
|
249
|
+
let mut state = RobotState::new(&mut store_a);
|
|
250
|
+
state
|
|
251
|
+
.set_x(3.0)
|
|
252
|
+
.set_y(4.0)
|
|
253
|
+
.set_speed(5.0)
|
|
254
|
+
.set_name("unit-7".to_string())
|
|
255
|
+
.set_active(true);
|
|
256
|
+
|
|
257
|
+
state.drain_pending() // collect queued ops for broadcast
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// Broadcast to all peers.
|
|
261
|
+
for env in ops {
|
|
262
|
+
store_b.apply_envelope(env);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
assert_eq!(store_b.get_register::<f64>("x"), Some(3.0));
|
|
266
|
+
assert_eq!(store_b.get_register::<f64>("speed"), Some(5.0));
|
|
267
|
+
assert_eq!(store_b.get_register::<bool>("active"), Some(true));
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
The generated struct also exposes:
|
|
271
|
+
- `pending_count() -> usize` – number of ops queued for broadcast.
|
|
272
|
+
- `store() -> &StateStore` – read-only access to the underlying store.
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
```
|
|
277
|
+
crdt-sync
|
|
278
|
+
├── lamport_clock – Scalar Lamport logical clock (tick / update rules)
|
|
279
|
+
├── vector_clock – Per-node vector clock (happened-before / concurrent detection)
|
|
280
|
+
├── lww_register – LWW-Register CmRDT
|
|
281
|
+
├── or_set – OR-Set CmRDT
|
|
282
|
+
├── rga – RGA CmRDT (with causal buffering)
|
|
283
|
+
├── state_store – Composite sync engine (LamportClock + Envelope)
|
|
284
|
+
├── proxy – StateProxy: intercepts mutations, auto-queues CRDT ops
|
|
285
|
+
└── macros – crdt_state! macro: typed proxy structs from field declarations
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Each module is self-contained and can be used independently. The `StateStore`
|
|
289
|
+
type-erases values via `serde_json::Value` so heterogeneous data can be stored
|
|
290
|
+
without dynamic dispatch.
|
|
291
|
+
|
|
292
|
+
---
|
|
293
|
+
|
|
294
|
+
## TypeScript SDK & React Integration
|
|
295
|
+
|
|
296
|
+
`crdt-sync` provides a seamless TypeScript monorepo offering a framework-agnostic core (`@crdt-sync/core`) and framework-specific adapters (`@crdt-sync/react`), built on top of WebAssembly.
|
|
297
|
+
|
|
298
|
+
### Installation
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
npm install @crdt-sync/core @crdt-sync/react
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### React usage (`useCrdtState`)
|
|
305
|
+
|
|
306
|
+
The React hook magically handles Wasm initialization, WebSocket network sync, CRDT proxying, and React component re-renders:
|
|
307
|
+
|
|
308
|
+
```tsx
|
|
309
|
+
import { useCrdtState } from '@crdt-sync/react';
|
|
310
|
+
|
|
311
|
+
export function RobotDashboard() {
|
|
312
|
+
// Binds the CRDT Wasm engine and networking directly to React state
|
|
313
|
+
const { state, proxy, status } = useCrdtState('wss://api.example.com/sync', {
|
|
314
|
+
robot: { speed: 0, active: true }
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
if (status !== 'open') return <p>Connecting to sync engine...</p>;
|
|
318
|
+
|
|
319
|
+
return (
|
|
320
|
+
<div>
|
|
321
|
+
<h1>Speed: {state.robot.speed}</h1>
|
|
322
|
+
{/*
|
|
323
|
+
Direct mutation is intercepted, applied as a CRDT operation,
|
|
324
|
+
broadcast over WebSocket, and triggers a local React re-render.
|
|
325
|
+
*/}
|
|
326
|
+
<button onClick={() => proxy!.state.robot.speed += 10}>
|
|
327
|
+
Increase Speed
|
|
328
|
+
</button>
|
|
329
|
+
</div>
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
## Running Tests
|
|
337
|
+
|
|
338
|
+
```bash
|
|
339
|
+
cargo test
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## License
|
|
345
|
+
|
|
346
|
+
MIT
|