@signaldb/core 2.0.0-beta.21 → 2.0.0-beta.22

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 (46) hide show
  1. package/README.md +3 -3
  2. package/dist/.vite/manifest.json +61 -59
  3. package/dist/AsyncDataAdapter.d.ts +15 -2
  4. package/dist/AutoFetchDataAdapter.d.ts +13 -2
  5. package/dist/Collection/Observer.d.ts +1 -1
  6. package/dist/Collection/index.d.ts +11 -12
  7. package/dist/DataAdapter.d.ts +19 -3
  8. package/dist/WorkerDataAdapterHost.d.ts +19 -6
  9. package/dist/index.cjs.js +4 -4
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.mjs +4 -4
  12. package/dist/index12.cjs.js +1 -1
  13. package/dist/index12.mjs +1 -1
  14. package/dist/index15.cjs.js +6 -2
  15. package/dist/index15.mjs +6 -2
  16. package/dist/index17.cjs.js +4 -2
  17. package/dist/index17.mjs +4 -3
  18. package/dist/index18.cjs.js +1 -0
  19. package/dist/index18.mjs +1 -1
  20. package/dist/index28.cjs.js +15 -5
  21. package/dist/index28.mjs +15 -5
  22. package/dist/index29.cjs.js +29 -16
  23. package/dist/index29.mjs +29 -16
  24. package/dist/index33.cjs.js +17 -49
  25. package/dist/index33.mjs +17 -49
  26. package/dist/index34.cjs.js +45 -445
  27. package/dist/index34.mjs +45 -445
  28. package/dist/index35.cjs.js +132 -65
  29. package/dist/index35.mjs +132 -65
  30. package/dist/index36.cjs.js +385 -531
  31. package/dist/index36.mjs +386 -533
  32. package/dist/index37.cjs.js +68 -17
  33. package/dist/index37.mjs +68 -17
  34. package/dist/index38.cjs.js +536 -319
  35. package/dist/index38.mjs +538 -321
  36. package/dist/index39.cjs.js +301 -466
  37. package/dist/index39.mjs +301 -466
  38. package/dist/index40.cjs.js +484 -0
  39. package/dist/index40.mjs +484 -0
  40. package/dist/index5.cjs.js +3 -3
  41. package/dist/index5.mjs +3 -3
  42. package/dist/index6.cjs.js +1 -0
  43. package/dist/index6.mjs +1 -1
  44. package/dist/types/StorageAdapter.d.ts +80 -0
  45. package/dist/utils/executeStorageQuery.d.ts +28 -0
  46. package/package.json +1 -1
package/dist/index36.mjs CHANGED
@@ -1,580 +1,433 @@
1
- import { applyQueryDelta, callWithDelta, canApplyQueryDelta, diffQueryResults, isEmptyQueryDelta } from "./index4.mjs";
2
- import randomId from "./index9.mjs";
1
+ import { callWithDelta, diffQueryResults, isEmptyQueryDelta } from "./index4.mjs";
3
2
  import deepClone from "./index18.mjs";
4
3
  import match from "./index19.mjs";
5
4
  import modify from "./index20.mjs";
6
- import { mergeChangesetIntoResult } from "./index25.mjs";
5
+ import incrementalQueryUpdate from "./index25.mjs";
7
6
  import queryId from "./index26.mjs";
8
- import batchOnNextTick from "./index35.mjs";
9
- //#region src/WorkerDataAdapter.ts
7
+ import executeStorageQuery from "./index35.mjs";
8
+ //#region src/AsyncDataAdapter.ts
10
9
  /**
11
- * Extracts the ids a selector names outright.
12
- * @param selector - The selector to inspect.
13
- * @returns The named ids, or `null` when the selector asks more than ids can answer.
10
+ * Carries the context needed to act on a failed query. The bare storage error
11
+ * on its own does not say which collection or selector produced it, which made
12
+ * the default `console.error` hook close to useless.
14
13
  */
