@rozek/sds-sync-engine 0.0.1

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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026-present Andreas Rozek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,196 @@
1
+ # @rozek/sds-sync-engine
2
+
3
+ The orchestration layer of the **shareable-data-store** (SDS) family. `SDS_SyncEngine` wires an `SDS_DataStore` to a persistence provider, a network provider, and a presence provider, and manages the full lifecycle: startup restore, offline patch queuing, automatic checkpointing, large-value transfer, and presence heartbeats.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pnpm add @rozek/sds-sync-engine
11
+ ```
12
+
13
+ ---
14
+
15
+ ## Concepts
16
+
17
+ ### Startup restore
18
+
19
+ When `start()` is called, the engine loads the latest snapshot from the persistence provider and replays all patches recorded since that snapshot. The store is in a consistent, up-to-date state before `start()` resolves.
20
+
21
+ ### Offline queue
22
+
23
+ While the network connection is in `'disconnected'` or `'reconnecting'` state, outgoing patches are queued in memory. As soon as the connection transitions to `'connected'`, the queue is flushed in order.
24
+
25
+ ### Automatic checkpointing
26
+
27
+ Every local mutation's patch bytes are accumulated. When the total crosses **512 KB**, the engine writes a new snapshot and prunes all patches up to that point. A final checkpoint is also written on `stop()` if there are any un-checkpointed patches.
28
+
29
+ ### Large-Value Transfer
30
+
31
+ When a data's value changes to a reference kind (`'literal-reference'` or `'binary-reference'`), the engine sends the blob to the network provider. When the store receives a patch referencing an unknown blob hash, the engine requests the blob from the network provider.
32
+
33
+ ### Presence Heartbeat
34
+
35
+ The engine periodically re-broadcasts the local presence state so that remote peers can detect stale entries (timeout controlled by `PresenceTimeoutMs`).
36
+
37
+ ### BroadcastChannel (Browser / Tauri)
38
+
39
+ When running in a browser or Tauri context, the engine optionally uses a `BroadcastChannel` to relay patches and presence frames between tabs opened on the same origin, without going through the server.
40
+
41
+ ---
42
+
43
+ ## API Reference
44
+
45
+ ### `SDS_SyncEngine`
46
+
47
+ ```typescript
48
+ import { SDS_SyncEngine } from '@rozek/sds-sync-engine'
49
+
50
+ class SDS_SyncEngine {
51
+ constructor (Store:SDS_DataStore, Options?:SDS_SyncEngineOptions)
52
+
53
+ /**** Lifecycle ****/
54
+
55
+ start ():Promise<void> // restore, wire providers
56
+ stop ():Promise<void> // flush queue, write checkpoint, close providers
57
+
58
+ /**** Network ****/
59
+
60
+ connectTo (URL:string, Options:SDS_ConnectionOptions):Promise<void>
61
+ disconnect ():void
62
+ reconnect ():Promise<void>
63
+
64
+ get ConnectionState ():SDS_ConnectionState
65
+ onConnectionChange (Callback:(State:SDS_ConnectionState) => void):() => void
66
+
67
+ /**** Presence ****/
68
+
69
+ readonly PeerId:string // unique identifier for this engine instance (UUID)
70
+
71
+ setPresenceTo (State:SDS_LocalPresenceState):void
72
+ readonly PeerSet:ReadonlyMap<string, SDS_RemotePresenceState>
73
+ onPresenceChange(
74
+ Callback:(
75
+ PeerId:string,
76
+ State: SDS_RemotePresenceState | undefined,
77
+ Origin:'local' | 'remote'
78
+ ) => void
79
+ ):() => void
80
+ }
81
+ ```
82
+
83
+ ### `SDS_SyncEngineOptions`
84
+
85
+ ```typescript
86
+ interface SDS_SyncEngineOptions {
87
+ PersistenceProvider?:SDS_PersistenceProvider // SQLite or IndexedDB
88
+ NetworkProvider?: SDS_NetworkProvider // WebSocket or WebRTC
89
+ PresenceProvider?: SDS_PresenceProvider // often the same as NetworkProvider
90
+ BroadcastChannel?: boolean // cross-tab relay (default: true in browser)
91
+ PresenceTimeoutMs?: number // peer inactivity timeout (default: 120 000 ms)
92
+ }
93
+ ```
94
+
95
+ All providers are optional. You can use any combination — for example persistence only (no network), or network only (no persistence).
96
+
97
+ ### Error codes
98
+
99
+ | Code | Thrown by | Reason |
100
+ | --- | --- | --- |
101
+ | `'no-network-provider'` | `connectTo()` | No `NetworkProvider` was configured |
102
+ | `'not-yet-connected'` | `reconnect()` | `connectTo()` has never been called successfully |
103
+
104
+ ---
105
+
106
+ ## Usage
107
+
108
+ ### Persistence only — offline-capable local Store
109
+
110
+ ```typescript
111
+ import { SDS_DataStore } from '@rozek/sds-core'
112
+ import { SDS_DesktopPersistenceProvider } from '@rozek/sds-persistence-node'
113
+ import { SDS_SyncEngine } from '@rozek/sds-sync-engine'
114
+
115
+ const DataStore = SDS_DataStore.fromScratch()
116
+ const Persistence = new SDS_DesktopPersistenceProvider('./data', 'my-store')
117
+
118
+ const engine = new SDS_SyncEngine(DataStore, { PersistenceProvider:Persistence })
119
+ await engine.start()
120
+
121
+ const data = DataStore.newItemAt('text/plain', DataStore.RootItem)
122
+ data.Label = 'This data survives restarts'
123
+
124
+ await engine.stop() // writes checkpoint, closes DB
125
+ ```
126
+
127
+ ### Full Stack — Persistence + WebSocket + Presence
128
+
129
+ ```typescript
130
+ import { SDS_DataStore } from '@rozek/sds-core'
131
+ import { SDS_BrowserPersistenceProvider } from '@rozek/sds-persistence-browser'
132
+ import { SDS_WebSocketProvider } from '@rozek/sds-network-websocket'
133
+ import { SDS_SyncEngine } from '@rozek/sds-sync-engine'
134
+
135
+ const DataStore = SDS_DataStore.fromScratch()
136
+ const Persistence = new SDS_BrowserPersistenceProvider('my-store')
137
+ const Network = new SDS_WebSocketProvider('my-store')
138
+
139
+ const SyncEngine = new SDS_SyncEngine(DataStore, {
140
+ PersistenceProvider:Persistence,
141
+ NetworkProvider: Network,
142
+ PresenceProvider:Network,
143
+ })
144
+
145
+ await SyncEngine.start()
146
+ await SyncEngine.connectTo('wss://my-server.example.com', { Token:'<jwt>' })
147
+
148
+ SyncEngine.onConnectionChange((ConnectionState) => {
149
+ if (ConnectionState === 'connected') console.log('Online — syncing')
150
+ if (ConnectionState === 'reconnecting') console.log('Offline — patches queued')
151
+ })
152
+ ```
153
+
154
+ ### Presence — show Collaborators
155
+
156
+ ```typescript
157
+ // announce yourself
158
+ SyncEngine.setPresenceTo({
159
+ UserName: 'Alice',
160
+ UserColor:'#3498db',
161
+ UserFocus:{ EntryId:data.Id, Property:'Value', Cursor:{ from:4, to:4 } },
162
+ })
163
+
164
+ // react to any peer change (local or remote)
165
+ SyncEngine.onPresenceChange((PeerId,PeerState,Origin) => {
166
+ if (PeerState == null) {
167
+ // peer timed out
168
+ removeAvatarFor(PeerId)
169
+ } else if (Origin === 'remote') {
170
+ showAvatarFor(PeerId,PeerState)
171
+ }
172
+ })
173
+
174
+ // snapshot of all currently active peers
175
+ for (const [PeerId,PeerState] of SyncEngine.PeerSet) {
176
+ console.log(PeerId, PeerState.UserName, PeerState.UserFocus)
177
+ }
178
+ ```
179
+
180
+ ### Reconnect after a planned disconnect
181
+
182
+ ```typescript
183
+ await SyncEngine.connectTo('wss://my-server.example.com', { Token:'<jwt>' })
184
+
185
+ // … later …
186
+ SyncEngine.disconnect()
187
+
188
+ // … reconnect using the same URL and token
189
+ await SyncEngine.reconnect()
190
+ ```
191
+
192
+ ---
193
+
194
+ ## License
195
+
196
+ MIT © Andreas Rozek
@@ -0,0 +1,44 @@
1
+ import { SDS_ConnectionOptions } from '@rozek/sds-core';
2
+ import { SDS_ConnectionState } from '@rozek/sds-core';
3
+ import { SDS_DataStore } from '@rozek/sds-core';
4
+ import { SDS_LocalPresenceState } from '@rozek/sds-core';
5
+ import { SDS_NetworkProvider } from '@rozek/sds-core';
6
+ import { SDS_PersistenceProvider } from '@rozek/sds-core';
7
+ import { SDS_PresenceProvider } from '@rozek/sds-core';
8
+ import { SDS_RemotePresenceState } from '@rozek/sds-core';
9
+
10
+ export declare class SDS_SyncEngine {
11
+ #private;
12
+ readonly PeerId: string;
13
+ constructor(Store: SDS_DataStore, Options?: SDS_SyncEngineOptions);
14
+ /**** start ****/
15
+ start(): Promise<void>;
16
+ /**** stop ****/
17
+ stop(): Promise<void>;
18
+ /**** connectTo ****/
19
+ connectTo(URL: string, Options: SDS_ConnectionOptions): Promise<void>;
20
+ /**** disconnect ****/
21
+ disconnect(): void;
22
+ /**** reconnect ****/
23
+ reconnect(): Promise<void>;
24
+ /**** ConnectionState ****/
25
+ get ConnectionState(): SDS_ConnectionState;
26
+ /**** onConnectionChange ****/
27
+ onConnectionChange(Callback: (State: SDS_ConnectionState) => void): () => void;
28
+ /**** setPresenceTo ****/
29
+ setPresenceTo(State: Omit<SDS_LocalPresenceState, never>): void;
30
+ /**** PeerSet (remote peers only) ****/
31
+ get PeerSet(): ReadonlyMap<string, SDS_RemotePresenceState>;
32
+ /**** onPresenceChange ****/
33
+ onPresenceChange(Callback: (PeerId: string, State: SDS_RemotePresenceState | undefined, Origin: 'local' | 'remote') => void): () => void;
34
+ }
35
+
36
+ export declare interface SDS_SyncEngineOptions {
37
+ PersistenceProvider?: SDS_PersistenceProvider;
38
+ NetworkProvider?: SDS_NetworkProvider;
39
+ PresenceProvider?: SDS_PresenceProvider;
40
+ BroadcastChannel?: boolean;
41
+ PresenceTimeoutMs?: number;
42
+ }
43
+
44
+ export { }
@@ -0,0 +1,321 @@
1
+ var st = Object.defineProperty;
2
+ var z = (n) => {
3
+ throw TypeError(n);
4
+ };
5
+ var it = (n, e, s) => e in n ? st(n, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[e] = s;
6
+ var F = (n, e, s) => it(n, typeof e != "symbol" ? e + "" : e, s), A = (n, e, s) => e.has(n) || z("Cannot " + s);
7
+ var t = (n, e, s) => (A(n, e, "read from private field"), s ? s.call(n) : e.get(n)), r = (n, e, s) => e.has(n) ? z("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(n) : e.set(n, s), c = (n, e, s, i) => (A(n, e, "write to private field"), i ? i.call(n, s) : e.set(n, s), s), v = (n, e, s) => (A(n, e, "access private method"), s);
8
+ var G = (n, e, s, i) => ({
9
+ set _(o) {
10
+ c(n, e, o, s);
11
+ },
12
+ get _() {
13
+ return t(n, e, i);
14
+ }
15
+ });
16
+ import { SDS_Error as U } from "@rozek/sds-core";
17
+ const ot = 512 * 1024;
18
+ var u, d, a, P, k, L, N, _, H, M, C, D, T, b, B, S, m, w, x, R, g, y, h, J, K, W, X, Y, Q, Z, I, O, tt, j;
19
+ class at {
20
+ //----------------------------------------------------------------------------//
21
+ // Constructor //
22
+ //----------------------------------------------------------------------------//
23
+ constructor(e, s = {}) {
24
+ r(this, h);
25
+ r(this, u);
26
+ r(this, d);
27
+ r(this, a);
28
+ r(this, P);
29
+ r(this, k);
30
+ F(this, "PeerId", crypto.randomUUID());
31
+ r(this, L);
32
+ r(this, N);
33
+ r(this, _, []);
34
+ // outgoing patch queue (patches created while disconnected)
35
+ r(this, H, 0);
36
+ // accumulated patch bytes since last checkpoint
37
+ r(this, M, 0);
38
+ // sequence number of the last saved snapshot
39
+ r(this, C, 0);
40
+ // current patch sequence # (append-monotonic counter, managed by SyncEngine)
41
+ // CRDT cursor captured after the last processed local change;
42
+ // passed to Store.exportPatch() to retrieve exactly that one change.
43
+ // Initialised to an empty cursor; updated in #loadAndRestore and after
44
+ // each local mutation. Backend-agnostic: the DataStore owns the format.
45
+ r(this, D, new Uint8Array(0));
46
+ // heartbeat timer
47
+ r(this, T);
48
+ r(this, b);
49
+ // presence peer tracking
50
+ r(this, B, /* @__PURE__ */ new Map());
51
+ r(this, S, /* @__PURE__ */ new Map());
52
+ r(this, m, /* @__PURE__ */ new Set());
53
+ // BroadcastChannel (optional, browser/tauri only)
54
+ r(this, w);
55
+ // connection state mirror
56
+ r(this, x, "disconnected");
57
+ r(this, R, /* @__PURE__ */ new Set());
58
+ // unsubscribe functions for registered handlers
59
+ r(this, g, []);
60
+ // tracks entryId → blob hash for all entries whose value is in a *-reference kind;
61
+ // used to call releaseValue() when the entry's value changes or the entry is purged
62
+ r(this, y, /* @__PURE__ */ new Map());
63
+ c(this, u, e), c(this, d, s.PersistenceProvider ?? void 0), c(this, a, s.NetworkProvider ?? void 0), c(this, P, s.PresenceProvider ?? s.NetworkProvider ?? void 0), c(this, k, s.PresenceTimeoutMs ?? 12e4), (s.BroadcastChannel ?? !0) && typeof BroadcastChannel < "u" && t(this, a) != null && c(this, w, new BroadcastChannel(`sns:${t(this, a).StoreId}`));
64
+ }
65
+ //----------------------------------------------------------------------------//
66
+ // Lifecycle //
67
+ //----------------------------------------------------------------------------//
68
+ /**** start ****/
69
+ async start() {
70
+ t(this, d) != null && t(this, u).setValueBlobLoader(
71
+ (e) => t(this, d).loadValue(e)
72
+ ), await v(this, h, J).call(this), v(this, h, K).call(this), v(this, h, W).call(this), v(this, h, X).call(this), v(this, h, Y).call(this), t(this, a) != null && t(this, a).onConnectionChange((e) => {
73
+ c(this, x, e);
74
+ for (const s of t(this, R))
75
+ try {
76
+ s(e);
77
+ } catch {
78
+ }
79
+ e === "connected" && v(this, h, Z).call(this);
80
+ });
81
+ }
82
+ /**** stop ****/
83
+ async stop() {
84
+ var e, s, i;
85
+ t(this, T) != null && (clearInterval(t(this, T)), c(this, T, void 0));
86
+ for (const o of t(this, S).values())
87
+ clearTimeout(o);
88
+ t(this, S).clear();
89
+ for (const o of t(this, g))
90
+ try {
91
+ o();
92
+ } catch {
93
+ }
94
+ c(this, g, []), (e = t(this, w)) == null || e.close(), c(this, w, void 0), (s = t(this, a)) == null || s.disconnect(), t(this, d) != null && t(this, H) > 0 && await v(this, h, Q).call(this), await ((i = t(this, d)) == null ? void 0 : i.close());
95
+ }
96
+ //----------------------------------------------------------------------------//
97
+ // Network Connection //
98
+ //----------------------------------------------------------------------------//
99
+ /**** connectTo ****/
100
+ async connectTo(e, s) {
101
+ if (t(this, a) == null)
102
+ throw new U("no-network-provider", "no NetworkProvider configured");
103
+ c(this, L, e), c(this, N, s), await t(this, a).connect(e, s);
104
+ }
105
+ /**** disconnect ****/
106
+ disconnect() {
107
+ if (t(this, a) == null)
108
+ throw new U("no-network-provider", "no NetworkProvider configured");
109
+ t(this, a).disconnect();
110
+ }
111
+ /**** reconnect ****/
112
+ async reconnect() {
113
+ if (t(this, a) == null)
114
+ throw new U("no-network-provider", "no NetworkProvider configured");
115
+ if (t(this, L) == null)
116
+ throw new U(
117
+ "not-yet-connected",
118
+ "connectTo() has not been called yet; cannot reconnect"
119
+ );
120
+ await t(this, a).connect(t(this, L), t(this, N));
121
+ }
122
+ /**** ConnectionState ****/
123
+ get ConnectionState() {
124
+ return t(this, x);
125
+ }
126
+ /**** onConnectionChange ****/
127
+ onConnectionChange(e) {
128
+ return t(this, R).add(e), () => {
129
+ t(this, R).delete(e);
130
+ };
131
+ }
132
+ //----------------------------------------------------------------------------//
133
+ // Presence //
134
+ //----------------------------------------------------------------------------//
135
+ /**** setPresenceTo ****/
136
+ setPresenceTo(e) {
137
+ var i, o;
138
+ c(this, b, e);
139
+ const s = { ...e, PeerId: this.PeerId };
140
+ (i = t(this, P)) == null || i.sendLocalState(e), (o = t(this, w)) == null || o.postMessage({ type: "presence", payload: e });
141
+ for (const l of t(this, m))
142
+ try {
143
+ l(this.PeerId, s, "local");
144
+ } catch (f) {
145
+ console.error("SDS: presence handler failed", f);
146
+ }
147
+ }
148
+ /**** PeerSet (remote peers only) ****/
149
+ get PeerSet() {
150
+ return t(this, B);
151
+ }
152
+ /**** onPresenceChange ****/
153
+ onPresenceChange(e) {
154
+ return t(this, m).add(e), () => {
155
+ t(this, m).delete(e);
156
+ };
157
+ }
158
+ }
159
+ u = new WeakMap(), d = new WeakMap(), a = new WeakMap(), P = new WeakMap(), k = new WeakMap(), L = new WeakMap(), N = new WeakMap(), _ = new WeakMap(), H = new WeakMap(), M = new WeakMap(), C = new WeakMap(), D = new WeakMap(), T = new WeakMap(), b = new WeakMap(), B = new WeakMap(), S = new WeakMap(), m = new WeakMap(), w = new WeakMap(), x = new WeakMap(), R = new WeakMap(), g = new WeakMap(), y = new WeakMap(), h = new WeakSet(), J = async function() {
160
+ if (t(this, d) == null)
161
+ return;
162
+ await t(this, d).loadSnapshot();
163
+ const e = await t(this, d).loadPatchesSince(t(this, M));
164
+ for (const s of e)
165
+ try {
166
+ t(this, u).applyRemotePatch(s);
167
+ } catch {
168
+ }
169
+ e.length > 0 && c(this, C, t(this, M) + e.length), c(this, D, t(this, u).currentCursor);
170
+ }, //----------------------------------------------------------------------------//
171
+ // Wiring //
172
+ //----------------------------------------------------------------------------//
173
+ /**** #wireStoreToProviders — subscribes to local store changes and routes them to persistence and network ****/
174
+ K = function() {
175
+ const e = t(this, u).onChangeInvoke((s, i) => {
176
+ var f, q;
177
+ if (s === "external") {
178
+ v(this, h, I).call(this, i, "request").catch(() => {
179
+ });
180
+ return;
181
+ }
182
+ const o = t(this, D);
183
+ G(this, C)._++;
184
+ const l = t(this, u).exportPatch(o);
185
+ c(this, D, t(this, u).currentCursor), l.byteLength !== 0 && (t(this, d) != null && (t(this, d).appendPatch(l, t(this, C)).catch(() => {
186
+ }), c(this, H, t(this, H) + l.byteLength), t(this, H) >= ot && v(this, h, Q).call(this).catch(() => {
187
+ })), ((f = t(this, a)) == null ? void 0 : f.ConnectionState) === "connected" ? (t(this, a).sendPatch(l), (q = t(this, w)) == null || q.postMessage({ type: "patch", payload: l })) : t(this, _).push(l), v(this, h, I).call(this, i, "send").catch(() => {
188
+ }));
189
+ });
190
+ t(this, g).push(e);
191
+ }, /**** #wireNetworkToStore — subscribes to incoming network patches and presence events ****/
192
+ W = function() {
193
+ if (t(this, a) != null) {
194
+ const s = t(this, a).onPatch((o) => {
195
+ try {
196
+ t(this, u).applyRemotePatch(o);
197
+ } catch {
198
+ }
199
+ });
200
+ t(this, g).push(s);
201
+ const i = t(this, a).onValue(async (o, l) => {
202
+ var f;
203
+ t(this, u).storeValueBlob(o, l), await ((f = t(this, d)) == null ? void 0 : f.saveValue(o, l));
204
+ });
205
+ t(this, g).push(i);
206
+ }
207
+ const e = t(this, P);
208
+ if (e != null) {
209
+ const s = e.onRemoteState((i, o) => {
210
+ v(this, h, O).call(this, i, o);
211
+ });
212
+ t(this, g).push(s);
213
+ }
214
+ }, /**** #wirePresenceHeartbeat — starts a periodic timer to re-broadcast local presence state ****/
215
+ X = function() {
216
+ const e = t(this, k) / 4;
217
+ c(this, T, setInterval(() => {
218
+ var s, i;
219
+ t(this, b) != null && ((s = t(this, P)) == null || s.sendLocalState(t(this, b)), (i = t(this, w)) == null || i.postMessage({ type: "presence", payload: t(this, b) }));
220
+ }, e));
221
+ }, /**** #wireBroadcastChannel — wires the BroadcastChannel for cross-tab patch and presence relay ****/
222
+ Y = function() {
223
+ t(this, w) != null && (t(this, w).onmessage = (e) => {
224
+ var i;
225
+ const s = e.data;
226
+ switch (!0) {
227
+ case s.type === "patch":
228
+ try {
229
+ t(this, u).applyRemotePatch(s.payload);
230
+ } catch (o) {
231
+ console.error("SDS: failed to apply remote patch from BroadcastChannel", o);
232
+ }
233
+ break;
234
+ case s.type === "presence":
235
+ (i = t(this, P)) == null || i.sendLocalState(s.payload);
236
+ break;
237
+ }
238
+ });
239
+ }, Q = async function() {
240
+ t(this, d) != null && (await t(this, d).saveSnapshot(t(this, u).asBinary()), await t(this, d).prunePatches(t(this, C)), c(this, M, t(this, C)), c(this, H, 0));
241
+ }, //----------------------------------------------------------------------------//
242
+ // Offline Queue Flush //
243
+ //----------------------------------------------------------------------------//
244
+ /**** #flushOfflineQueue — sends all queued offline patches to the network ****/
245
+ Z = function() {
246
+ var s;
247
+ const e = t(this, _).splice(0);
248
+ for (const i of e)
249
+ try {
250
+ (s = t(this, a)) == null || s.sendPatch(i);
251
+ } catch (o) {
252
+ console.error("SDS: failed to send queued patch", o);
253
+ }
254
+ }, I = async function(e, s) {
255
+ var i, o, l;
256
+ for (const [f, q] of Object.entries(e)) {
257
+ const $ = q;
258
+ if ($.has("Existence")) {
259
+ const V = t(this, y).get(f);
260
+ V != null && (await ((i = t(this, d)) == null ? void 0 : i.releaseValue(V)), t(this, y).delete(f));
261
+ }
262
+ if (!$.has("Value"))
263
+ continue;
264
+ const E = t(this, y).get(f), p = t(this, u)._getValueRefOf(f), et = p == null ? void 0 : p.Hash;
265
+ if (E != null && E !== et && (await ((o = t(this, d)) == null ? void 0 : o.releaseValue(E)), t(this, y).delete(f)), p != null) {
266
+ if (t(this, a) == null) {
267
+ t(this, y).set(f, p.Hash);
268
+ continue;
269
+ }
270
+ if (s === "send") {
271
+ const V = t(this, u).getValueBlobByHash(p.Hash);
272
+ V != null && (await ((l = t(this, d)) == null ? void 0 : l.saveValue(p.Hash, V)), t(this, y).set(f, p.Hash), t(this, a).ConnectionState === "connected" && t(this, a).sendValue(p.Hash, V));
273
+ } else
274
+ t(this, y).set(f, p.Hash), !t(this, u).hasValueBlob(p.Hash) && t(this, a).ConnectionState === "connected" && t(this, a).requestValue(p.Hash);
275
+ }
276
+ }
277
+ }, //----------------------------------------------------------------------------//
278
+ // Remote Presence //
279
+ //----------------------------------------------------------------------------//
280
+ /**** #handleRemotePresence — updates the peer set and notifies handlers when a presence update arrives ****/
281
+ O = function(e, s) {
282
+ if (s == null) {
283
+ v(this, h, j).call(this, e);
284
+ return;
285
+ }
286
+ const i = { ...s, _lastSeen: Date.now() };
287
+ t(this, B).set(e, i), v(this, h, tt).call(this, e);
288
+ for (const o of t(this, m))
289
+ try {
290
+ o(e, s, "remote");
291
+ } catch (l) {
292
+ console.error("SDS: presence handler failed", l);
293
+ }
294
+ }, /**** #resetPeerTimeout — arms a timeout to remove a peer if no heartbeat arrives within PresenceTimeoutMs ****/
295
+ tt = function(e) {
296
+ const s = t(this, S).get(e);
297
+ s != null && clearTimeout(s);
298
+ const i = setTimeout(
299
+ () => {
300
+ v(this, h, j).call(this, e);
301
+ },
302
+ t(this, k)
303
+ );
304
+ t(this, S).set(e, i);
305
+ }, /**** #removePeer — removes a peer from the peer set and notifies presence change handlers ****/
306
+ j = function(e) {
307
+ if (!t(this, B).has(e))
308
+ return;
309
+ t(this, B).delete(e);
310
+ const s = t(this, S).get(e);
311
+ s != null && (clearTimeout(s), t(this, S).delete(e));
312
+ for (const i of t(this, m))
313
+ try {
314
+ i(e, void 0, "remote");
315
+ } catch (o) {
316
+ console.error("SDS: presence handler failed", o);
317
+ }
318
+ };
319
+ export {
320
+ at as SDS_SyncEngine
321
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@rozek/sds-sync-engine",
3
+ "description": "Coordinates persistence, network and presence for shareable-data-store",
4
+ "version": "0.0.1",
5
+ "author": "Andreas Rozek",
6
+ "homepage": "https://github.com/rozek/shareable-data-store#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/rozek/shareable-data-store.git",
10
+ "directory": "packages/sync-engine"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/rozek/shareable-data-store/issues"
14
+ },
15
+ "license": "MIT",
16
+ "type": "module",
17
+ "main": "./dist/sds-sync-engine.js",
18
+ "module": "./dist/sds-sync-engine.js",
19
+ "types": "./dist/sds-sync-engine.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "import": "./dist/sds-sync-engine.js",
23
+ "types": "./dist/sds-sync-engine.d.ts"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "peerDependencies": {
30
+ "@rozek/sds-core": "0.0.1"
31
+ },
32
+ "devDependencies": {
33
+ "typescript": "^5.7.2",
34
+ "vite": "^6.0.0",
35
+ "vite-plugin-dts": "^4.0.0",
36
+ "vitest": "^2.0.0",
37
+ "@rozek/sds-core-jj": "0.0.1",
38
+ "@rozek/sds-core": "0.0.1"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "scripts": {
44
+ "dev": "vite build --watch",
45
+ "build": "vite build",
46
+ "test": "vitest",
47
+ "test:run": "vitest run",
48
+ "typecheck": "tsc --noEmit"
49
+ }
50
+ }