@firtoz/collection-sync 4.0.0 → 6.0.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.
Files changed (130) hide show
  1. package/README.md +46 -0
  2. package/dist/cache-manager.d.ts +52 -0
  3. package/dist/cache-manager.js +5 -0
  4. package/dist/cache-manager.js.map +1 -0
  5. package/dist/chunk-3EHHMLSV.js +57 -0
  6. package/dist/chunk-3EHHMLSV.js.map +1 -0
  7. package/dist/chunk-43KYAIKY.js +46 -0
  8. package/dist/chunk-43KYAIKY.js.map +1 -0
  9. package/dist/chunk-4BEXLBCH.js +64 -0
  10. package/dist/chunk-4BEXLBCH.js.map +1 -0
  11. package/dist/chunk-5V6BSQAB.js +148 -0
  12. package/dist/chunk-5V6BSQAB.js.map +1 -0
  13. package/dist/chunk-5VMFQT5Z.js +112 -0
  14. package/dist/chunk-5VMFQT5Z.js.map +1 -0
  15. package/dist/chunk-6EHROJFY.js +111 -0
  16. package/dist/chunk-6EHROJFY.js.map +1 -0
  17. package/dist/chunk-6X3434GJ.js +21 -0
  18. package/dist/chunk-6X3434GJ.js.map +1 -0
  19. package/dist/chunk-BGJH6PH2.js +175 -0
  20. package/dist/chunk-BGJH6PH2.js.map +1 -0
  21. package/dist/chunk-BJJEAKXL.js +252 -0
  22. package/dist/chunk-BJJEAKXL.js.map +1 -0
  23. package/dist/chunk-GWIOC5CP.js +51 -0
  24. package/dist/chunk-GWIOC5CP.js.map +1 -0
  25. package/dist/chunk-HMLY7DHA.js +12 -0
  26. package/dist/chunk-HMLY7DHA.js.map +1 -0
  27. package/dist/chunk-I6RJWBGF.js +112 -0
  28. package/dist/chunk-I6RJWBGF.js.map +1 -0
  29. package/dist/chunk-M5MJHS6A.js +10 -0
  30. package/dist/chunk-M5MJHS6A.js.map +1 -0
  31. package/dist/chunk-O3KBDCEI.js +615 -0
  32. package/dist/chunk-O3KBDCEI.js.map +1 -0
  33. package/dist/chunk-OP53UBPN.js +19 -0
  34. package/dist/chunk-OP53UBPN.js.map +1 -0
  35. package/dist/chunk-P3JOTUAB.js +802 -0
  36. package/dist/chunk-P3JOTUAB.js.map +1 -0
  37. package/dist/chunk-QJP4GSJH.js +373 -0
  38. package/dist/chunk-QJP4GSJH.js.map +1 -0
  39. package/dist/chunk-RDDS7JQW.js +623 -0
  40. package/dist/chunk-RDDS7JQW.js.map +1 -0
  41. package/dist/chunk-TEH7V76G.js +209 -0
  42. package/dist/chunk-TEH7V76G.js.map +1 -0
  43. package/dist/chunk-UJ24XW52.js +20 -0
  44. package/dist/chunk-UJ24XW52.js.map +1 -0
  45. package/dist/chunk-UVZJL6QV.js +18 -0
  46. package/dist/chunk-UVZJL6QV.js.map +1 -0
  47. package/dist/chunk-XC4QNFSQ.js +238 -0
  48. package/dist/chunk-XC4QNFSQ.js.map +1 -0
  49. package/dist/chunk-YD5LVGWX.js +125 -0
  50. package/dist/chunk-YD5LVGWX.js.map +1 -0
  51. package/dist/chunk-YYGPIHHJ.js +166 -0
  52. package/dist/chunk-YYGPIHHJ.js.map +1 -0
  53. package/dist/connect-partial-sync.d.ts +41 -0
  54. package/dist/connect-partial-sync.js +6 -0
  55. package/dist/connect-partial-sync.js.map +1 -0
  56. package/dist/connect-sync.d.ts +26 -0
  57. package/dist/connect-sync.js +5 -0
  58. package/dist/connect-sync.js.map +1 -0
  59. package/dist/create-partial-synced-collection.d.ts +24 -0
  60. package/dist/create-partial-synced-collection.js +8 -0
  61. package/dist/create-partial-synced-collection.js.map +1 -0
  62. package/dist/create-synced-collection.d.ts +26 -0
  63. package/dist/create-synced-collection.js +8 -0
  64. package/dist/create-synced-collection.js.map +1 -0
  65. package/dist/index.d.ts +18 -0
  66. package/dist/index.js +18 -0
  67. package/dist/index.js.map +1 -0
  68. package/dist/partial-sync-client-bridge.d.ts +157 -0
  69. package/dist/partial-sync-client-bridge.js +6 -0
  70. package/dist/partial-sync-client-bridge.js.map +1 -0
  71. package/dist/partial-sync-interest.d.ts +48 -0
  72. package/dist/partial-sync-interest.js +6 -0
  73. package/dist/partial-sync-interest.js.map +1 -0
  74. package/dist/partial-sync-mutation-handler.d.ts +31 -0
  75. package/dist/partial-sync-mutation-handler.js +6 -0
  76. package/dist/partial-sync-mutation-handler.js.map +1 -0
  77. package/dist/partial-sync-predicate-match.d.ts +8 -0
  78. package/dist/partial-sync-predicate-match.js +4 -0
  79. package/dist/partial-sync-predicate-match.js.map +1 -0
  80. package/dist/partial-sync-row-key.d.ts +41 -0
  81. package/dist/partial-sync-row-key.js +4 -0
  82. package/dist/partial-sync-row-key.js.map +1 -0
  83. package/dist/partial-sync-server-bridge.d.ts +102 -0
  84. package/dist/partial-sync-server-bridge.js +8 -0
  85. package/dist/partial-sync-server-bridge.js.map +1 -0
  86. package/dist/react/constants.d.ts +12 -0
  87. package/dist/react/constants.js +4 -0
  88. package/dist/react/constants.js.map +1 -0
  89. package/dist/react/index.d.ts +19 -0
  90. package/dist/react/index.js +17 -0
  91. package/dist/react/index.js.map +1 -0
  92. package/dist/react/partial-sync-adapter.d.ts +40 -0
  93. package/dist/react/partial-sync-adapter.js +4 -0
  94. package/dist/react/partial-sync-adapter.js.map +1 -0
  95. package/dist/react/partial-sync-utils.d.ts +42 -0
  96. package/dist/react/partial-sync-utils.js +5 -0
  97. package/dist/react/partial-sync-utils.js.map +1 -0
  98. package/dist/react/range-conditions-expression.d.ts +49 -0
  99. package/dist/react/range-conditions-expression.js +4 -0
  100. package/dist/react/range-conditions-expression.js.map +1 -0
  101. package/dist/react/types.d.ts +196 -0
  102. package/dist/react/types.js +3 -0
  103. package/dist/react/types.js.map +1 -0
  104. package/dist/react/usePartialSyncCollection.d.ts +20 -0
  105. package/dist/react/usePartialSyncCollection.js +10 -0
  106. package/dist/react/usePartialSyncCollection.js.map +1 -0
  107. package/dist/react/usePartialSyncViewport.d.ts +20 -0
  108. package/dist/react/usePartialSyncViewport.js +7 -0
  109. package/dist/react/usePartialSyncViewport.js.map +1 -0
  110. package/dist/react/usePartialSyncWindow.d.ts +17 -0
  111. package/dist/react/usePartialSyncWindow.js +12 -0
  112. package/dist/react/usePartialSyncWindow.js.map +1 -0
  113. package/dist/react/usePredicateFilteredRows.d.ts +20 -0
  114. package/dist/react/usePredicateFilteredRows.js +6 -0
  115. package/dist/react/usePredicateFilteredRows.js.map +1 -0
  116. package/dist/sync-client-bridge.d.ts +48 -0
  117. package/dist/sync-client-bridge.js +6 -0
  118. package/dist/sync-client-bridge.js.map +1 -0
  119. package/dist/sync-protocol.d.ts +378 -0
  120. package/dist/sync-protocol.js +4 -0
  121. package/dist/sync-protocol.js.map +1 -0
  122. package/dist/sync-server-bridge.d.ts +35 -0
  123. package/dist/sync-server-bridge.js +6 -0
  124. package/dist/sync-server-bridge.js.map +1 -0
  125. package/dist/with-sync.d.ts +107 -0
  126. package/dist/with-sync.js +7 -0
  127. package/dist/with-sync.js.map +1 -0
  128. package/package.json +27 -21
  129. package/src/connect-partial-sync.ts +16 -12
  130. package/src/connect-sync.ts +12 -10