15
- function selectorIds(selector) {
16
- if (selector == null || typeof selector !== "object") return null;
17
- const keys = Object.keys(selector);
18
- if (keys.length !== 1 || keys[0] !== "id") return null;
19
- const value = selector.id;
20
- if (value == null) return null;
21
- if (typeof value !== "object") return [value];
22
- const valueKeys = Object.keys(value);
23
- if (valueKeys.length !== 1 || valueKeys[0] !== "$in") return null;
24
- const inValues = value.$in;
25
- return Array.isArray(inValues) ? inValues : null;
14
+ var QueryError = class extends Error {
15
+ collectionName;
16
+ selector;
17
+ options;
18
+ attempts;
19
+ constructor(collectionName, selector, options, attempts, cause) {
20
+ const reason = cause instanceof Error ? cause.message : String(cause);
21
+ super(`Query on "${collectionName}" failed after ${attempts} attempt(s): ${reason}`);
22
+ this.name = "QueryError";
23
+ this.collectionName = collectionName;
24
+ this.selector = selector;
25
+ this.options = options;
26
+ this.attempts = attempts;
27
+ this.cause = cause;
28
+ }
29
+ };
30
+ /**
31
+ * Turns the item states a write produced into the upsert/delete split a query update needs.
32
+ *
33
+ * The two lists are not symmetric: an item that is still there after the write is described by its
34
+ * new state, while an item that is gone — removed, or given a new id — is described by the id it
35
+ * used to have and nothing else. Mixing the states from before and after a write into one list
36
+ * loses exactly that distinction.
37
+ * @template T - The type of the items.
38
+ * @param previousItems - The items as they were before the write.
39
+ * @param modifiedItems - The items as they are after it.
40
+ * @returns The changeset describing the write.
41
+ */
42
+ function toChangeset(previousItems, modifiedItems) {
43
+ const modifiedIds = new Set(modifiedItems.map((item) => item.id));
44
+ return {
45
+ upserts: modifiedItems,
46
+ deletes: previousItems.map((item) => item.id).filter((id) => !modifiedIds.has(id))
47
+ };
26
48
  }
27
- var WorkerDataAdapter = class WorkerDataAdapter {
28
- worker;
49
+ var DEFAULT_RETRY_ATTEMPTS = 3;
50
+ var defaultRetryDelay = (attempt) => 100 * 4 ** (attempt - 1);
51
+ var wait = (ms) => new Promise((resolve) => {
52
+ setTimeout(resolve, ms);
53
+ });
54
+ /**
55
+ * AsyncDataAdapter
56
+ * Combines WorkerDataAdapter + WorkerDataAdapterHost into a single, transport-free adapter.
57
+ * - Keeps the DataAdapter/CollectionBackend surface identical to the Worker version.
58
+ * - Executes queries and mutations directly against the provided StorageAdapter.
59
+ * - Preserves index-aware query optimization and push-style query updates to listeners.
60
+ */
61
+ var AsyncDataAdapter = class {
29
62
  options;
30
63
  id;
31
- isDisposed = false;
32
- workerReady;
33
- log = () => {};
34
- collectionReady = /* @__PURE__ */ new Map();
35
- batchExecutionHelpers = /* @__PURE__ */ new Map();
36
- queries = {};
37
- pendingRequests = /* @__PURE__ */ new Map();
38
- pendingWrites = /* @__PURE__ */ new Map();
39
- pendingWriteSeq = 0;
40
- pendingWriteVersions = /* @__PURE__ */ new Map();
41
- bumpPendingWriteVersion(collectionName) {
42
- const current = this.pendingWriteVersions.get(collectionName) ?? 0;
43
- this.pendingWriteVersions.set(collectionName, current + 1);
44
- }
45
- constructor(worker, options) {
46
- this.worker = worker;
64
+ onError;
65
+ retryAttempts;
66
+ retryDelay;
67
+ storageAdapters = /* @__PURE__ */ new Map();
68
+ storageAdapterReady = /* @__PURE__ */ new Map();
69
+ collectionIndices = /* @__PURE__ */ new Map();
70
+ queries = /* @__PURE__ */ new Map();
71
+ constructor(options) {
47
72
  this.options = options;
48
- this.id = this.options.id || "default-worker-data-adapter";
49
- if (this.options.log) this.log = this.options.log;
50
- this.workerReady = new Promise((resolve, reject) => {
51
- const timeoutId = setTimeout(() => {
52
- reject(/* @__PURE__ */ new Error("WorkerDataAdapter initialization timed out"));
53
- }, 5e3);
54
- this.resolveWorkerReady = () => {
55
- clearTimeout(timeoutId);
56
- resolve();
57
- };
73
+ this.id = options.id || "async-data-adapter";
74
+ this.onError = options.onError ?? ((error) => {
75
+ console.error(error);
58
76
  });
59
- this.worker.addEventListener("message", this.handleWorkerMessage);
77
+ this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
78
+ this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
60
79
  }
61
- resolveWorkerReady = () => {};
62
- handleWorkerMessage = (event) => {
63
- const message = event.data;
64
- if (message == null) return;
65
- if (message.workerId !== this.id) return;
66
- if (message.type === "ready") {
67
- this.resolveWorkerReady();
68
- return;
69
- }
70
- if (message.type === "response") {
71
- if (message.id == null) return;
72
- const pending = this.pendingRequests.get(message.id);
73
- if (!pending) return;
74
- this.pendingRequests.delete(message.id);
75
- this.log("response", message.data ?? message.error);
76
- if (message.error) pending.reject(message.error);
77
- else pending.resolve(message.data);
78
- return;
79
- }
80
- if (message.type === "queryUpdate") this.handleQueryUpdate(message.data, message.error ?? null);
81
- };
82
- handleQueryUpdate(data, error) {
83
- if (data == null) return;
84
- const { collectionName, qid, selector, options, state, items, delta } = data;
85
- if (collectionName == null) return;
86
- const collectionQueries = this.queries[collectionName];
87
- if (!collectionQueries) return;
88
- const id = qid ?? (selector === void 0 ? void 0 : queryId(selector, options));
89
- if (id == null) return;
90
- const query = collectionQueries.get(id);
91
- if (!query) return;
92
- this.log("queryUpdate", query.selector, query.options, state, data ?? error);
93
- let nextItems = items;
94
- let deltaToPublish;
95
- if (delta != null) {
96
- const canApply = canApplyQueryDelta(query.items, delta);
97
- if (!canApply || isEmptyQueryDelta(delta)) {
98
- if (state === query.state) return;
99
- this.updateQuery(collectionName, {
100
- selector: query.selector,
101
- options: query.options
102
- }, {
103
- state,
104
- error
105
- });
106
- const settled = collectionQueries.get(id);
107
- if (!settled) return;
108
- settled.stateChangeCallbacks.forEach((callback) => callWithDelta(callback, state, canApply ? delta : void 0));
109
- return;
110
- }
111
- const servedBefore = this.flattenPendingWrites(collectionName) == null ? null : this.servedResult(collectionName, query);
112
- nextItems = applyQueryDelta(query.items, delta);
113
- if (servedBefore == null) deltaToPublish = delta;
114
- else {
115
- this.updateQuery(collectionName, {
116
- selector: query.selector,
117
- options: query.options
118
- }, {
119
- state,
120
- error,
121
- items: nextItems
122
- });
123
- const stored = collectionQueries.get(id);
124
- if (!stored) return;
125
- const servedDelta = diffQueryResults(servedBefore, this.servedResult(collectionName, stored));
126
- if (isEmptyQueryDelta(servedDelta) && state === query.state) return;
127
- stored.stateChangeCallbacks.forEach((callback) => callWithDelta(callback, state, servedDelta));
128
- return;
80
+ createCollectionBackend(collection, indices) {
81
+ this.collectionIndices.set(collection.name, indices);
82
+ this.queries.set(collection.name, /* @__PURE__ */ new Map());
83
+ this.ensureStorageAdapter(collection.name);
84
+ const ready = (async () => {
85
+ try {
86
+ await this.setupStorage(collection.name, indices);
87
+ } catch (error) {
88
+ this.onError(error);
89
+ throw error;
129
90
  }
130
- }
131
- this.updateQuery(collectionName, {
132
- selector: query.selector,
133
- options: query.options
134
- }, {
135
- state,
136
- error,
137
- items: nextItems
138
- });
139
- const updated = collectionQueries.get(id);
140
- if (!updated) return;
141
- updated.stateChangeCallbacks.forEach((callback) => callWithDelta(callback, state, deltaToPublish));
142
- }
143
- async exec(method, collectionName, ...args) {
144
- await this.workerReady;
145
- if (method !== "isReady") {
146
- const collectionReady = this.collectionReady.get(collectionName);
147
- if (!collectionReady) throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
148
- await collectionReady;
149
- }
150
- if (this.isDisposed) throw new Error("WorkerDataAdapter is disposed");
151
- return new Promise((resolve, reject) => {
152
- const messageId = randomId();
153
- this.pendingRequests.set(messageId, {
154
- resolve,
155
- reject
91
+ })();
92
+ this.storageAdapterReady.set(collection.name, ready);
93
+ const registerQuery = (selector, options) => {
94
+ const qid = queryId(selector, options);
95
+ const registry = this.queries.get(collection.name);
96
+ if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
97
+ registry.set(qid, {
98
+ selector,
99
+ options,
100
+ items: [],
101
+ listeners: /* @__PURE__ */ new Set(),
102
+ ...registry.get(qid),
103
+ state: "active",
104
+ error: null
156
105
  });
157
- this.worker.postMessage({
158
- id: messageId,
159
- workerId: this.id,
160
- method,
161
- args: [collectionName, ...args]
106
+ this.fulfillQuery(collection.name, selector, options).catch(this.onError);
107
+ };
108
+ const unregisterQuery = (selector, options) => {
109
+ const qid = queryId(selector, options);
110
+ this.queries.get(collection.name)?.delete(qid);
111
+ };
112
+ const getQueryState = (selector, options) => {
113
+ return (this.queries.get(collection.name)?.get(queryId(selector, options)))?.state ?? "active";
114
+ };
115
+ const getQueryError = (selector, options) => {
116
+ return (this.queries.get(collection.name)?.get(queryId(selector, options)))?.error ?? null;
117
+ };
118
+ const getQueryResult = (selector, options) => {
119
+ return (this.queries.get(collection.name)?.get(queryId(selector, options)))?.items ?? [];
120
+ };
121
+ const retryQuery = (selector, options) => {
122
+ const qid = queryId(selector, options);
123
+ if (!this.queries.get(collection.name)?.get(qid)) return;
124
+ this.publishState(collection.name, qid, "active", null);
125
+ this.runQuery(collection.name, selector, options);
126
+ };
127
+ const onQueryStateChange = (selector, options, callback) => {
128
+ const qid = queryId(selector, options);
129
+ const registry = this.queries.get(collection.name);
130
+ if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
131
+ if (!registry.has(qid)) registry.set(qid, {
132
+ selector,
133
+ options,
134
+ state: "active",
135
+ error: null,
136
+ items: [],
137
+ listeners: /* @__PURE__ */ new Set()
162
138
  });
163
- });
164
- }
165
- /**
166
- * Issues a call whose result nobody is waiting for, and makes sure a failure has somewhere to
167
- * go. A bare rejection here would surface as an uncaught error — which is what a disposed
168
- * collection produced every time a cursor was cleaned up after it.
169
- * @param method - The method to call on the worker.
170
- * @param collectionName - The collection it applies to.
171
- * @param args - The remaining arguments.
172
- * @param onError - Called when the call fails, in place of merely logging it.
173
- */
174
- execInBackground(method, collectionName, args = [], onError) {
175
- this.exec(method, collectionName, ...args).catch((error) => {
176
- if (onError) {
177
- onError(error);
178
- return;
139
+ registry.get(qid)?.listeners.add(callback);
140
+ if (registry.get(qid)?.state === "error") retryQuery(selector, options);
141
+ return () => registry.get(qid)?.listeners.delete(callback);
142
+ };
143
+ return {
144
+ insert: async (item) => {
145
+ await ready;
146
+ return await this.insert(collection.name, item);
147
+ },
148
+ updateOne: async (selector, modifier) => {
149
+ await ready;
150
+ return this.updateOne(collection.name, selector, modifier);
151
+ },
152
+ updateMany: async (selector, modifier) => {
153
+ await ready;
154
+ return this.updateMany(collection.name, selector, modifier);
155
+ },
156
+ replaceOne: async (selector, replacement) => {
157
+ await ready;
158
+ return this.replaceOne(collection.name, selector, replacement);
159
+ },
160
+ removeOne: async (selector) => {
161
+ await ready;
162
+ return this.removeOne(collection.name, selector);
163
+ },
164
+ removeMany: async (selector) => {
165
+ await ready;
166
+ return this.removeMany(collection.name, selector);
167
+ },
168
+ registerQuery,
169
+ unregisterQuery,
170
+ retryQuery,
171
+ getQueryState,
172
+ getQueryError,
173
+ getQueryResult,
174
+ onQueryStateChange,
175
+ executeQuery: async (selector, options) => {
176
+ await ready;
177
+ return this.executeQuery(collection.name, selector, options);
178
+ },
179
+ dispose: async () => {
180
+ this.storageAdapters.delete(collection.name);
181
+ this.queries.delete(collection.name);
182
+ this.collectionIndices.delete(collection.name);
183
+ this.storageAdapterReady.delete(collection.name);
184
+ },
185
+ isReady: async () => {
186
+ await ready;
179
187
  }
180
- this.log(method, "failed", error);
181
- });
182
- }
183
- queryItemsById(query) {
184
- if (!query.itemsById) query.itemsById = new Map(query.items.map((item) => [item.id, item]));
185
- return query.itemsById;
186
- }
187
- flattenPendingWrites(collectionName) {
188
- const state = this.pendingWrites.get(collectionName);
189
- if (!state || state.writes.size === 0) return null;
190
- return state.flat;
191
- }
192
- static pushPendingEntry(state, id, item, seq) {
193
- const stack = state.byId.get(id);
194
- if (stack) stack.push({
195
- seq,
196
- item
197
- });
198
- else state.byId.set(id, [{
199
- seq,
200
- item
201
- }]);
202
- WorkerDataAdapter.writeFlatEntry(state, id, item);
203
- }
204
- static dropPendingEntry(state, id, seq) {
205
- const stack = state.byId.get(id);
206
- if (!stack) return;
207
- const index = stack.findIndex((entry) => entry.seq === seq);
208
- if (index !== -1) stack.splice(index, 1);
209
- const top = stack.at(-1);
210
- if (!top) {
211
- state.byId.delete(id);
212
- state.flat.upserts.delete(id);
213
- state.flat.deletes.delete(id);
214
- return;
215
- }
216
- WorkerDataAdapter.writeFlatEntry(state, id, top.item);
188
+ };
217
189
  }
218
- static writeFlatEntry(state, id, item) {
219
- if (item === null) {
220
- state.flat.deletes.add(id);
221
- state.flat.upserts.delete(id);
222
- return;
223
- }
224
- state.flat.upserts.set(id, item);
225
- state.flat.deletes.delete(id);
190
+ async setupStorage(collectionName, indices) {
191
+ const storage = this.storageAdapters.get(collectionName);
192
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
193
+ await storage.setup();
194
+ await Promise.all(indices.map((index) => storage.createIndex(index)));
226
195
  }
227
- static providesFullItems(query) {
228
- return query.options?.fields == null;
196
+ ensureStorageAdapter(name) {
197
+ if (this.storageAdapters.has(name)) return;
198
+ const adapter = this.options.storage(name);
199
+ if (!adapter) return;
200
+ this.storageAdapters.set(name, adapter);
229
201
  }
230
- observableItems(collectionName) {
231
- const byId = /* @__PURE__ */ new Map();
232
- this.queries[collectionName]?.forEach((query) => {
233
- if (!WorkerDataAdapter.providesFullItems(query)) return;
234
- query.items.forEach((item) => byId.set(item.id, item));
235
- });
236
- const pending = this.flattenPendingWrites(collectionName);
237
- if (pending) {
238
- pending.upserts.forEach((item, id) => byId.set(id, item));
239
- pending.deletes.forEach((id) => byId.delete(id));
240
- }
241
- return [...byId.values()];
202
+ /**
203
+ * Compute and publish the result for a specific query
204
+ * @param collectionName - name of the collection
205
+ * @param selector - query selector
206
+ * @param options - query options
207
+ */
208
+ async fulfillQuery(collectionName, selector, options) {
209
+ const qid = queryId(selector, options);
210
+ const registry = this.queries.get(collectionName);
211
+ if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
212
+ if (!registry.get(qid)) return;
213
+ this.publishState(collectionName, qid, "active", null);
214
+ await this.runQuery(collectionName, selector, options);
242
215
  }
243
- observableItemsByIds(collectionName, ids) {
244
- const pending = this.flattenPendingWrites(collectionName);
245
- const found = /* @__PURE__ */ new Map();
246
- ids.forEach((id) => {
247
- if (pending?.deletes.has(id)) return;
248
- const pendingItem = pending?.upserts.get(id);
249
- if (pendingItem) {
250
- found.set(id, pendingItem);
216
+ /**
217
+ * Executes a query, retrying transient failures before giving up. The state
218
+ * stays `'active'` across retries consumers should see "still loading",
219
+ * not "failed", until we actually stop trying. Only the final failure is
220
+ * published as `'error'` and reported through `onError`; previously that
221
+ * error was swallowed entirely (`fulfillQuery` caught it internally, so the
222
+ * `.catch(this.onError)` on its call site was unreachable) and the query
223
+ * stayed dead for the rest of the session.
224
+ * @param collectionName - name of the collection
225
+ * @param selector - query selector
226
+ * @param options - query options
227
+ */
228
+ async runQuery(collectionName, selector, options) {
229
+ const qid = queryId(selector, options);
230
+ let lastError;
231
+ for (let attempt = 1; attempt <= this.retryAttempts; attempt += 1) {
232
+ if (!this.queries.get(collectionName)?.has(qid)) return;
233
+ try {
234
+ const items = await this.executeQuery(collectionName, selector, options);
235
+ const rec = this.queries.get(collectionName)?.get(qid);
236
+ const delta = rec?.answered ? diffQueryResults(rec.items, items) : void 0;
237
+ this.publishResult(collectionName, qid, items);
238
+ this.publishState(collectionName, qid, "complete", null, delta);
251
239
  return;
240
+ } catch (error) {
241
+ lastError = error;
242
+ if (attempt < this.retryAttempts) await wait(this.retryDelay(attempt));
252
243
  }
253
- const queries = this.queries[collectionName];
254
- if (!queries) return;
255
- for (const query of queries.values()) {
256
- if (!WorkerDataAdapter.providesFullItems(query)) continue;
257
- const item = this.queryItemsById(query).get(id);
258
- if (item) {
259
- found.set(id, item);
260
- return;
261
- }
262
- }
263
- });
264
- return [...found.values()];
244
+ }
245
+ const queryError = new QueryError(collectionName, selector, options, this.retryAttempts, lastError);
246
+ this.publishState(collectionName, qid, "error", queryError);
247
+ this.onError(queryError);
265
248
  }
266
- servedResult(collectionName, query) {
267
- const pendingVersion = this.pendingWriteVersions.get(collectionName) ?? 0;
268
- if (query.served && query.served.fromItems === query.items && query.served.pendingVersion === pendingVersion) return query.served.items;
269
- const items = this.advanceServedResult(collectionName, query, pendingVersion) ?? this.computeServedResult(collectionName, query);
270
- query.served = {
271
- items,
272
- fromItems: query.items,
273
- pendingVersion
274
- };
275
- return items;
249
+ /**
250
+ * Notify listeners about state changes and keep the cache updated
251
+ * @param collectionName - name of the collection
252
+ * @param qid - query id
253
+ * @param state - new state
254
+ * @param error - error if state is 'error', null otherwise
255
+ * @param delta - what changed about the result, when that is known
256
+ */
257
+ publishState(collectionName, qid, state, error, delta) {
258
+ const rec = this.queries.get(collectionName)?.get(qid);
259
+ if (!rec) return;
260
+ rec.state = state;
261
+ rec.error = error;
262
+ const subscribers = [...rec.listeners];
263
+ for (const callback of subscribers) try {
264
+ callWithDelta(callback, state, delta);
265
+ } catch (error_) {
266
+ this.onError(error_);
267
+ }
276
268
  }
277
- advanceServedResult(collectionName, query, pendingVersion) {
278
- const served = query.served;
279
- if (!served || served.fromItems !== query.items) return null;
280
- if (served.pendingVersion !== pendingVersion - 1) return null;
281
- if (!WorkerDataAdapter.providesFullItems(query) || query.options?.limit != null) return null;
282
- const state = this.pendingWrites.get(collectionName);
283
- if (state?.lastChange == null || state.lastChange.version !== pendingVersion) return null;
284
- return mergeChangesetIntoResult(served.items, query.selector, query.options, this.changesetForIds(collectionName, query, state.lastChange.ids));
269
+ publishResult(collectionName, qid, items) {
270
+ const rec = this.queries.get(collectionName)?.get(qid);
271
+ if (!rec) return;
272
+ rec.items = items;
273
+ rec.itemIds = void 0;
274
+ rec.answered = true;
285
275
  }
286
- changesetForIds(collectionName, query, ids) {
287
- const pending = this.flattenPendingWrites(collectionName);
288
- const stored = this.queryItemsById(query);
289
- const upserts = [];
290
- const deletes = [];
291
- ids.forEach((id) => {
292
- const pendingItem = pending?.upserts.get(id);
293
- if (pendingItem) {
294
- upserts.push(pendingItem);
295
- return;
296
- }
297
- if (pending?.deletes.has(id)) {
298
- deletes.push(id);
299
- return;
300
- }
301
- const storedItem = stored.get(id);
302
- if (storedItem) upserts.push(storedItem);
303
- else deletes.push(id);
304
- });
305
- return {
306
- upserts,
307
- deletes
308
- };
309
- }
310
- computeServedResult(collectionName, query) {
311
- const pending = this.flattenPendingWrites(collectionName);
312
- if (!pending) return query.items;
313
- const byId = this.queryItemsById(query);
314
- let affected = false;
315
- pending.deletes.forEach((id) => {
316
- if (byId.has(id)) affected = true;
317
- });
318
- if (!affected) pending.upserts.forEach((item, id) => {
319
- if (affected) return;
320
- if (byId.has(id)) affected = true;
321
- else if (query.selector != null && match(item, query.selector)) affected = true;
322
- });
323
- if (!affected) return query.items;
324
- return mergeChangesetIntoResult(query.items, query.selector, query.options, {
325
- upserts: [...pending.upserts.values()],
326
- deletes: [...pending.deletes]
327
- });
276
+ queryItemIds(rec) {
277
+ if (!rec.itemIds) rec.itemIds = new Set(rec.items.map((item) => item.id));
278
+ return rec.itemIds;
328
279
  }
329
280
  /**
330
- * Registers a write's effect locally and notifies every active query it
331
- * touches, then returns a function that drops it again once the write
332
- * settles.
333
- * @param collectionName - The collection the write targets.
334
- * @param upserts - Items inserted or updated by the write.
335
- * @param deletes - Ids removed by the write.
336
- * @returns A function that drops the pending write and re-notifies.
281
+ * Reads one query's result from the storage adapter.
282
+ *
283
+ * The work lives in `executeStorageQuery` because this method used to exist
284
+ * three times over here, in `AutoFetchDataAdapter` and in
285
+ * `WorkerDataAdapterHost` as the same eight lines, which is how a storage
286
+ * adapter capability ends up honoured by one adapter and silently ignored by
287
+ * the others.
288
+ * @template T - The type of the items.
289
+ * @template I - The type of the item ids.
290
+ * @param collectionName - The collection to read from.
291
+ * @param selector - The query's selector.
292
+ * @param options - The query's sort, window and projection.
293
+ * @returns The query result.
337
294
  */
338
- applyPendingWrite(collectionName, upserts, deletes) {
339
- if (upserts.length === 0 && deletes.length === 0) return () => {};
340
- const affectedIds = new Set([...upserts.map((item) => item.id), ...deletes]);
341
- const affected = this.affectedQueries(collectionName, upserts, affectedIds);
342
- const servedBefore = this.servedResults(collectionName, affected);
343
- const seq = this.pendingWriteSeq += 1;
344
- const state = this.pendingWrites.get(collectionName) ?? {
345
- writes: /* @__PURE__ */ new Map(),
346
- byId: /* @__PURE__ */ new Map(),
347
- flat: {
348
- upserts: /* @__PURE__ */ new Map(),
349
- deletes: /* @__PURE__ */ new Set()
350
- },
351
- lastChange: null
352
- };
353
- const write = {
354
- upserts: new Map(upserts.map((item) => [item.id, item])),
355
- deletes: new Set(deletes)
356
- };
357
- state.writes.set(seq, write);
358
- write.upserts.forEach((item, id) => WorkerDataAdapter.pushPendingEntry(state, id, item, seq));
359
- write.deletes.forEach((id) => WorkerDataAdapter.pushPendingEntry(state, id, null, seq));
360
- this.pendingWrites.set(collectionName, state);
361
- this.bumpPendingWriteVersion(collectionName);
362
- state.lastChange = {
363
- version: this.pendingWriteVersions.get(collectionName) ?? 0,
364
- ids: [...affectedIds]
365
- };
366
- this.notifyWithDeltas(collectionName, affected, servedBefore);
367
- return () => {
368
- const current = this.pendingWrites.get(collectionName);
369
- if (!current) return;
370
- const settled = current.writes.get(seq);
371
- if (!settled) return;
372
- const affectedOnDrop = this.affectedQueries(collectionName, upserts, affectedIds);
373
- const beforeDrop = this.servedResults(collectionName, affectedOnDrop);
374
- current.writes.delete(seq);
375
- settled.upserts.forEach((item, id) => WorkerDataAdapter.dropPendingEntry(current, id, seq));
376
- settled.deletes.forEach((id) => WorkerDataAdapter.dropPendingEntry(current, id, seq));
377
- if (current.writes.size === 0) this.pendingWrites.delete(collectionName);
378
- this.bumpPendingWriteVersion(collectionName);
379
- if (current.writes.size > 0) current.lastChange = {
380
- version: this.pendingWriteVersions.get(collectionName) ?? 0,
381
- ids: [...affectedIds]
382
- };
383
- this.notifyWithDeltas(collectionName, affectedOnDrop, beforeDrop);
384
- };
295
+ async executeQuery(collectionName, selector, options) {
296
+ const storageAdapter = this.storageAdapters.get(collectionName);
297
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
298
+ return executeStorageQuery(storageAdapter, this.collectionIndices.get(collectionName) ?? [], selector, options);
385
299
  }
386
- affectedQueries(collectionName, upserts, affectedIds) {
387
- const affected = [];
388
- this.queries[collectionName]?.forEach((query) => {
389
- const byId = this.queryItemsById(query);
390
- let wasHolding = false;
391
- affectedIds.forEach((id) => {
392
- if (byId.has(id)) wasHolding = true;
393
- });
394
- const nowMatches = upserts.some((item) => query.selector != null && match(item, query.selector));
395
- if (!wasHolding && !nowMatches) return;
396
- affected.push(query);
300
+ /**
301
+ * After mutations, bring every affected active query up to date.
302
+ *
303
+ * A query whose previous result is enough to answer the change is brought up to date from that
304
+ * result alone — no round trip to the storage, and no detour through `'active'`, because there is
305
+ * no window in which the query is stale. Only a query the change cannot be reasoned about
306
+ * locally — a window onto a larger set, or one that has never been answered — goes back to the
307
+ * store, and that one gets the same retry and reporting behaviour a freshly registered query
308
+ * gets: a refresh that fails silently leaves exactly the same dead cursor.
309
+ * @param collectionName - name of the collection
310
+ * @param changes - the items the write created, updated or removed
311
+ */
312
+ async checkQueryUpdates(collectionName, changes) {
313
+ const registry = this.queries.get(collectionName);
314
+ if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
315
+ if (registry.size === 0) return;
316
+ if (changes.upserts.length === 0 && changes.deletes.length === 0) return;
317
+ const affected = [...registry.values()].filter((rec) => {
318
+ const ids = this.queryItemIds(rec);
319
+ if (changes.deletes.some((id) => ids.has(id))) return true;
320
+ return changes.upserts.some((item) => ids.has(item.id) || match(item, rec.selector));
397
321
  });
398
- return affected;
399
- }
400
- servedResults(collectionName, queries) {
401
- return new Map(queries.map((query) => [query, this.servedResult(collectionName, query)]));
322
+ if (affected.length === 0) return;
323
+ const needsReExecution = [];
324
+ for (const rec of affected) {
325
+ const { selector, options } = rec;
326
+ const incremental = rec.answered ? incrementalQueryUpdate(rec.items, selector, options, changes) : null;
327
+ if (incremental == null) {
328
+ needsReExecution.push(rec);
329
+ continue;
330
+ }
331
+ const qid = queryId(selector, options);
332
+ const delta = diffQueryResults(rec.items, incremental);
333
+ if (isEmptyQueryDelta(delta)) continue;
334
+ this.publishResult(collectionName, qid, incremental);
335
+ this.publishState(collectionName, qid, "complete", null, delta);
336
+ }
337
+ for (const { selector, options } of needsReExecution) this.publishState(collectionName, queryId(selector, options), "active", null);
338
+ await Promise.all(needsReExecution.map(({ selector, options }) => this.runQuery(collectionName, selector, options)));
402
339
  }
403
- notifyWithDeltas(collectionName, queries, servedBefore) {
404
- queries.forEach((query) => {
405
- const before = servedBefore.get(query);
406
- if (before == null) return;
407
- const delta = diffQueryResults(before, this.servedResult(collectionName, query));
408
- if (isEmptyQueryDelta(delta)) return;
409
- query.stateChangeCallbacks.forEach((callback) => callWithDelta(callback, query.state, delta));
340
+ async insert(collectionName, newItem) {
341
+ const storage = this.storageAdapters.get(collectionName);
342
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
343
+ if ((await storage.readIds([newItem.id])).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
344
+ await storage.insert([newItem]);
345
+ await this.checkQueryUpdates(collectionName, {
346
+ upserts: [newItem],
347
+ deletes: []
410
348
  });
349
+ return newItem;
411
350
  }
412
- matchObservableItems(collectionName, selector, onlyFirst) {
413
- if (selector == null) return [];
414
- const ids = selectorIds(selector);
415
- const matches = ids == null ? this.observableItems(collectionName).filter((item) => match(item, selector)) : this.observableItemsByIds(collectionName, ids);
416
- return onlyFirst ? matches.slice(0, 1) : matches;
417
- }
418
- resolveUpdate(collectionName, selector, modifier, onlyFirst) {
419
- const { $setOnInsert, ...restModifier } = modifier;
420
- const upserts = [];
421
- const deletes = [];
422
- this.matchObservableItems(collectionName, selector, onlyFirst).forEach((item) => {
423
- const modifiedItem = modify(deepClone(item), restModifier);
424
- upserts.push(modifiedItem);
425
- if (modifiedItem.id !== item.id) deletes.push(item.id);
426
- });
351
+ async updateOne(collectionName, selector, modifier) {
352
+ const storage = this.storageAdapters.get(collectionName);
353
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
354
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
355
+ const { $setOnInsert, ...rest } = modifier;
356
+ if (item == null) return [];
357
+ const modified = modify(deepClone(item), rest);
358
+ if (item.id !== modified.id) {
359
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
360
+ }
361
+ await storage.replace([modified]);
362
+ await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
427
363
  return {
428
- upserts,
429
- deletes
364
+ items: [modified],
365
+ previousItems: [item]
430
366
  };
431
367
  }
432
- resolveRemoval(collectionName, selector, onlyFirst) {
433
- return this.matchObservableItems(collectionName, selector, onlyFirst).map((item) => item.id);
368
+ async updateMany(collectionName, selector, modifier) {
369
+ const storage = this.storageAdapters.get(collectionName);
370
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
371
+ const items = await this.executeQuery(collectionName, selector);
372
+ if (items.length === 0) return [];
373
+ const { $setOnInsert, ...rest } = modifier;
374
+ const changed = await Promise.all(items.map(async (item) => {
375
+ const modified = modify(deepClone(item), rest);
376
+ if (item.id !== modified.id) {
377
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
378
+ }
379
+ return modified;
380
+ }));
381
+ await storage.replace(changed);
382
+ await this.checkQueryUpdates(collectionName, toChangeset(items, changed));
383
+ return {
384
+ items: changed,
385
+ previousItems: items
386
+ };
434
387
  }
435
- resolveReplacement(item, replacement) {
436
- const modifiedItem = {
388
+ async replaceOne(collectionName, selector, replacement) {
389
+ const storage = this.storageAdapters.get(collectionName);
390
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
391
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
392
+ if (item == null) return [];
393
+ const modified = {
437
394
  ...replacement,
438
395
  id: replacement.id ?? item.id
439
396
  };
397
+ if (item.id !== modified.id) {
398
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
399
+ }
400
+ await storage.replace([modified]);
401
+ await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
440
402
  return {
441
- upserts: [modifiedItem],
442
- deletes: modifiedItem.id === item.id ? [] : [item.id]
403
+ items: [modified],
404
+ previousItems: [item]
443
405
  };
444
406
  }
445
- async withPendingWrite(collectionName, delta, run) {
446
- const dropPendingWrite = this.applyPendingWrite(collectionName, delta.upserts, delta.deletes);
447
- try {
448
- return await run();
449
- } finally {
450
- dropPendingWrite();
451
- }
452
- }
453
- enqueueBatched(collectionName, method, args) {
454
- const helper = this.batchExecutionHelpers.get(collectionName);
455
- if (!helper) throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
456
- return helper.enqueue(method, args);
457
- }
458
- updateQuery(collectionName, query, update) {
459
- const id = queryId(query.selector, query.options);
460
- const collectionQueries = this.queries[collectionName];
461
- if (!collectionQueries) return;
462
- const existing = collectionQueries.get(id);
463
- const newState = {
464
- selector: query.selector,
465
- options: query.options,
466
- state: "active",
467
- error: null,
468
- stateChangeCallbacks: [],
469
- ...existing,
470
- ...update,
471
- ...update.items ? {
472
- items: update.items,
473
- itemsById: void 0
474
- } : { items: existing?.items ?? [] }
475
- };
476
- collectionQueries.set(id, newState);
477
- this.queries[collectionName] = collectionQueries;
407
+ async removeOne(collectionName, selector) {
408
+ const storage = this.storageAdapters.get(collectionName);
409
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
410
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
411
+ if (item == null) return [];
412
+ await storage.remove([item]);
413
+ await this.checkQueryUpdates(collectionName, {
414
+ upserts: [],
415
+ deletes: [item.id]
416
+ });
417
+ return [item];
478
418
  }
479
- createCollectionBackend(collection, indices) {
480
- this.queries[collection.name] = /* @__PURE__ */ new Map();
481
- this.execInBackground("registerCollection", collection.name, [indices]);
482
- this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
483
- this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
484
- return {
485
- insert: async (item) => {
486
- return this.withPendingWrite(collection.name, {
487
- upserts: [item],
488
- deletes: []
489
- }, () => this.enqueueBatched(collection.name, "insert", [item]));
490
- },
491
- updateOne: async (selector, modifier) => {
492
- return this.withPendingWrite(collection.name, this.resolveUpdate(collection.name, selector, modifier, true), () => this.enqueueBatched(collection.name, "updateOne", [selector, modifier]));
493
- },
494
- updateMany: async (selector, modifier) => {
495
- return this.withPendingWrite(collection.name, this.resolveUpdate(collection.name, selector, modifier, false), () => this.enqueueBatched(collection.name, "updateMany", [selector, modifier]));
496
- },
497
- replaceOne: async (selector, replacement) => {
498
- const [item] = this.matchObservableItems(collection.name, selector, true);
499
- return this.withPendingWrite(collection.name, item == null ? {
500
- upserts: [],
501
- deletes: []
502
- } : this.resolveReplacement(item, replacement), () => this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]));
503
- },
504
- removeOne: async (selector) => {
505
- return this.withPendingWrite(collection.name, {
506
- upserts: [],
507
- deletes: this.resolveRemoval(collection.name, selector, true)
508
- }, () => this.enqueueBatched(collection.name, "removeOne", [selector]));
509
- },
510
- removeMany: async (selector) => {
511
- return this.withPendingWrite(collection.name, {
512
- upserts: [],
513
- deletes: this.resolveRemoval(collection.name, selector, false)
514
- }, () => this.enqueueBatched(collection.name, "removeMany", [selector]));
515
- },
516
- registerQuery: (selector, options) => {
517
- this.updateQuery(collection.name, {
518
- selector,
519
- options
520
- }, {
521
- state: "active",
522
- error: null,
523
- items: []
524
- });
525
- this.execInBackground("registerQuery", collection.name, [selector, options], (error) => {
526
- const query = this.queries[collection.name]?.get(queryId(selector, options));
527
- if (!query) return;
528
- this.updateQuery(collection.name, {
529
- selector,
530
- options
531
- }, {
532
- state: "error",
533
- error
534
- });
535
- query.stateChangeCallbacks.forEach((callback) => callback("error"));
536
- });
537
- },
538
- unregisterQuery: (selector, options) => {
539
- this.queries[collection.name]?.delete(queryId(selector, options));
540
- this.execInBackground("unregisterQuery", collection.name, [selector, options]);
541
- },
542
- getQueryState: (selector, options) => {
543
- return (this.queries[collection.name]?.get(queryId(selector, options)))?.state || "active";
544
- },
545
- getQueryError: (selector, options) => {
546
- return (this.queries[collection.name]?.get(queryId(selector, options)))?.error || null;
547
- },
548
- getQueryResult: (selector, options) => {
549
- const query = this.queries[collection.name]?.get(queryId(selector, options));
550
- if (!query) return [];
551
- return this.servedResult(collection.name, query);
552
- },
553
- onQueryStateChange: (selector, options, callback) => {
554
- this.updateQuery(collection.name, {
555
- selector,
556
- options
557
- }, { stateChangeCallbacks: [...this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks || [], callback] });
558
- return () => {
559
- const currentCallbacks = this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks;
560
- if (!currentCallbacks) return;
561
- this.updateQuery(collection.name, {
562
- selector,
563
- options
564
- }, { stateChangeCallbacks: currentCallbacks.filter((existingCallback) => existingCallback !== callback) });
565
- };
566
- },
567
- executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
568
- dispose: async () => {
569
- await this.exec("unregisterCollection", collection.name);
570
- this.isDisposed = true;
571
- this.worker.terminate?.();
572
- },
573
- isReady: async () => {
574
- await this.collectionReady.get(collection.name);
575
- }
576
- };
419
+ async removeMany(collectionName, selector) {
420
+ const storage = this.storageAdapters.get(collectionName);
421
+ if (!storage) throw new Error(`No storage adapter for collection ${collectionName}`);
422
+ const items = await this.executeQuery(collectionName, selector);
423
+ if (items.length === 0) return [];
424
+ await storage.remove(items);
425
+ await this.checkQueryUpdates(collectionName, {
426
+ upserts: [],
427
+ deletes: items.map((item) => item.id)
428
+ });
429
+ return items;
577
430
  }
578
431
  };
579
432
  //#endregion
580
- export { WorkerDataAdapter as default };
433
+ export { QueryError, AsyncDataAdapter as default };