@crdt-sync/core 0.2.2 → 0.3.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 CHANGED
@@ -1,3 +1,5 @@
1
+ export { WasmStateStore, default as initWasm } from '../pkg/web/crdt_sync.js';
2
+
1
3
  /**
2
4
  * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.
3
5
  *
@@ -18,7 +20,7 @@ interface WasmStateStore {
18
20
  * through the proxy.
19
21
  */
20
22
  interface UpdateEvent {
21
- /** Dot-separated key path that was updated (e.g. `"robot.speed"`). */
23
+ /** The register key that was updated (e.g. `"speed"` or `"robot.speed"`). */
22
24
  key: string;
23
25
  /** The new JavaScript value. */
24
26
  value: unknown;
@@ -32,50 +34,42 @@ interface UpdateEvent {
32
34
  type UpdateHandler = (event: UpdateEvent) => void;
33
35
  /**
34
36
  * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend
35
- * developers a **magical, object-oriented** experience.
37
+ * developers a transparent, object-oriented experience.
36
38
  *
37
- * ## How it works
39
+ * ## Reading state
38
40
  *
39
- * 1. **JS `Proxy` interception** accessing a nested path on `state` returns
40
- * another `Proxy`. Assigning a value anywhere in the tree intercepts the
41
- * write and forwards it to the underlying Wasm store via
42
- * `set_register(dotPath, JSON.stringify(value))`.
41
+ * Access any registered key directly on `proxy.state`:
43
42
  *
44
- * 2. **Wasm call** – the interceptor immediately calls
45
- * `WasmStateStore.set_register()` so the CRDT operation is recorded and
46
- * returns an `Envelope` JSON string ready for broadcasting.
43
+ * ```ts
44
+ * proxy.state.speed // returns the registered value, or undefined
45
+ * proxy.state["robot.speed"] // dot-path keys via bracket notation
46
+ * ```
47
47
  *
48
- * 3. **Event emitter** – every write fires all `onUpdate` listeners with the
49
- * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other
50
- * UI frameworks to react to state changes.
48
+ * ## Writing state
51
49
  *
52
- * ## Usage
50
+ * Assign any value — primitives, objects, arrays:
53
51
  *
54
52
  * ```ts
55
- * import init, { WasmStateStore } from './crdt_sync.js';
56
- * import { CrdtStateProxy } from './CrdtStateProxy.js';
57
- *
58
- * await init();
59
- * const store = new WasmStateStore('node-1');
60
- * const proxy = new CrdtStateProxy(store);
53
+ * proxy.state.speed = 100;
54
+ * proxy.state.robot = { x: 10, y: 20 }; // store the whole object
55
+ * proxy.state["robot.speed"] = 100; // dot-path key
56
+ * ```
61
57
  *
62
- * // Register a listener (e.g. trigger a React re-render).
63
- * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {
64
- * console.log(`${key} =`, value);
65
- * broadcast(envelope); // send to peers
66
- * });
58
+ * ## Listening for changes
67
59
  *
68
- * // Write through the proxy the interceptor handles everything.
69
- * proxy.state.speed = 100;
70
- * proxy.state.robot.x = 42;
60
+ * - `onUpdate(handler)` fires on **local** writes with the full `UpdateEvent`
61
+ * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.
62
+ * - `onChange(handler)` — fires on **any** change: local writes _and_ remote
63
+ * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.
71
64
  *
72
- * // Clean up when done.
73
- * unsubscribe();
65
+ * ```ts
66
+ * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));
74
67
  * ```
75
68
  */
76
69
  declare class CrdtStateProxy {
77
70
  private readonly _store;
78
71
  private readonly _handlers;
72
+ private readonly _changeHandlers;
79
73
  private readonly _state;
80
74
  /**
81
75
  * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
@@ -86,35 +80,56 @@ declare class CrdtStateProxy {
86
80
  /**
87
81
  * The proxied state object.
88
82
  *
89
- * Assigning any property (or nested property) on this object will
90
- * automatically call `WasmStateStore.set_register` and fire `onUpdate`
91
- * listeners.
92
- *
93
- * ```ts
94
- * proxy.state.speed = 100; // key: "speed"
95
- * proxy.state.robot.speed = 100; // key: "robot.speed"
96
- * ```
83
+ * Reading a key returns the registered value, or `undefined` if it has
84
+ * not been set yet. Assigning a value calls `WasmStateStore.set_register`
85
+ * and fires all `onUpdate` and `onChange` listeners.
97
86
  */
98
87
  get state(): Record<string, unknown>;
99
88
  /**
100
- * Register a listener that is called whenever a property is written through
101
- * `proxy.state`.
89
+ * Register a listener that fires whenever a value is written through
90
+ * `proxy.state` (local writes only).
102
91
  *
103
- * @param handler - Callback receiving an `UpdateEvent`.
104
- * @returns An unsubscribe function call it to remove the listener.
92
+ * The `UpdateEvent` carries the register key, new value, and the CRDT
93
+ * envelope forward the envelope to peers via `apply_envelope`.
94
+ *
95
+ * @returns An unsubscribe function.
105
96
  */
106
97
  onUpdate(handler: UpdateHandler): () => void;
107
98
  /**
108
- * Recursively build a `Proxy` for the given dot-path `prefix`.
99
+ * Register a listener that fires whenever state changes — whether from a
100
+ * local write or an incoming remote update (after `notifyRemoteUpdate`).
101
+ *
102
+ * Use this in UI frameworks to trigger re-renders:
103
+ *
104
+ * ```ts
105
+ * proxy.onChange(() => setTick(t => t + 1));
106
+ * ```
109
107
  *
110
- * - **`get` trap**: returns a child proxy for the nested path so that deep
111
- * assignments like `proxy.state.robot.speed = 100` work correctly.
108
+ * @returns An unsubscribe function.
109
+ */
110
+ onChange(handler: () => void): () => void;
111
+ /**
112
+ * Notify all `onChange` listeners that remote state has been applied to the
113
+ * store. Called by `WebSocketManager` after every `apply_envelope` so that
114
+ * UI frameworks re-render with the latest state.
115
+ */
116
+ notifyRemoteUpdate(): void;
117
+ /**
118
+ * Build the state `Proxy`.
119
+ *
120
+ * - **`get` trap**: reads the value from the WASM store via `get_register`.
121
+ * Returns the parsed value, or `undefined` if the key has not been registered.
112
122
  * - **`set` trap**: serialises the value, calls `set_register`, and fires
113
- * all `onUpdate` listeners.
123
+ * all `onUpdate` and `onChange` listeners.
124
+ *
125
+ * Keys may be dot-separated paths (e.g. `"robot.speed"`) when using bracket
126
+ * notation: `proxy.state["robot.speed"] = 100`.
114
127
  */
115
128
  private _makeProxy;
116
- /** Dispatch an `UpdateEvent` to all registered handlers. */
129
+ /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */
117
130
  private _emit;
131
+ /** Notify all `onChange` handlers of a state change. */
132
+ private _emitChange;
118
133
  }
119
134
 
120
135
  /**
@@ -230,4 +245,4 @@ declare class WebSocketManager {
230
245
  disconnect(): void;
231
246
  }
232
247
 
233
- export { CrdtStateProxy, type UpdateEvent, type UpdateHandler, type WasmStateStore, type WebSocketLike, WebSocketManager };
248
+ export { CrdtStateProxy, type UpdateEvent, type UpdateHandler, type WebSocketLike, WebSocketManager };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export { WasmStateStore, default as initWasm } from '../pkg/web/crdt_sync.js';
2
+
1
3
  /**
2
4
  * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.
3
5
  *
@@ -18,7 +20,7 @@ interface WasmStateStore {
18
20
  * through the proxy.
19
21
  */
20
22
  interface UpdateEvent {
21
- /** Dot-separated key path that was updated (e.g. `"robot.speed"`). */
23
+ /** The register key that was updated (e.g. `"speed"` or `"robot.speed"`). */
22
24
  key: string;
23
25
  /** The new JavaScript value. */
24
26
  value: unknown;
@@ -32,50 +34,42 @@ interface UpdateEvent {
32
34
  type UpdateHandler = (event: UpdateEvent) => void;
33
35
  /**
34
36
  * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend
35
- * developers a **magical, object-oriented** experience.
37
+ * developers a transparent, object-oriented experience.
36
38
  *
37
- * ## How it works
39
+ * ## Reading state
38
40
  *
39
- * 1. **JS `Proxy` interception** accessing a nested path on `state` returns
40
- * another `Proxy`. Assigning a value anywhere in the tree intercepts the
41
- * write and forwards it to the underlying Wasm store via
42
- * `set_register(dotPath, JSON.stringify(value))`.
41
+ * Access any registered key directly on `proxy.state`:
43
42
  *
44
- * 2. **Wasm call** – the interceptor immediately calls
45
- * `WasmStateStore.set_register()` so the CRDT operation is recorded and
46
- * returns an `Envelope` JSON string ready for broadcasting.
43
+ * ```ts
44
+ * proxy.state.speed // returns the registered value, or undefined
45
+ * proxy.state["robot.speed"] // dot-path keys via bracket notation
46
+ * ```
47
47
  *
48
- * 3. **Event emitter** – every write fires all `onUpdate` listeners with the
49
- * full `UpdateEvent` (key, value, envelope), enabling React / Vue and other
50
- * UI frameworks to react to state changes.
48
+ * ## Writing state
51
49
  *
52
- * ## Usage
50
+ * Assign any value — primitives, objects, arrays:
53
51
  *
54
52
  * ```ts
55
- * import init, { WasmStateStore } from './crdt_sync.js';
56
- * import { CrdtStateProxy } from './CrdtStateProxy.js';
57
- *
58
- * await init();
59
- * const store = new WasmStateStore('node-1');
60
- * const proxy = new CrdtStateProxy(store);
53
+ * proxy.state.speed = 100;
54
+ * proxy.state.robot = { x: 10, y: 20 }; // store the whole object
55
+ * proxy.state["robot.speed"] = 100; // dot-path key
56
+ * ```
61
57
  *
62
- * // Register a listener (e.g. trigger a React re-render).
63
- * const unsubscribe = proxy.onUpdate(({ key, value, envelope }) => {
64
- * console.log(`${key} =`, value);
65
- * broadcast(envelope); // send to peers
66
- * });
58
+ * ## Listening for changes
67
59
  *
68
- * // Write through the proxy the interceptor handles everything.
69
- * proxy.state.speed = 100;
70
- * proxy.state.robot.x = 42;
60
+ * - `onUpdate(handler)` fires on **local** writes with the full `UpdateEvent`
61
+ * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.
62
+ * - `onChange(handler)` — fires on **any** change: local writes _and_ remote
63
+ * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.
71
64
  *
72
- * // Clean up when done.
73
- * unsubscribe();
65
+ * ```ts
66
+ * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));
74
67
  * ```
75
68
  */
76
69
  declare class CrdtStateProxy {
77
70
  private readonly _store;
78
71
  private readonly _handlers;
72
+ private readonly _changeHandlers;
79
73
  private readonly _state;
80
74
  /**
81
75
  * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.
@@ -86,35 +80,56 @@ declare class CrdtStateProxy {
86
80
  /**
87
81
  * The proxied state object.
88
82
  *
89
- * Assigning any property (or nested property) on this object will
90
- * automatically call `WasmStateStore.set_register` and fire `onUpdate`
91
- * listeners.
92
- *
93
- * ```ts
94
- * proxy.state.speed = 100; // key: "speed"
95
- * proxy.state.robot.speed = 100; // key: "robot.speed"
96
- * ```
83
+ * Reading a key returns the registered value, or `undefined` if it has
84
+ * not been set yet. Assigning a value calls `WasmStateStore.set_register`
85
+ * and fires all `onUpdate` and `onChange` listeners.
97
86
  */
98
87
  get state(): Record<string, unknown>;
99
88
  /**
100
- * Register a listener that is called whenever a property is written through
101
- * `proxy.state`.
89
+ * Register a listener that fires whenever a value is written through
90
+ * `proxy.state` (local writes only).
102
91
  *
103
- * @param handler - Callback receiving an `UpdateEvent`.
104
- * @returns An unsubscribe function call it to remove the listener.
92
+ * The `UpdateEvent` carries the register key, new value, and the CRDT
93
+ * envelope forward the envelope to peers via `apply_envelope`.
94
+ *
95
+ * @returns An unsubscribe function.
105
96
  */
106
97
  onUpdate(handler: UpdateHandler): () => void;
107
98
  /**
108
- * Recursively build a `Proxy` for the given dot-path `prefix`.
99
+ * Register a listener that fires whenever state changes — whether from a
100
+ * local write or an incoming remote update (after `notifyRemoteUpdate`).
101
+ *
102
+ * Use this in UI frameworks to trigger re-renders:
103
+ *
104
+ * ```ts
105
+ * proxy.onChange(() => setTick(t => t + 1));
106
+ * ```
109
107
  *
110
- * - **`get` trap**: returns a child proxy for the nested path so that deep
111
- * assignments like `proxy.state.robot.speed = 100` work correctly.
108
+ * @returns An unsubscribe function.
109
+ */
110
+ onChange(handler: () => void): () => void;
111
+ /**
112
+ * Notify all `onChange` listeners that remote state has been applied to the
113
+ * store. Called by `WebSocketManager` after every `apply_envelope` so that
114
+ * UI frameworks re-render with the latest state.
115
+ */
116
+ notifyRemoteUpdate(): void;
117
+ /**
118
+ * Build the state `Proxy`.
119
+ *
120
+ * - **`get` trap**: reads the value from the WASM store via `get_register`.
121
+ * Returns the parsed value, or `undefined` if the key has not been registered.
112
122
  * - **`set` trap**: serialises the value, calls `set_register`, and fires
113
- * all `onUpdate` listeners.
123
+ * all `onUpdate` and `onChange` listeners.
124
+ *
125
+ * Keys may be dot-separated paths (e.g. `"robot.speed"`) when using bracket
126
+ * notation: `proxy.state["robot.speed"] = 100`.
114
127
  */
115
128
  private _makeProxy;
116
- /** Dispatch an `UpdateEvent` to all registered handlers. */
129
+ /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */
117
130
  private _emit;
131
+ /** Notify all `onChange` handlers of a state change. */
132
+ private _emitChange;
118
133
  }
119
134
 
120
135
  /**
@@ -230,4 +245,4 @@ declare class WebSocketManager {
230
245
  disconnect(): void;
231
246
  }
232
247
 
233
- export { CrdtStateProxy, type UpdateEvent, type UpdateHandler, type WasmStateStore, type WebSocketLike, WebSocketManager };
248
+ export { CrdtStateProxy, type UpdateEvent, type UpdateHandler, type WebSocketLike, WebSocketManager };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,13 +17,23 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var index_exports = {};
22
32
  __export(index_exports, {
23
33
  CrdtStateProxy: () => CrdtStateProxy,
24
- WebSocketManager: () => WebSocketManager
34
+ WasmStateStore: () => import_crdt_sync.WasmStateStore,
35
+ WebSocketManager: () => WebSocketManager,
36
+ initWasm: () => import_crdt_sync.default
25
37
  });
26
38
  module.exports = __toCommonJS(index_exports);
27
39
 
@@ -34,31 +46,29 @@ var CrdtStateProxy = class {
34
46
  */
35
47
  constructor(store) {
36
48
  this._handlers = /* @__PURE__ */ new Set();
49
+ this._changeHandlers = /* @__PURE__ */ new Set();
37
50
  this._store = store;
38
- this._state = this._makeProxy("");
51
+ this._state = this._makeProxy();
39
52
  }
40
53
  // ── Public API ────────────────────────────────────────────────────────
41
54
  /**
42
55
  * The proxied state object.
43
56
  *
44
- * Assigning any property (or nested property) on this object will
45
- * automatically call `WasmStateStore.set_register` and fire `onUpdate`
46
- * listeners.
47
- *
48
- * ```ts
49
- * proxy.state.speed = 100; // key: "speed"
50
- * proxy.state.robot.speed = 100; // key: "robot.speed"
51
- * ```
57
+ * Reading a key returns the registered value, or `undefined` if it has
58
+ * not been set yet. Assigning a value calls `WasmStateStore.set_register`
59
+ * and fires all `onUpdate` and `onChange` listeners.
52
60
  */
53
61
  get state() {
54
62
  return this._state;
55
63
  }
56
64
  /**
57
- * Register a listener that is called whenever a property is written through
58
- * `proxy.state`.
65
+ * Register a listener that fires whenever a value is written through
66
+ * `proxy.state` (local writes only).
59
67
  *
60
- * @param handler - Callback receiving an `UpdateEvent`.
61
- * @returns An unsubscribe function call it to remove the listener.
68
+ * The `UpdateEvent` carries the register key, new value, and the CRDT
69
+ * envelope forward the envelope to peers via `apply_envelope`.
70
+ *
71
+ * @returns An unsubscribe function.
62
72
  */
63
73
  onUpdate(handler) {
64
74
  this._handlers.add(handler);
@@ -66,45 +76,74 @@ var CrdtStateProxy = class {
66
76
  this._handlers.delete(handler);
67
77
  };
68
78
  }
79
+ /**
80
+ * Register a listener that fires whenever state changes — whether from a
81
+ * local write or an incoming remote update (after `notifyRemoteUpdate`).
82
+ *
83
+ * Use this in UI frameworks to trigger re-renders:
84
+ *
85
+ * ```ts
86
+ * proxy.onChange(() => setTick(t => t + 1));
87
+ * ```
88
+ *
89
+ * @returns An unsubscribe function.
90
+ */
91
+ onChange(handler) {
92
+ this._changeHandlers.add(handler);
93
+ return () => {
94
+ this._changeHandlers.delete(handler);
95
+ };
96
+ }
97
+ /**
98
+ * Notify all `onChange` listeners that remote state has been applied to the
99
+ * store. Called by `WebSocketManager` after every `apply_envelope` so that
100
+ * UI frameworks re-render with the latest state.
101
+ */
102
+ notifyRemoteUpdate() {
103
+ this._emitChange();
104
+ }
69
105
  // ── Internal helpers ──────────────────────────────────────────────────
70
106
  /**
71
- * Recursively build a `Proxy` for the given dot-path `prefix`.
107
+ * Build the state `Proxy`.
72
108
  *
73
- * - **`get` trap**: returns a child proxy for the nested path so that deep
74
- * assignments like `proxy.state.robot.speed = 100` work correctly.
109
+ * - **`get` trap**: reads the value from the WASM store via `get_register`.
110
+ * Returns the parsed value, or `undefined` if the key has not been registered.
75
111
  * - **`set` trap**: serialises the value, calls `set_register`, and fires
76
- * all `onUpdate` listeners.
112
+ * all `onUpdate` and `onChange` listeners.
113
+ *
114
+ * Keys may be dot-separated paths (e.g. `"robot.speed"`) when using bracket
115
+ * notation: `proxy.state["robot.speed"] = 100`.
77
116
  */
78
- _makeProxy(prefix) {
79
- const children = {};
117
+ _makeProxy() {
80
118
  return new Proxy({}, {
81
119
  get: (_target, prop) => {
82
120
  if (typeof prop === "symbol") return void 0;
83
- const key = prefix ? `${prefix}.${String(prop)}` : String(prop);
84
- const raw = this._store.get_register(key);
85
- if (raw !== void 0) {
86
- return JSON.parse(raw);
87
- }
88
- if (!(prop in children)) {
89
- children[prop] = this._makeProxy(key);
90
- }
91
- return children[prop];
121
+ const raw = this._store.get_register(String(prop));
122
+ return raw !== void 0 ? JSON.parse(raw) : void 0;
92
123
  },
93
124
  set: (_target, prop, value) => {
94
125
  if (typeof prop === "symbol") return false;
95
- const key = prefix ? `${prefix}.${String(prop)}` : String(prop);
126
+ const key = String(prop);
96
127
  const envelope = this._store.set_register(key, JSON.stringify(value));
97
128
  this._emit({ key, value, envelope });
129
+ this._emitChange();
98
130
  return true;
99
131
  }
100
132
  });
101
133
  }
102
- /** Dispatch an `UpdateEvent` to all registered handlers. */
134
+ /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */
103
135
  _emit(event) {
104
136
  this._handlers.forEach((handler) => handler(event));
105
137
  }
138
+ /** Notify all `onChange` handlers of a state change. */
139
+ _emitChange() {
140
+ this._changeHandlers.forEach((handler) => handler());
141
+ }
106
142
  };
107
143
 
144
+ // src/index.ts
145
+ var import_crdt_sync = __toESM(require("../pkg/web/crdt_sync.js"));
146
+
108
147
  // src/WebSocketManager.ts
109
148
  var WebSocketManager = class {
110
149
  /**
@@ -162,6 +201,7 @@ var WebSocketManager = class {
162
201
  } else {
163
202
  this._store.apply_envelope(event.data);
164
203
  }
204
+ this._proxy.notifyRemoteUpdate();
165
205
  };
166
206
  ws.onclose = () => {
167
207
  };
@@ -223,6 +263,8 @@ var WebSocketManager = class {
223
263
  // Annotate the CommonJS export names for ESM import in node:
224
264
  0 && (module.exports = {
225
265
  CrdtStateProxy,
226
- WebSocketManager
266
+ WasmStateStore,
267
+ WebSocketManager,
268
+ initWasm
227
269
  });
228
270
  //# sourceMappingURL=index.js.map
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 { 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) => {\n if (typeof prop === 'symbol') return undefined;\n const key = prefix ? `${prefix}.${String(prop)}` : String(prop);\n\n const raw = this._store.get_register(key);\n if (raw !== undefined) {\n return JSON.parse(raw);\n }\n\n if (!(prop in children)) {\n children[prop] = this._makeProxy(key);\n }\n return children[prop];\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = prefix ? `${prefix}.${String(prop)}` : String(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,SAAS;AACtB,YAAI,OAAO,SAAS,SAAU,QAAO;AACrC,cAAM,MAAM,SAAS,GAAG,MAAM,IAAI,OAAO,IAAI,CAAC,KAAK,OAAO,IAAI;AAE9D,cAAM,MAAM,KAAK,OAAO,aAAa,GAAG;AACxC,YAAI,QAAQ,QAAW;AACrB,iBAAO,KAAK,MAAM,GAAG;AAAA,QACvB;AAEA,YAAI,EAAE,QA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;AAAA;AAAA;AAAA,EAUA,aAAmB;AACjB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,oBAAoB,CAAC;AAC1B,SAAK,gBAAgB,CAAC;AACtB,SAAK,IAAI,MAAM;AAAA,EACjB;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/CrdtStateProxy.ts","../src/WebSocketManager.ts"],"sourcesContent":["export { CrdtStateProxy } from './CrdtStateProxy.js';\nexport type { UpdateEvent, UpdateHandler } from './CrdtStateProxy.js';\nexport { default as initWasm, WasmStateStore } from '../pkg/web/crdt_sync.js';\nexport { WebSocketManager } from './WebSocketManager.js';\nexport type { WebSocketLike } from './WebSocketManager.js';\n","/**\n * TypeScript interface matching the Wasm-bindgen-generated `WasmStateStore`.\n *\n * In production, import the real `WasmStateStore` from the compiled Wasm\n * package (e.g. `import { WasmStateStore } from './crdt_sync.js'`).\n * In tests the interface can be satisfied by any mock object.\n */\nexport interface WasmStateStore {\n /** Write a JSON-encoded value to the named LWW register. Returns the Envelope JSON. */\n set_register(key: string, value_json: string): string;\n /** Read the current value of a named LWW register as a JSON string, or `undefined`. */\n get_register(key: string): string | undefined;\n /** Apply a remote Envelope (serialised as JSON) to this store. */\n apply_envelope(envelope_json: string): void;\n}\n\n/**\n * Payload delivered to every `onUpdate` listener when a property is written\n * through the proxy.\n */\nexport interface UpdateEvent {\n /** The register key that was updated (e.g. `\"speed\"` or `\"robot.speed\"`). */\n key: string;\n /** The new JavaScript value. */\n value: unknown;\n /**\n * The CRDT Envelope returned by `WasmStateStore.set_register`, serialised\n * as a JSON string. Broadcast this to peer nodes via `apply_envelope`.\n */\n envelope: string;\n}\n\n/** Callback type for `onUpdate` listeners. */\nexport type UpdateHandler = (event: UpdateEvent) => void;\n\n/**\n * A TypeScript proxy wrapper around `WasmStateStore` that gives frontend\n * developers a transparent, object-oriented experience.\n *\n * ## Reading state\n *\n * Access any registered key directly on `proxy.state`:\n *\n * ```ts\n * proxy.state.speed // returns the registered value, or undefined\n * proxy.state[\"robot.speed\"] // dot-path keys via bracket notation\n * ```\n *\n * ## Writing state\n *\n * Assign any value — primitives, objects, arrays:\n *\n * ```ts\n * proxy.state.speed = 100;\n * proxy.state.robot = { x: 10, y: 20 }; // store the whole object\n * proxy.state[\"robot.speed\"] = 100; // dot-path key\n * ```\n *\n * ## Listening for changes\n *\n * - `onUpdate(handler)` — fires on **local** writes with the full `UpdateEvent`\n * (key, value, envelope). Used by `WebSocketManager` to collect outgoing envelopes.\n * - `onChange(handler)` — fires on **any** change: local writes _and_ remote\n * `apply_envelope` notifications. Use this in UI frameworks to trigger re-renders.\n *\n * ```ts\n * const unsubscribe = proxy.onChange(() => setTick(t => t + 1));\n * ```\n */\nexport class CrdtStateProxy {\n private readonly _store: WasmStateStore;\n private readonly _handlers: Set<UpdateHandler> = new Set();\n private readonly _changeHandlers: Set<() => void> = new Set();\n private readonly _state: Record<string, unknown>;\n\n /**\n * Create a new `CrdtStateProxy` backed by the given `WasmStateStore`.\n *\n * @param store - The Wasm state store instance to proxy.\n */\n constructor(store: WasmStateStore) {\n this._store = store;\n this._state = this._makeProxy();\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * The proxied state object.\n *\n * Reading a key returns the registered value, or `undefined` if it has\n * not been set yet. Assigning a value calls `WasmStateStore.set_register`\n * and fires all `onUpdate` and `onChange` listeners.\n */\n get state(): Record<string, unknown> {\n return this._state;\n }\n\n /**\n * Register a listener that fires whenever a value is written through\n * `proxy.state` (local writes only).\n *\n * The `UpdateEvent` carries the register key, new value, and the CRDT\n * envelope — forward the envelope to peers via `apply_envelope`.\n *\n * @returns An unsubscribe function.\n */\n onUpdate(handler: UpdateHandler): () => void {\n this._handlers.add(handler);\n return () => {\n this._handlers.delete(handler);\n };\n }\n\n /**\n * Register a listener that fires whenever state changes — whether from a\n * local write or an incoming remote update (after `notifyRemoteUpdate`).\n *\n * Use this in UI frameworks to trigger re-renders:\n *\n * ```ts\n * proxy.onChange(() => setTick(t => t + 1));\n * ```\n *\n * @returns An unsubscribe function.\n */\n onChange(handler: () => void): () => void {\n this._changeHandlers.add(handler);\n return () => {\n this._changeHandlers.delete(handler);\n };\n }\n\n /**\n * Notify all `onChange` listeners that remote state has been applied to the\n * store. Called by `WebSocketManager` after every `apply_envelope` so that\n * UI frameworks re-render with the latest state.\n */\n notifyRemoteUpdate(): void {\n this._emitChange();\n }\n\n // ── Internal helpers ──────────────────────────────────────────────────\n\n /**\n * Build the state `Proxy`.\n *\n * - **`get` trap**: reads the value from the WASM store via `get_register`.\n * Returns the parsed value, or `undefined` if the key has not been registered.\n * - **`set` trap**: serialises the value, calls `set_register`, and fires\n * all `onUpdate` and `onChange` listeners.\n *\n * Keys may be dot-separated paths (e.g. `\"robot.speed\"`) when using bracket\n * notation: `proxy.state[\"robot.speed\"] = 100`.\n */\n private _makeProxy(): Record<string, unknown> {\n return new Proxy({} as Record<string, unknown>, {\n get: (_target, prop) => {\n if (typeof prop === 'symbol') return undefined;\n const raw = this._store.get_register(String(prop));\n return raw !== undefined ? JSON.parse(raw) : undefined;\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = String(prop);\n const envelope = this._store.set_register(key, JSON.stringify(value));\n this._emit({ key, value, envelope });\n this._emitChange();\n return true;\n },\n });\n }\n\n /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */\n private _emit(event: UpdateEvent): void {\n this._handlers.forEach((handler) => handler(event));\n }\n\n /** Notify all `onChange` handlers of a state change. */\n private _emitChange(): void {\n this._changeHandlers.forEach((handler) => handler());\n }\n}\n","import { CrdtStateProxy } from './CrdtStateProxy.js';\nimport type { WasmStateStore } from './CrdtStateProxy.js';\n\n/**\n * Minimal subset of the browser `WebSocket` API used by `WebSocketManager`.\n *\n * The real browser `WebSocket` satisfies this interface out-of-the-box.\n * In tests, a plain mock object can be used instead.\n */\nexport interface WebSocketLike {\n /** Current connection state (0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED). */\n readonly readyState: number;\n /** Send a UTF-8 string frame to the server. */\n send(data: string): void;\n /** Initiate the closing handshake. */\n close(): void;\n /** Fired when a message frame is received. */\n onmessage: ((event: { data: string }) => void) | null;\n /** Fired when the connection is established. */\n onopen: ((event: unknown) => void) | null;\n /** Fired when the connection is closed. */\n onclose: ((event: unknown) => void) | null;\n /** Fired when an error occurs. */\n onerror: ((event: unknown) => void) | null;\n}\n\n/**\n * Bridges a `CrdtStateProxy` to a WebSocket connection so that every CRDT\n * operation produced locally is broadcast to peers, and every envelope\n * received from a peer is applied to the local store.\n *\n * ## Data flow\n *\n * ```\n * Local write\n * → CrdtStateProxy.onUpdate (envelope collected in _pendingEnvelopes)\n * → requestAnimationFrame / setTimeout schedules a batch flush\n * → WebSocket.send(JSON.stringify(envelopes)) // one payload per frame\n *\n * Incoming message (single envelope or JSON array of envelopes)\n * → WebSocket.onmessage\n * → WasmStateStore.apply_envelope() // merge into local store\n * ```\n *\n * ## Throttling / batching\n *\n * Multiple proxy writes that occur within the same JavaScript task (e.g. a\n * 60 FPS game loop) are collected in `_pendingEnvelopes` and sent as a single\n * JSON array payload on the next animation frame (browser) or the next\n * `setTimeout(fn, 0)` tick (Node.js / non-browser environments). This keeps\n * network traffic proportional to frame rate rather than to the raw mutation\n * rate.\n *\n * ## Usage\n *\n * ```ts\n * import init, { WasmStateStore } from './crdt_sync.js';\n * import { CrdtStateProxy, WebSocketManager } from './index.js';\n *\n * await init();\n * const store = new WasmStateStore('node-1');\n * const proxy = new CrdtStateProxy(store);\n * const manager = new WebSocketManager(store, proxy, new WebSocket('wss://example.com/sync'));\n *\n * // Writes are automatically batched and broadcast to peers.\n * proxy.state.robot = { x: 10, y: 20 };\n *\n * // Clean up.\n * manager.disconnect();\n * ```\n */\nexport class WebSocketManager {\n private readonly _store: WasmStateStore;\n private readonly _proxy: CrdtStateProxy;\n private readonly _ws: WebSocketLike;\n private _unsubscribe: (() => void) | null = null;\n /** Envelopes collected in the current frame, waiting for the batch flush. */\n private _pendingEnvelopes: string[] = [];\n /** Cancels the currently scheduled batch flush (rAF or setTimeout handle). */\n private _cancelFlush: (() => void) | null = null;\n /** Envelopes queued while the socket is not open, flushed on reconnection. */\n private _offlineQueue: string[] = [];\n\n /**\n * Create a `WebSocketManager` and attach it to the given WebSocket.\n *\n * @param store - The Wasm state store. Incoming peer envelopes will be\n * applied to this store via `apply_envelope`.\n * @param proxy - The CRDT state proxy. Outgoing envelopes produced by\n * `set_register` calls will be read from the proxy's `onUpdate` events.\n * @param ws - An open or connecting WebSocket (or any `WebSocketLike` object).\n */\n constructor(store: WasmStateStore, proxy: CrdtStateProxy, ws: WebSocketLike) {\n this._store = store;\n this._proxy = proxy;\n this._ws = ws;\n this._attach();\n }\n\n // ── Internal setup ────────────────────────────────────────────────────\n\n private _attach(): void {\n const ws = this._ws;\n\n // Collect envelopes and schedule a batch flush once per frame so that\n // multiple synchronous writes (e.g. a 60 FPS game loop) are coalesced\n // into a single network payload instead of one message per mutation.\n this._unsubscribe = this._proxy.onUpdate(({ envelope }) => {\n this._pendingEnvelopes.push(envelope);\n this._scheduleBatchFlush();\n });\n\n // On (re)connection, immediately flush pending envelopes together with any\n // envelopes that were queued while the socket was offline.\n ws.onopen = () => {\n // Cancel any scheduled flush — we will drain everything right now.\n this._cancelFlush?.();\n this._cancelFlush = null;\n const offline = this._offlineQueue;\n const pending = this._pendingEnvelopes;\n this._offlineQueue = [];\n this._pendingEnvelopes = [];\n const batch = [...offline, ...pending];\n if (batch.length > 0) {\n ws.send(JSON.stringify(batch));\n }\n };\n\n // Apply envelopes received from peers. Peers may send either a single\n // envelope JSON string or a JSON array of envelope strings (batch format).\n ws.onmessage = (event) => {\n let parsed: unknown;\n try {\n parsed = JSON.parse(event.data);\n } catch {\n parsed = null;\n }\n if (Array.isArray(parsed)) {\n for (const env of parsed) {\n this._store.apply_envelope(env as string);\n }\n } else {\n // Fallback: treat the raw frame data as a single envelope string.\n this._store.apply_envelope(event.data);\n }\n // Notify UI listeners (e.g. React) that remote state has been applied.\n this._proxy.notifyRemoteUpdate();\n };\n\n // On close or error the subscription stays active so that writes made\n // while offline are buffered and flushed when the socket reconnects.\n ws.onclose = () => { /* keep buffering */ };\n ws.onerror = () => { /* keep buffering */ };\n }\n\n /**\n * Schedule a single batch flush for the current frame. Subsequent calls\n * before the flush fires are no-ops (only one flush is ever outstanding).\n *\n * Uses `requestAnimationFrame` when available (browser, ~60 FPS cadence),\n * falling back to `setTimeout(fn, 0)` in non-browser environments.\n */\n private _scheduleBatchFlush(): void {\n if (this._cancelFlush !== null) return; // already scheduled\n\n const doFlush = () => {\n this._cancelFlush = null;\n this._flushBatch();\n };\n\n if (typeof requestAnimationFrame === 'function') {\n const id = requestAnimationFrame(doFlush);\n this._cancelFlush = () => cancelAnimationFrame(id);\n } else {\n const id = setTimeout(doFlush, 0);\n this._cancelFlush = () => clearTimeout(id);\n }\n }\n\n /**\n * Send all pending envelopes as a single JSON-array payload, or move them\n * to the offline queue if the socket is not currently open.\n */\n private _flushBatch(): void {\n const envelopes = this._pendingEnvelopes.splice(0);\n if (envelopes.length === 0) return;\n\n const ws = this._ws;\n if (ws.readyState === 1 /* OPEN */) {\n ws.send(JSON.stringify(envelopes));\n } else {\n this._offlineQueue.push(...envelopes);\n }\n }\n\n // ── Public API ────────────────────────────────────────────────────────\n\n /**\n * Unsubscribe from proxy updates, discard any buffered envelopes, and close\n * the WebSocket connection.\n *\n * Safe to call more than once.\n */\n disconnect(): void {\n this._unsubscribe?.();\n this._unsubscribe = null;\n this._cancelFlush?.();\n this._cancelFlush = null;\n this._pendingEnvelopes = [];\n this._offlineQueue = [];\n this._ws.close();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqEO,IAAM,iBAAN,MAAqB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAW1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jB;AACF;","names":[]}
package/dist/index.mjs CHANGED
@@ -7,31 +7,29 @@ var CrdtStateProxy = class {
7
7
  */
8
8
  constructor(store) {
9
9
  this._handlers = /* @__PURE__ */ new Set();
10
+ this._changeHandlers = /* @__PURE__ */ new Set();
10
11
  this._store = store;
11
- this._state = this._makeProxy("");
12
+ this._state = this._makeProxy();
12
13
  }
13
14
  // ── Public API ────────────────────────────────────────────────────────
14
15
  /**
15
16
  * The proxied state object.
16
17
  *
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
- * ```
18
+ * Reading a key returns the registered value, or `undefined` if it has
19
+ * not been set yet. Assigning a value calls `WasmStateStore.set_register`
20
+ * and fires all `onUpdate` and `onChange` listeners.
25
21
  */
26
22
  get state() {
27
23
  return this._state;
28
24
  }
29
25
  /**
30
- * Register a listener that is called whenever a property is written through
31
- * `proxy.state`.
26
+ * Register a listener that fires whenever a value is written through
27
+ * `proxy.state` (local writes only).
32
28
  *
33
- * @param handler - Callback receiving an `UpdateEvent`.
34
- * @returns An unsubscribe function call it to remove the listener.
29
+ * The `UpdateEvent` carries the register key, new value, and the CRDT
30
+ * envelope forward the envelope to peers via `apply_envelope`.
31
+ *
32
+ * @returns An unsubscribe function.
35
33
  */
36
34
  onUpdate(handler) {
37
35
  this._handlers.add(handler);
@@ -39,45 +37,74 @@ var CrdtStateProxy = class {
39
37
  this._handlers.delete(handler);
40
38
  };
41
39
  }
40
+ /**
41
+ * Register a listener that fires whenever state changes — whether from a
42
+ * local write or an incoming remote update (after `notifyRemoteUpdate`).
43
+ *
44
+ * Use this in UI frameworks to trigger re-renders:
45
+ *
46
+ * ```ts
47
+ * proxy.onChange(() => setTick(t => t + 1));
48
+ * ```
49
+ *
50
+ * @returns An unsubscribe function.
51
+ */
52
+ onChange(handler) {
53
+ this._changeHandlers.add(handler);
54
+ return () => {
55
+ this._changeHandlers.delete(handler);
56
+ };
57
+ }
58
+ /**
59
+ * Notify all `onChange` listeners that remote state has been applied to the
60
+ * store. Called by `WebSocketManager` after every `apply_envelope` so that
61
+ * UI frameworks re-render with the latest state.
62
+ */
63
+ notifyRemoteUpdate() {
64
+ this._emitChange();
65
+ }
42
66
  // ── Internal helpers ──────────────────────────────────────────────────
43
67
  /**
44
- * Recursively build a `Proxy` for the given dot-path `prefix`.
68
+ * Build the state `Proxy`.
45
69
  *
46
- * - **`get` trap**: returns a child proxy for the nested path so that deep
47
- * assignments like `proxy.state.robot.speed = 100` work correctly.
70
+ * - **`get` trap**: reads the value from the WASM store via `get_register`.
71
+ * Returns the parsed value, or `undefined` if the key has not been registered.
48
72
  * - **`set` trap**: serialises the value, calls `set_register`, and fires
49
- * all `onUpdate` listeners.
73
+ * all `onUpdate` and `onChange` listeners.
74
+ *
75
+ * Keys may be dot-separated paths (e.g. `"robot.speed"`) when using bracket
76
+ * notation: `proxy.state["robot.speed"] = 100`.
50
77
  */
51
- _makeProxy(prefix) {
52
- const children = {};
78
+ _makeProxy() {
53
79
  return new Proxy({}, {
54
80
  get: (_target, prop) => {
55
81
  if (typeof prop === "symbol") return void 0;
56
- const key = prefix ? `${prefix}.${String(prop)}` : String(prop);
57
- const raw = this._store.get_register(key);
58
- if (raw !== void 0) {
59
- return JSON.parse(raw);
60
- }
61
- if (!(prop in children)) {
62
- children[prop] = this._makeProxy(key);
63
- }
64
- return children[prop];
82
+ const raw = this._store.get_register(String(prop));
83
+ return raw !== void 0 ? JSON.parse(raw) : void 0;
65
84
  },
66
85
  set: (_target, prop, value) => {
67
86
  if (typeof prop === "symbol") return false;
68
- const key = prefix ? `${prefix}.${String(prop)}` : String(prop);
87
+ const key = String(prop);
69
88
  const envelope = this._store.set_register(key, JSON.stringify(value));
70
89
  this._emit({ key, value, envelope });
90
+ this._emitChange();
71
91
  return true;
72
92
  }
73
93
  });
74
94
  }
75
- /** Dispatch an `UpdateEvent` to all registered handlers. */
95
+ /** Dispatch an `UpdateEvent` to all `onUpdate` handlers. */
76
96
  _emit(event) {
77
97
  this._handlers.forEach((handler) => handler(event));
78
98
  }
99
+ /** Notify all `onChange` handlers of a state change. */
100
+ _emitChange() {
101
+ this._changeHandlers.forEach((handler) => handler());
102
+ }
79
103
  };
80
104
 
105
+ // src/index.ts
106
+ import { default as default2, WasmStateStore } from "../pkg/web/crdt_sync.js";
107
+
81
108
  // src/WebSocketManager.ts
82
109
  var WebSocketManager = class {
83
110
  /**
@@ -135,6 +162,7 @@ var WebSocketManager = class {
135
162
  } else {
136
163
  this._store.apply_envelope(event.data);
137
164
  }
165
+ this._proxy.notifyRemoteUpdate();
138
166
  };
139
167
  ws.onclose = () => {
140
168
  };
@@ -195,6 +223,8 @@ var WebSocketManager = class {
195
223
  };
196
224
  export {
197
225
  CrdtStateProxy,
198
- WebSocketManager
226
+ WasmStateStore,
227
+ WebSocketManager,
228
+ default2 as initWasm
199
229
  };
200
230
  //# sourceMappingURL=index.mjs.map
@@ -1 +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) => {\n if (typeof prop === 'symbol') return undefined;\n const key = prefix ? `${prefix}.${String(prop)}` : String(prop);\n\n const raw = this._store.get_register(key);\n if (raw !== undefined) {\n return JSON.parse(raw);\n }\n\n if (!(prop in children)) {\n children[prop] = this._makeProxy(key);\n }\n return children[prop];\n },\n\n set: (_target, prop, value: unknown) => {\n if (typeof prop === 'symbol') return false;\n const key = prefix ? `${prefix}.${String(prop)}` : String(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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A,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":[]}
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,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,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,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crdt-sync/core",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript proxy wrapper for the crdt-sync Wasm StateStore",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",