@@ -0,0 +1,623 @@
1
+ import { DEFAULT_SYNC_COLLECTION_ID } from './chunk-BJJEAKXL.js';
2
+ import { classifyPartialSyncRangePatch, filterSyncMessagesForPredicateRange } from './chunk-6EHROJFY.js';
3
+ import { defaultPredicateColumnValue } from './chunk-3EHHMLSV.js';
4
+ import { partialSyncRowKey, partialSyncRowVersionWatermarkMs } from './chunk-UJ24XW52.js';
5
+ import { __privateAdd, __privateSet, __privateGet, __privateMethod } from './chunk-HMLY7DHA.js';
6
+ import { exhaustiveGuard } from '@firtoz/maybe-error';
7
+
8
+ function deliveredRowIdKey(id) {
9
+ return String(partialSyncRowKey(id));
10
+ }
11
+ var _clientStates, _cid, _PartialSyncServerBridge_instances, emit_fn, resetClientInterest_fn, trackDeliveredRowIdsFromRows_fn, trackDeliveredRowIdsFromMessages_fn, applyPatchToDeliveredRowIds_fn, handleRangeQuery_fn, handleRangeReconcile_fn, collectRowsForRange_fn, syncInterestAfterReconcile_fn, handleQueryPredicate_fn, mergeDeliveredRangesFromChanges_fn, handleQueryRange_fn, handleQueryByOffset_fn, getOrCreateClientState_fn, trackDeliveredRange_fn, flushPendingPatches_fn;
12
+ var PartialSyncServerBridge = class {
13
+ constructor(options) {
14
+ this.options = options;
15
+ __privateAdd(this, _PartialSyncServerBridge_instances);
16
+ __privateAdd(this, _clientStates, /* @__PURE__ */ new Map());
17
+ __privateAdd(this, _cid);
18
+ __privateSet(this, _cid, options.collectionId ?? DEFAULT_SYNC_COLLECTION_ID);
19
+ }
20
+ get collectionId() {
21
+ return __privateGet(this, _cid);
22
+ }
23
+ async handleClientMessage(message) {
24
+ const mid = message.collectionId ?? DEFAULT_SYNC_COLLECTION_ID;
25
+ if (mid !== __privateGet(this, _cid)) return;
26
+ switch (message.type) {
27
+ case "ping":
28
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
29
+ type: "pong",
30
+ timestamp: message.timestamp
31
+ });
32
+ return;
33
+ case "queryRange":
34
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleQueryRange_fn).call(this, message);
35
+ return;
36
+ case "queryByOffset":
37
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleQueryByOffset_fn).call(this, message);
38
+ return;
39
+ case "rangeQuery":
40
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleRangeQuery_fn).call(this, message);
41
+ return;
42
+ case "rangeReconcile":
43
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleRangeReconcile_fn).call(this, message);
44
+ return;
45
+ case "syncHello":
46
+ case "mutateBatch":
47
+ return;
48
+ default:
49
+ exhaustiveGuard(message);
50
+ }
51
+ }
52
+ async pushServerChanges(changes, options) {
53
+ const exclude = options?.excludeClientId;
54
+ for (const state of __privateGet(this, _clientStates).values()) {
55
+ if (exclude !== void 0 && state.clientId === exclude) continue;
56
+ for (const change of changes) {
57
+ const patch = classifyPartialSyncRangePatch(
58
+ state.deliveredRanges,
59
+ state.predicateGroups,
60
+ change,
61
+ (row, column) => this.options.store.getSortValue(row, column),
62
+ (row, column) => defaultPredicateColumnValue(row, column),
63
+ { deliveredRowIds: state.deliveredRowIds }
64
+ );
65
+ if (patch === null) continue;
66
+ if (state.streaming) {
67
+ state.pendingPatches.push(patch);
68
+ continue;
69
+ }
70
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, state.clientId, {
71
+ type: "rangePatch",
72
+ change: patch.change,
73
+ ...patch.viewTransition !== void 0 ? { viewTransition: patch.viewTransition } : {}
74
+ });
75
+ __privateMethod(this, _PartialSyncServerBridge_instances, applyPatchToDeliveredRowIds_fn).call(this, state, patch);
76
+ }
77
+ }
78
+ }
79
+ /**
80
+ * Drop partial-sync interest for a disconnected client (prevents unbounded `#clientStates`
81
+ * growth and stale subscriptions).
82
+ */
83
+ removeClient(clientId) {
84
+ __privateGet(this, _clientStates).delete(clientId);
85
+ }
86
+ /**
87
+ * Replace predicate interest for a client (server-authoritative visibility). Clears sort-range
88
+ * tracking; the next `rangeQuery` should re-establish delivered ranges from fresh chunks.
89
+ */
90
+ setClientVisibility(clientId, conditions) {
91
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, clientId);
92
+ state.predicateGroups = [[...conditions]];
93
+ state.deliveredRanges.length = 0;
94
+ state.deliveredRowIds.clear();
95
+ }
96
+ getClientState(clientId) {
97
+ return __privateGet(this, _clientStates).get(clientId);
98
+ }
99
+ };
100
+ _clientStates = new WeakMap();
101
+ _cid = new WeakMap();
102
+ _PartialSyncServerBridge_instances = new WeakSet();
103
+ emit_fn = function(clientId, body) {
104
+ this.options.sendToClient(clientId, {
105
+ ...body,
106
+ collectionId: __privateGet(this, _cid)
107
+ });
108
+ };
109
+ resetClientInterest_fn = function(state) {
110
+ state.deliveredRanges.length = 0;
111
+ state.predicateGroups.length = 0;
112
+ state.pendingPatches.length = 0;
113
+ state.deliveredRowIds.clear();
114
+ };
115
+ trackDeliveredRowIdsFromRows_fn = function(state, rows) {
116
+ for (const row of rows) {
117
+ state.deliveredRowIds.add(deliveredRowIdKey(row.id));
118
+ }
119
+ };
120
+ trackDeliveredRowIdsFromMessages_fn = function(state, messages) {
121
+ for (const c of messages) {
122
+ if (c.type === "insert" || c.type === "update") {
123
+ state.deliveredRowIds.add(deliveredRowIdKey(c.value.id));
124
+ }
125
+ if (c.type === "delete") {
126
+ state.deliveredRowIds.delete(deliveredRowIdKey(c.key));
127
+ }
128
+ if (c.type === "truncate") {
129
+ state.deliveredRowIds.clear();
130
+ }
131
+ }
132
+ };
133
+ applyPatchToDeliveredRowIds_fn = function(state, patch) {
134
+ const ch = patch.change;
135
+ if (ch.type === "truncate") {
136
+ state.deliveredRowIds.clear();
137
+ return;
138
+ }
139
+ if (ch.type === "delete") {
140
+ state.deliveredRowIds.delete(deliveredRowIdKey(ch.key));
141
+ return;
142
+ }
143
+ if (ch.type === "insert") {
144
+ state.deliveredRowIds.add(deliveredRowIdKey(ch.value.id));
145
+ return;
146
+ }
147
+ if (ch.type === "update") {
148
+ const id = deliveredRowIdKey(ch.value.id);
149
+ if (patch.viewTransition === "exitView") {
150
+ state.deliveredRowIds.delete(id);
151
+ } else {
152
+ state.deliveredRowIds.add(id);
153
+ }
154
+ }
155
+ };
156
+ handleRangeQuery_fn = async function(message) {
157
+ const { clientId, requestId, fingerprint } = message;
158
+ let range = message.range;
159
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, clientId);
160
+ if (range.kind === "predicate") {
161
+ __privateMethod(this, _PartialSyncServerBridge_instances, resetClientInterest_fn).call(this, state);
162
+ } else {
163
+ state.predicateGroups.length = 0;
164
+ state.pendingPatches.length = 0;
165
+ }
166
+ if (range.kind === "predicate") {
167
+ const resolved = this.options.resolveClientVisibility !== void 0 ? await this.options.resolveClientVisibility(
168
+ clientId,
169
+ range.conditions
170
+ ) : range.conditions;
171
+ range = { ...range, conditions: resolved };
172
+ state.predicateGroups.push([...resolved]);
173
+ }
174
+ const rangeLimit = range.kind === "index" ? range.limit : range.limit ?? 200;
175
+ if (fingerprint !== void 0 && this.options.store.changesSince) {
176
+ const delta = await this.options.store.changesSince({
177
+ range,
178
+ sinceVersion: fingerprint.version,
179
+ chunkSize: Math.max(1, this.options.queryChunkSize ?? 200)
180
+ });
181
+ if (delta !== null) {
182
+ if (delta.changes.length === 0) {
183
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, clientId, {
184
+ type: "rangeUpToDate",
185
+ requestId,
186
+ totalCount: delta.totalCount
187
+ });
188
+ __privateMethod(this, _PartialSyncServerBridge_instances, mergeDeliveredRangesFromChanges_fn).call(this, state, range, delta.changes);
189
+ return;
190
+ }
191
+ const maxDelta = Math.max(1, Math.ceil(rangeLimit * 0.5));
192
+ if (delta.changes.length <= maxDelta) {
193
+ const filteredDelta = range.kind === "predicate" ? filterSyncMessagesForPredicateRange(
194
+ range.conditions,
195
+ delta.changes,
196
+ (row, column) => this.options.store.getSortValue(row, column),
197
+ defaultPredicateColumnValue
198
+ ) : delta.changes;
199
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, clientId, {
200
+ type: "rangeDelta",
201
+ requestId,
202
+ totalCount: delta.totalCount,
203
+ changes: filteredDelta
204
+ });
205
+ __privateMethod(this, _PartialSyncServerBridge_instances, mergeDeliveredRangesFromChanges_fn).call(this, state, range, filteredDelta);
206
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRowIdsFromMessages_fn).call(this, state, filteredDelta);
207
+ return;
208
+ }
209
+ }
210
+ }
211
+ if (range.kind === "index" && range.mode === "cursor") {
212
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleQueryRange_fn).call(this, {
213
+ type: "queryRange",
214
+ collectionId: __privateGet(this, _cid),
215
+ clientId,
216
+ requestId,
217
+ sort: range.sort,
218
+ limit: range.limit,
219
+ afterCursor: range.afterCursor
220
+ });
221
+ return;
222
+ }
223
+ if (range.kind === "index" && range.mode === "offset") {
224
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleQueryByOffset_fn).call(this, {
225
+ type: "queryByOffset",
226
+ collectionId: __privateGet(this, _cid),
227
+ clientId,
228
+ requestId,
229
+ sort: range.sort,
230
+ limit: range.limit,
231
+ offset: range.offset
232
+ });
233
+ return;
234
+ }
235
+ if (range.kind === "predicate") {
236
+ await __privateMethod(this, _PartialSyncServerBridge_instances, handleQueryPredicate_fn).call(this, message, range);
237
+ return;
238
+ }
239
+ exhaustiveGuard(range);
240
+ };
241
+ handleRangeReconcile_fn = async function(message) {
242
+ const { clientId, requestId, manifest } = message;
243
+ let range = message.range;
244
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, clientId);
245
+ if (range.kind === "predicate") {
246
+ __privateMethod(this, _PartialSyncServerBridge_instances, resetClientInterest_fn).call(this, state);
247
+ } else {
248
+ state.predicateGroups.length = 0;
249
+ state.pendingPatches.length = 0;
250
+ }
251
+ if (range.kind === "predicate") {
252
+ const resolved = this.options.resolveClientVisibility !== void 0 ? await this.options.resolveClientVisibility(
253
+ clientId,
254
+ range.conditions
255
+ ) : range.conditions;
256
+ range = { ...range, conditions: resolved };
257
+ state.predicateGroups.push([...resolved]);
258
+ }
259
+ const { rows, totalCount } = await __privateMethod(this, _PartialSyncServerBridge_instances, collectRowsForRange_fn).call(this, range);
260
+ const byKey = /* @__PURE__ */ new Map();
261
+ for (const row of rows) {
262
+ byKey.set(deliveredRowIdKey(row.id), row);
263
+ }
264
+ const manifestKeys = new Set(
265
+ manifest.map((m) => String(partialSyncRowKey(m.id)))
266
+ );
267
+ const added = [];
268
+ const updated = [];
269
+ const stale = [];
270
+ const movedHints = [];
271
+ for (const row of rows) {
272
+ const k = deliveredRowIdKey(row.id);
273
+ if (!manifestKeys.has(k)) {
274
+ added.push({ type: "insert", value: row });
275
+ }
276
+ }
277
+ for (const entry of manifest) {
278
+ const k = String(partialSyncRowKey(entry.id));
279
+ const serverRow = byKey.get(k);
280
+ if (serverRow === void 0) {
281
+ stale.push(entry.id);
282
+ const getRow = this.options.store.getRow;
283
+ const resolveMovedHint = this.options.resolveMovedHint;
284
+ if (getRow !== void 0 && resolveMovedHint !== void 0) {
285
+ const current = await getRow(entry.id);
286
+ if (current !== void 0) {
287
+ const hint = await resolveMovedHint(current, range);
288
+ if (hint !== null) {
289
+ movedHints.push({ id: entry.id, hint });
290
+ }
291
+ }
292
+ }
293
+ continue;
294
+ }
295
+ const serverV = partialSyncRowVersionWatermarkMs(serverRow);
296
+ if (serverV !== entry.version) {
297
+ updated.push({
298
+ type: "update",
299
+ value: serverRow,
300
+ previousValue: {
301
+ ...serverRow,
302
+ updatedAt: entry.version
303
+ }
304
+ });
305
+ }
306
+ }
307
+ __privateMethod(this, _PartialSyncServerBridge_instances, syncInterestAfterReconcile_fn).call(this, state, range, rows);
308
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, clientId, {
309
+ type: "rangeReconcileResult",
310
+ requestId,
311
+ added,
312
+ updated,
313
+ stale,
314
+ movedHints,
315
+ totalCount
316
+ });
317
+ };
318
+ collectRowsForRange_fn = async function(range) {
319
+ const chunkSize = Math.max(1, this.options.queryChunkSize ?? 200);
320
+ if (range.kind === "predicate") {
321
+ const queryByPredicate = this.options.store.queryByPredicate;
322
+ if (!queryByPredicate) {
323
+ return {
324
+ rows: [],
325
+ totalCount: await this.options.store.getTotalCount()
326
+ };
327
+ }
328
+ const limit = range.limit ?? chunkSize;
329
+ const totalCount = this.options.store.getPredicateCount ? await this.options.store.getPredicateCount(range.conditions) : await this.options.store.getTotalCount();
330
+ const rows = [];
331
+ for await (const chunk of queryByPredicate({
332
+ conditions: range.conditions,
333
+ sort: range.sort,
334
+ limit,
335
+ chunkSize
336
+ })) {
337
+ rows.push(...chunk);
338
+ if (rows.length >= limit) break;
339
+ }
340
+ return { rows: rows.slice(0, limit), totalCount };
341
+ }
342
+ if (range.kind === "index" && range.mode === "offset") {
343
+ const totalCount = await this.options.store.getTotalCount();
344
+ const rows = [];
345
+ for await (const chunk of this.options.store.queryByOffset({
346
+ sort: range.sort,
347
+ limit: range.limit,
348
+ offset: range.offset,
349
+ chunkSize
350
+ })) {
351
+ rows.push(...chunk);
352
+ if (rows.length >= range.limit) break;
353
+ }
354
+ return { rows: rows.slice(0, range.limit), totalCount };
355
+ }
356
+ if (range.kind === "index" && range.mode === "cursor") {
357
+ const totalCount = await this.options.store.getTotalCount();
358
+ const rows = [];
359
+ for await (const chunk of this.options.store.queryRange({
360
+ sort: range.sort,
361
+ limit: range.limit,
362
+ afterCursor: range.afterCursor,
363
+ chunkSize
364
+ })) {
365
+ rows.push(...chunk);
366
+ if (rows.length >= range.limit) break;
367
+ }
368
+ return { rows: rows.slice(0, range.limit), totalCount };
369
+ }
370
+ exhaustiveGuard(range);
371
+ };
372
+ syncInterestAfterReconcile_fn = function(state, range, rows) {
373
+ state.deliveredRowIds.clear();
374
+ for (const row of rows) {
375
+ state.deliveredRowIds.add(deliveredRowIdKey(row.id));
376
+ }
377
+ state.deliveredRanges.length = 0;
378
+ const sort = range.kind === "index" ? range.sort : range.kind === "predicate" ? range.sort : void 0;
379
+ if (sort !== void 0 && rows.length > 0) {
380
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRange_fn).call(this, state, sort, null, rows);
381
+ }
382
+ };
383
+ handleQueryPredicate_fn = async function(message, range) {
384
+ const queryByPredicate = this.options.store.queryByPredicate;
385
+ if (!queryByPredicate) {
386
+ const totalCount2 = await this.options.store.getTotalCount();
387
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
388
+ type: "queryRangeChunk",
389
+ requestId: message.requestId,
390
+ rows: [],
391
+ totalCount: totalCount2,
392
+ lastCursor: null,
393
+ hasMore: false,
394
+ chunkIndex: 0,
395
+ done: true
396
+ });
397
+ return;
398
+ }
399
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, message.clientId);
400
+ state.streaming = true;
401
+ const chunkSize = Math.max(1, this.options.queryChunkSize ?? 200);
402
+ const limit = range.limit ?? chunkSize;
403
+ const totalCount = this.options.store.getPredicateCount ? await this.options.store.getPredicateCount(range.conditions) : await this.options.store.getTotalCount();
404
+ const iterable = queryByPredicate({
405
+ conditions: range.conditions,
406
+ sort: range.sort,
407
+ limit,
408
+ chunkSize
409
+ });
410
+ let chunkIndex = 0;
411
+ let totalDelivered = 0;
412
+ let emittedAny = false;
413
+ const sortForTrack = range.sort;
414
+ for await (const rows of iterable) {
415
+ emittedAny = true;
416
+ totalDelivered += rows.length;
417
+ const reachedLimit = totalDelivered >= limit;
418
+ const likelyFinalChunk = rows.length < chunkSize || reachedLimit;
419
+ const isFinalChunk = likelyFinalChunk;
420
+ const lastRow = rows[rows.length - 1];
421
+ const lastCursor = lastRow === void 0 || sortForTrack === void 0 ? null : this.options.store.getSortValue(lastRow, sortForTrack.column);
422
+ const hasMoreForClient = isFinalChunk ? totalDelivered === limit && totalDelivered < totalCount : true;
423
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
424
+ type: "queryRangeChunk",
425
+ requestId: message.requestId,
426
+ rows,
427
+ totalCount,
428
+ lastCursor,
429
+ hasMore: hasMoreForClient,
430
+ chunkIndex,
431
+ done: isFinalChunk
432
+ });
433
+ if (sortForTrack !== void 0) {
434
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRange_fn).call(this, state, sortForTrack, null, rows);
435
+ }
436
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRowIdsFromRows_fn).call(this, state, rows);
437
+ chunkIndex += 1;
438
+ if (isFinalChunk) break;
439
+ }
440
+ if (!emittedAny) {
441
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
442
+ type: "queryRangeChunk",
443
+ requestId: message.requestId,
444
+ rows: [],
445
+ totalCount,
446
+ lastCursor: null,
447
+ hasMore: false,
448
+ chunkIndex,
449
+ done: true
450
+ });
451
+ }
452
+ state.streaming = false;
453
+ __privateMethod(this, _PartialSyncServerBridge_instances, flushPendingPatches_fn).call(this, state);
454
+ };
455
+ mergeDeliveredRangesFromChanges_fn = function(state, range, changes) {
456
+ const sort = range.kind === "index" ? range.sort : range.kind === "predicate" ? range.sort : void 0;
457
+ if (sort === void 0) return;
458
+ const rows = [];
459
+ for (const change of changes) {
460
+ if (change.type === "insert" || change.type === "update") {
461
+ rows.push(change.value);
462
+ }
463
+ }
464
+ if (rows.length === 0) return;
465
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRange_fn).call(this, state, sort, null, rows);
466
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRowIdsFromRows_fn).call(this, state, rows);
467
+ };
468
+ handleQueryRange_fn = async function(message) {
469
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, message.clientId);
470
+ state.streaming = true;
471
+ const totalCount = await this.options.store.getTotalCount();
472
+ const chunkSize = Math.max(1, this.options.queryChunkSize ?? 200);
473
+ const iterable = this.options.store.queryRange({
474
+ sort: message.sort,
475
+ limit: message.limit,
476
+ afterCursor: message.afterCursor,
477
+ chunkSize
478
+ });
479
+ let chunkIndex = 0;
480
+ let totalDelivered = 0;
481
+ let emittedAny = false;
482
+ for await (const rows of iterable) {
483
+ emittedAny = true;
484
+ totalDelivered += rows.length;
485
+ const reachedLimit = totalDelivered >= message.limit;
486
+ const likelyFinalChunk = rows.length < chunkSize || reachedLimit;
487
+ const isFinalChunk = likelyFinalChunk;
488
+ const lastRow = rows[rows.length - 1];
489
+ const lastCursor = lastRow === void 0 ? message.afterCursor : this.options.store.getSortValue(lastRow, message.sort.column);
490
+ const hasMoreForClient = isFinalChunk ? totalDelivered === message.limit && totalDelivered < totalCount : true;
491
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
492
+ type: "queryRangeChunk",
493
+ requestId: message.requestId,
494
+ rows,
495
+ totalCount,
496
+ lastCursor,
497
+ hasMore: hasMoreForClient,
498
+ chunkIndex,
499
+ done: isFinalChunk
500
+ });
501
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRange_fn).call(this, state, message.sort, message.afterCursor, rows);
502
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRowIdsFromRows_fn).call(this, state, rows);
503
+ chunkIndex += 1;
504
+ if (isFinalChunk) {
505
+ break;
506
+ }
507
+ }
508
+ if (!emittedAny) {
509
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
510
+ type: "queryRangeChunk",
511
+ requestId: message.requestId,
512
+ rows: [],
513
+ totalCount,
514
+ lastCursor: message.afterCursor,
515
+ hasMore: false,
516
+ chunkIndex,
517
+ done: true
518
+ });
519
+ }
520
+ state.streaming = false;
521
+ __privateMethod(this, _PartialSyncServerBridge_instances, flushPendingPatches_fn).call(this, state);
522
+ };
523
+ handleQueryByOffset_fn = async function(message) {
524
+ const state = __privateMethod(this, _PartialSyncServerBridge_instances, getOrCreateClientState_fn).call(this, message.clientId);
525
+ state.streaming = true;
526
+ const totalCount = await this.options.store.getTotalCount();
527
+ const chunkSize = Math.max(1, this.options.queryChunkSize ?? 200);
528
+ const iterable = this.options.store.queryByOffset({
529
+ sort: message.sort,
530
+ limit: message.limit,
531
+ offset: message.offset,
532
+ chunkSize
533
+ });
534
+ let chunkIndex = 0;
535
+ let totalDelivered = 0;
536
+ let emittedAny = false;
537
+ for await (const rows of iterable) {
538
+ emittedAny = true;
539
+ totalDelivered += rows.length;
540
+ const reachedLimit = totalDelivered >= message.limit;
541
+ const likelyFinalChunk = rows.length < chunkSize || reachedLimit;
542
+ const isFinalChunk = likelyFinalChunk;
543
+ const lastRow = rows[rows.length - 1];
544
+ const lastCursor = lastRow === void 0 ? null : this.options.store.getSortValue(lastRow, message.sort.column);
545
+ const hasMoreForClient = isFinalChunk ? totalDelivered === message.limit && message.offset + totalDelivered < totalCount : true;
546
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
547
+ type: "queryRangeChunk",
548
+ requestId: message.requestId,
549
+ rows,
550
+ totalCount,
551
+ lastCursor,
552
+ hasMore: hasMoreForClient,
553
+ chunkIndex,
554
+ done: isFinalChunk
555
+ });
556
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRange_fn).call(this, state, message.sort, null, rows);
557
+ __privateMethod(this, _PartialSyncServerBridge_instances, trackDeliveredRowIdsFromRows_fn).call(this, state, rows);
558
+ chunkIndex += 1;
559
+ if (isFinalChunk) {
560
+ break;
561
+ }
562
+ }
563
+ if (!emittedAny) {
564
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, message.clientId, {
565
+ type: "queryRangeChunk",
566
+ requestId: message.requestId,
567
+ rows: [],
568
+ totalCount,
569
+ lastCursor: null,
570
+ hasMore: false,
571
+ chunkIndex,
572
+ done: true
573
+ });
574
+ }
575
+ state.streaming = false;
576
+ __privateMethod(this, _PartialSyncServerBridge_instances, flushPendingPatches_fn).call(this, state);
577
+ };
578
+ getOrCreateClientState_fn = function(clientId) {
579
+ let state = __privateGet(this, _clientStates).get(clientId);
580
+ if (!state) {
581
+ state = {
582
+ clientId,
583
+ deliveredRanges: [],
584
+ predicateGroups: [],
585
+ deliveredRowIds: /* @__PURE__ */ new Set(),
586
+ pendingPatches: [],
587
+ streaming: false
588
+ };
589
+ __privateGet(this, _clientStates).set(clientId, state);
590
+ }
591
+ return state;
592
+ };
593
+ trackDeliveredRange_fn = function(state, sort, afterCursor, rows) {
594
+ if (rows.length === 0) return;
595
+ const firstValue = afterCursor ?? this.options.store.getSortValue(rows[0], sort.column);
596
+ const lastValue = this.options.store.getSortValue(
597
+ rows[rows.length - 1],
598
+ sort.column
599
+ );
600
+ const range = {
601
+ sortColumn: sort.column,
602
+ sortDirection: sort.direction,
603
+ fromValue: firstValue,
604
+ toValue: lastValue
605
+ };
606
+ state.deliveredRanges.push(range);
607
+ };
608
+ flushPendingPatches_fn = function(state) {
609
+ if (state.pendingPatches.length === 0) return;
610
+ for (const patch of state.pendingPatches) {
611
+ __privateMethod(this, _PartialSyncServerBridge_instances, emit_fn).call(this, state.clientId, {
612
+ type: "rangePatch",
613
+ change: patch.change,
614
+ ...patch.viewTransition !== void 0 ? { viewTransition: patch.viewTransition } : {}
615
+ });
616
+ __privateMethod(this, _PartialSyncServerBridge_instances, applyPatchToDeliveredRowIds_fn).call(this, state, patch);
617
+ }
618
+ state.pendingPatches.length = 0;
619
+ };
620
+
621
+ export { PartialSyncServerBridge };
622
+ //# sourceMappingURL=chunk-RDDS7JQW.js.map
623
+ //# sourceMappingURL=chunk-RDDS7JQW.js.map