@signaldb/core 2.0.0-beta.18 → 2.0.0-beta.19

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.
@@ -1,580 +1,453 @@
1
+ const require_isEqual = require("./index2.cjs.js");
1
2
  const require_queryDelta = require("./index4.cjs.js");
2
- const require_randomId = require("./index9.cjs.js");
3
+ const require_getIndexInfo = require("./index17.cjs.js");
3
4
  const require_deepClone = require("./index18.cjs.js");
4
5
  const require_match = require("./index19.cjs.js");
5
6
  const require_modify = require("./index20.cjs.js");
7
+ const require_projectItems = require("./index23.cjs.js");
8
+ const require_sortItems = require("./index24.cjs.js");
6
9
  const require_incrementalQueryUpdate = require("./index25.cjs.js");
7
10
  const require_queryId = require("./index26.cjs.js");
8
- const require_batchOnNextTick = require("./index33.cjs.js");
9
- //#region src/WorkerDataAdapter.ts
11
+ const require_idIndexQuery = require("./index27.cjs.js");
12
+ const require_storageIndexQuery = require("./index33.cjs.js");
13
+ //#region src/AsyncDataAdapter.ts
10
14
  /**
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.
15
+ * Carries the context needed to act on a failed query. The bare storage error
16
+ * on its own does not say which collection or selector produced it, which made
17
+ * the default `console.error` hook close to useless.
14
18
  */
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;
19
+ var QueryError = class extends Error {
20
+ collectionName;
21
+ selector;
22
+ options;
23
+ attempts;
24
+ constructor(collectionName, selector, options, attempts, cause) {
25
+ const reason = cause instanceof Error ? cause.message : String(cause);
26
+ super(`Query on "${collectionName}" failed after ${attempts} attempt(s): ${reason}`);
27
+ this.name = "QueryError";
28
+ this.collectionName = collectionName;
29
+ this.selector = selector;
30
+ this.options = options;
31
+ this.attempts = attempts;
32
+ this.cause = cause;
33
+ }
34
+ };
35
+ /**
36
+ * Turns the item states a write produced into the upsert/delete split a query update needs.
37
+ *
38
+ * The two lists are not symmetric: an item that is still there after the write is described by its
39
+ * new state, while an item that is gone — removed, or given a new id — is described by the id it
40
+ * used to have and nothing else. Mixing the states from before and after a write into one list
41
+ * loses exactly that distinction.
42
+ * @template T - The type of the items.
43
+ * @param previousItems - The items as they were before the write.
44
+ * @param modifiedItems - The items as they are after it.
45
+ * @returns The changeset describing the write.
46
+ */
47
+ function toChangeset(previousItems, modifiedItems) {
48
+ const modifiedIds = new Set(modifiedItems.map((item) => item.id));
49
+ return {
50
+ upserts: modifiedItems,
51
+ deletes: previousItems.map((item) => item.id).filter((id) => !modifiedIds.has(id))
52
+ };
26
53
  }
27
- var WorkerDataAdapter = class WorkerDataAdapter {
28
- worker;
54
+ var DEFAULT_RETRY_ATTEMPTS = 3;
55
+ var defaultRetryDelay = (attempt) => 100 * 4 ** (attempt - 1);
56
+ var wait = (ms) => new Promise((resolve) => {
57
+ setTimeout(resolve, ms);
58
+ });
59
+ /**
60
+ * AsyncDataAdapter
61
+ * Combines WorkerDataAdapter + WorkerDataAdapterHost into a single, transport-free adapter.
62
+ * - Keeps the DataAdapter/CollectionBackend surface identical to the Worker version.
63
+ * - Executes queries and mutations directly against the provided StorageAdapter.
64
+ * - Preserves index-aware query optimization and push-style query updates to listeners.
65
+ */
66
+ var AsyncDataAdapter = class {
29
67
  options;
30
68
  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;
69
+ onError;
70
+ retryAttempts;
71
+ retryDelay;
72
+ storageAdapters = /* @__PURE__ */ new Map();
73
+ storageAdapterReady = /* @__PURE__ */ new Map();
74
+ collectionIndices = /* @__PURE__ */ new Map();
75
+ queries = /* @__PURE__ */ new Map();
76
+ constructor(options) {
47
77
  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
- };
78
+ this.id = options.id || "async-data-adapter";
79
+ this.onError = options.onError ?? ((error) => {
80
+ console.error(error);
58
81
  });
59
- this.worker.addEventListener("message", this.handleWorkerMessage);
82
+ this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
83
+ this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
60
84
  }
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 : require_queryId.default(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 = require_queryDelta.canApplyQueryDelta(query.items, delta);
97
- if (!canApply || require_queryDelta.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) => require_queryDelta.callWithDelta(callback, state, canApply ? delta : void 0));
109
- return;
110
- }
111
- const servedBefore = this.flattenPendingWrites(collectionName) == null ? null : this.servedResult(collectionName, query);
112
- nextItems = require_queryDelta.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 = require_queryDelta.diffQueryResults(servedBefore, this.servedResult(collectionName, stored));
126
- if (require_queryDelta.isEmptyQueryDelta(servedDelta) && state === query.state) return;
127
- stored.stateChangeCallbacks.forEach((callback) => require_queryDelta.callWithDelta(callback, state, servedDelta));
128
- return;
85
+ createCollectionBackend(collection, indices) {
86
+ this.collectionIndices.set(collection.name, indices);
87
+ this.queries.set(collection.name, /* @__PURE__ */ new Map());
88
+ this.ensureStorageAdapter(collection.name);
89
+ const ready = (async () => {
90
+ try {
91
+ await this.setupStorage(collection.name, indices);
92
+ } catch (error) {
93
+ this.onError(error);
94
+ throw error;
129
95
  }
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) => require_queryDelta.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 = require_randomId.default();
153
- this.pendingRequests.set(messageId, {
154
- resolve,
155
- reject
96
+ })();
97
+ this.storageAdapterReady.set(collection.name, ready);
98
+ const registerQuery = (selector, options) => {
99
+ const qid = require_queryId.default(selector, options);
100
+ const registry = this.queries.get(collection.name);
101
+ if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
102
+ registry.set(qid, {
103
+ selector,
104
+ options,
105
+ items: [],
106
+ listeners: /* @__PURE__ */ new Set(),
107
+ ...registry.get(qid),
108
+ state: "active",
109
+ error: null
156
110
  });
157
- this.worker.postMessage({
158
- id: messageId,
159
- workerId: this.id,
160
- method,
161
- args: [collectionName, ...args]
111
+ this.fulfillQuery(collection.name, selector, options).catch(this.onError);
112
+ };
113
+ const unregisterQuery = (selector, options) => {
114
+ const qid = require_queryId.default(selector, options);
115
+ this.queries.get(collection.name)?.delete(qid);
116
+ };
117
+ const getQueryState = (selector, options) => {
118
+ return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.state ?? "active";
119
+ };
120
+ const getQueryError = (selector, options) => {
121
+ return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.error ?? null;
122
+ };
123
+ const getQueryResult = (selector, options) => {
124
+ return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.items ?? [];
125
+ };
126
+ const retryQuery = (selector, options) => {
127
+ const qid = require_queryId.default(selector, options);
128
+ if (!this.queries.get(collection.name)?.get(qid)) return;
129
+ this.publishState(collection.name, qid, "active", null);
130
+ this.runQuery(collection.name, selector, options);
131
+ };
132
+ const onQueryStateChange = (selector, options, callback) => {
133
+ const qid = require_queryId.default(selector, options);
134
+ const registry = this.queries.get(collection.name);
135
+ if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
136
+ if (!registry.has(qid)) registry.set(qid, {
137
+ selector,
138
+ options,
139
+ state: "active",
140
+ error: null,
141
+ items: [],
142
+ listeners: /* @__PURE__ */ new Set()
162
143
  });
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;
144
+ registry.get(qid)?.listeners.add(callback);
145
+ if (registry.get(qid)?.state === "error") retryQuery(selector, options);
146
+ return () => registry.get(qid)?.listeners.delete(callback);
147
+ };
148
+ return {
149
+ insert: async (item) => {
150
+ await ready;
151
+ return await this.insert(collection.name, item);
152
+ },
153
+ updateOne: async (selector, modifier) => {
154
+ await ready;
155
+ return this.updateOne(collection.name, selector, modifier);
156
+ },
157
+ updateMany: async (selector, modifier) => {
158
+ await ready;
159
+ return this.updateMany(collection.name, selector, modifier);
160
+ },
161
+ replaceOne: async (selector, replacement) => {
162
+ await ready;
163
+ return this.replaceOne(collection.name, selector, replacement);
164
+ },
165
+ removeOne: async (selector) => {
166
+ await ready;
167
+ return this.removeOne(collection.name, selector);
168
+ },
169
+ removeMany: async (selector) => {
170
+ await ready;
171
+ return this.removeMany(collection.name, selector);
172
+ },
173
+ registerQuery,
174
+ unregisterQuery,
175
+ retryQuery,
176
+ getQueryState,
177
+ getQueryError,
178
+ getQueryResult,
179
+ onQueryStateChange,
180
+ executeQuery: async (selector, options) => {
181
+ await ready;
182
+ return this.executeQuery(collection.name, selector, options);
183
+ },
184
+ dispose: async () => {
185
+ this.storageAdapters.delete(collection.name);
186
+ this.queries.delete(collection.name);
187
+ this.collectionIndices.delete(collection.name);
188
+ this.storageAdapterReady.delete(collection.name);
189
+ },
190
+ isReady: async () => {
191
+ await ready;
179
192
  }
180
- this.log(method, "failed", error);
181
- });
193
+ };
182
194
  }
183
- queryItemsById(query) {
184
- if (!query.itemsById) query.itemsById = new Map(query.items.map((item) => [item.id, item]));
185
- return query.itemsById;
195
+ async setupStorage(collectionName, indices) {
196
+ const storage = this.storageAdapters.get(collectionName);
197
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
198
+ await storage.setup();
199
+ await Promise.all(indices.map((index) => storage.createIndex(index)));
186
200
  }
187
- flattenPendingWrites(collectionName) {
188
- const state = this.pendingWrites.get(collectionName);
189
- if (!state || state.writes.size === 0) return null;
190
- return state.flat;
201
+ ensureStorageAdapter(name) {
202
+ if (this.storageAdapters.has(name)) return;
203
+ const adapter = this.options.storage(name);
204
+ if (!adapter) return;
205
+ this.storageAdapters.set(name, adapter);
191
206
  }
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);
207
+ /**
208
+ * Compute and publish the result for a specific query
209
+ * @param collectionName - name of the collection
210
+ * @param selector - query selector
211
+ * @param options - query options
212
+ */
213
+ async fulfillQuery(collectionName, selector, options) {
214
+ const qid = require_queryId.default(selector, options);
215
+ const registry = this.queries.get(collectionName);
216
+ if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
217
+ if (!registry.get(qid)) return;
218
+ this.publishState(collectionName, qid, "active", null);
219
+ await this.runQuery(collectionName, selector, options);
203
220
  }
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;
221
+ /**
222
+ * Executes a query, retrying transient failures before giving up. The state
223
+ * stays `'active'` across retries — consumers should see "still loading",
224
+ * not "failed", until we actually stop trying. Only the final failure is
225
+ * published as `'error'` and reported through `onError`; previously that
226
+ * error was swallowed entirely (`fulfillQuery` caught it internally, so the
227
+ * `.catch(this.onError)` on its call site was unreachable) and the query
228
+ * stayed dead for the rest of the session.
229
+ * @param collectionName - name of the collection
230
+ * @param selector - query selector
231
+ * @param options - query options
232
+ */
233
+ async runQuery(collectionName, selector, options) {
234
+ const qid = require_queryId.default(selector, options);
235
+ let lastError;
236
+ for (let attempt = 1; attempt <= this.retryAttempts; attempt += 1) {
237
+ if (!this.queries.get(collectionName)?.has(qid)) return;
238
+ try {
239
+ const items = await this.executeQuery(collectionName, selector, options);
240
+ const rec = this.queries.get(collectionName)?.get(qid);
241
+ const delta = rec?.answered ? require_queryDelta.diffQueryResults(rec.items, items) : void 0;
242
+ this.publishResult(collectionName, qid, items);
243
+ this.publishState(collectionName, qid, "complete", null, delta);
244
+ return;
245
+ } catch (error) {
246
+ lastError = error;
247
+ if (attempt < this.retryAttempts) await wait(this.retryDelay(attempt));
248
+ }
215
249
  }
216
- WorkerDataAdapter.writeFlatEntry(state, id, top.item);
250
+ const queryError = new QueryError(collectionName, selector, options, this.retryAttempts, lastError);
251
+ this.publishState(collectionName, qid, "error", queryError);
252
+ this.onError(queryError);
217
253
  }
218
- static writeFlatEntry(state, id, item) {
219
- if (item === null) {
220
- state.flat.deletes.add(id);
221
- state.flat.upserts.delete(id);
222
- return;
254
+ /**
255
+ * Notify listeners about state changes and keep the cache updated
256
+ * @param collectionName - name of the collection
257
+ * @param qid - query id
258
+ * @param state - new state
259
+ * @param error - error if state is 'error', null otherwise
260
+ * @param delta - what changed about the result, when that is known
261
+ */
262
+ publishState(collectionName, qid, state, error, delta) {
263
+ const rec = this.queries.get(collectionName)?.get(qid);
264
+ if (!rec) return;
265
+ rec.state = state;
266
+ rec.error = error;
267
+ const subscribers = [...rec.listeners];
268
+ for (const callback of subscribers) try {
269
+ require_queryDelta.callWithDelta(callback, state, delta);
270
+ } catch (error_) {
271
+ this.onError(error_);
223
272
  }
224
- state.flat.upserts.set(id, item);
225
- state.flat.deletes.delete(id);
226
273
  }
227
- static providesFullItems(query) {
228
- return query.options?.fields == null;
274
+ publishResult(collectionName, qid, items) {
275
+ const rec = this.queries.get(collectionName)?.get(qid);
276
+ if (!rec) return;
277
+ rec.items = items;
278
+ rec.itemIds = void 0;
279
+ rec.answered = true;
229
280
  }
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()];
281
+ queryItemIds(rec) {
282
+ if (!rec.itemIds) rec.itemIds = new Set(rec.items.map((item) => item.id));
283
+ return rec.itemIds;
242
284
  }
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);
251
- return;
252
- }
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()];
265
- }
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
285
+ async getIndexInfo(collectionName, selector) {
286
+ const storageAdapter = this.storageAdapters.get(collectionName);
287
+ if (!storageAdapter) throw new Error(`No persistence adapter for collection ${collectionName}`);
288
+ if (selector != null && Object.keys(selector).length === 1 && "id" in selector) {
289
+ const idResult = require_idIndexQuery.default(selector);
290
+ if (idResult.matched) return {
291
+ matched: true,
292
+ ids: idResult.ids.filter(Boolean),
293
+ optimizedSelector: {}
294
+ };
295
+ }
296
+ if (selector == null) return {
297
+ matched: false,
298
+ ids: [],
299
+ optimizedSelector: {}
274
300
  };
275
- return items;
301
+ return require_getIndexInfo.default((this.collectionIndices.get(collectionName) ?? []).map((field) => require_storageIndexQuery.default(storageAdapter, field)), selector);
276
302
  }
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 require_incrementalQueryUpdate.mergeChangesetIntoResult(served.items, query.selector, query.options, this.changesetForIds(collectionName, query, state.lastChange.ids));
285
- }
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
303
+ async queryItems(collectionName, selector) {
304
+ const storage = this.storageAdapters.get(collectionName);
305
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
306
+ const index = await this.getIndexInfo(collectionName, selector);
307
+ const matchItems = (item) => {
308
+ if (index.optimizedSelector == null) return true;
309
+ if (Object.keys(index.optimizedSelector).length <= 0) return true;
310
+ return require_match.default(item, index.optimizedSelector);
308
311
  };
312
+ if (index.matched) {
313
+ const items = await storage.readIds(index.ids);
314
+ if (require_isEqual.default(index.optimizedSelector, {})) return items;
315
+ return items.filter(matchItems);
316
+ } else {
317
+ const allItems = await storage.readAll();
318
+ if (require_isEqual.default(selector, {})) return allItems;
319
+ return allItems.filter(matchItems);
320
+ }
309
321
  }
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 && require_match.default(item, query.selector)) affected = true;
322
- });
323
- if (!affected) return query.items;
324
- return require_incrementalQueryUpdate.mergeChangesetIntoResult(query.items, query.selector, query.options, {
325
- upserts: [...pending.upserts.values()],
326
- deletes: [...pending.deletes]
327
- });
322
+ async executeQuery(collectionName, selector, options) {
323
+ const items = await this.queryItems(collectionName, selector || {});
324
+ const { sort, skip, limit, fields } = options || {};
325
+ const sorted = sort ? require_sortItems.default(items, sort) : items;
326
+ const skipped = skip ? sorted.slice(skip) : sorted;
327
+ return require_projectItems.default(limit ? skipped.slice(0, limit) : skipped, fields);
328
328
  }
329
329
  /**
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.
330
+ * After mutations, bring every affected active query up to date.
331
+ *
332
+ * A query whose previous result is enough to answer the change is brought up to date from that
333
+ * result alone no round trip to the storage, and no detour through `'active'`, because there is
334
+ * no window in which the query is stale. Only a query the change cannot be reasoned about
335
+ * locally a window onto a larger set, or one that has never been answered — goes back to the
336
+ * store, and that one gets the same retry and reporting behaviour a freshly registered query
337
+ * gets: a refresh that fails silently leaves exactly the same dead cursor.
338
+ * @param collectionName - name of the collection
339
+ * @param changes - the items the write created, updated or removed
337
340
  */
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
- };
385
- }
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 && require_match.default(item, query.selector));
395
- if (!wasHolding && !nowMatches) return;
396
- affected.push(query);
341
+ async checkQueryUpdates(collectionName, changes) {
342
+ const registry = this.queries.get(collectionName);
343
+ if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
344
+ if (registry.size === 0) return;
345
+ if (changes.upserts.length === 0 && changes.deletes.length === 0) return;
346
+ const affected = [...registry.values()].filter((rec) => {
347
+ const ids = this.queryItemIds(rec);
348
+ if (changes.deletes.some((id) => ids.has(id))) return true;
349
+ return changes.upserts.some((item) => ids.has(item.id) || require_match.default(item, rec.selector));
397
350
  });
398
- return affected;
399
- }
400
- servedResults(collectionName, queries) {
401
- return new Map(queries.map((query) => [query, this.servedResult(collectionName, query)]));
351
+ if (affected.length === 0) return;
352
+ const needsReExecution = [];
353
+ for (const rec of affected) {
354
+ const { selector, options } = rec;
355
+ const incremental = rec.answered ? require_incrementalQueryUpdate.default(rec.items, selector, options, changes) : null;
356
+ if (incremental == null) {
357
+ needsReExecution.push(rec);
358
+ continue;
359
+ }
360
+ const qid = require_queryId.default(selector, options);
361
+ const delta = require_queryDelta.diffQueryResults(rec.items, incremental);
362
+ if (require_queryDelta.isEmptyQueryDelta(delta)) continue;
363
+ this.publishResult(collectionName, qid, incremental);
364
+ this.publishState(collectionName, qid, "complete", null, delta);
365
+ }
366
+ for (const { selector, options } of needsReExecution) this.publishState(collectionName, require_queryId.default(selector, options), "active", null);
367
+ await Promise.all(needsReExecution.map(({ selector, options }) => this.runQuery(collectionName, selector, options)));
402
368
  }
403
- notifyWithDeltas(collectionName, queries, servedBefore) {
404
- queries.forEach((query) => {
405
- const before = servedBefore.get(query);
406
- if (before == null) return;
407
- const delta = require_queryDelta.diffQueryResults(before, this.servedResult(collectionName, query));
408
- if (require_queryDelta.isEmptyQueryDelta(delta)) return;
409
- query.stateChangeCallbacks.forEach((callback) => require_queryDelta.callWithDelta(callback, query.state, delta));
369
+ async insert(collectionName, newItem) {
370
+ const storage = this.storageAdapters.get(collectionName);
371
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
372
+ if ((await storage.readIds([newItem.id])).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
373
+ await storage.insert([newItem]);
374
+ await this.checkQueryUpdates(collectionName, {
375
+ upserts: [newItem],
376
+ deletes: []
410
377
  });
378
+ return newItem;
411
379
  }
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) => require_match.default(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 = require_modify.default(require_deepClone.default(item), restModifier);
424
- upserts.push(modifiedItem);
425
- if (modifiedItem.id !== item.id) deletes.push(item.id);
426
- });
427
- return {
428
- upserts,
429
- deletes
430
- };
380
+ async updateOne(collectionName, selector, modifier) {
381
+ const storage = this.storageAdapters.get(collectionName);
382
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
383
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
384
+ const { $setOnInsert, ...rest } = modifier;
385
+ if (item == null) return [];
386
+ const modified = require_modify.default(require_deepClone.default(item), rest);
387
+ if (item.id !== modified.id) {
388
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
389
+ }
390
+ await storage.replace([modified]);
391
+ await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
392
+ return [modified];
431
393
  }
432
- resolveRemoval(collectionName, selector, onlyFirst) {
433
- return this.matchObservableItems(collectionName, selector, onlyFirst).map((item) => item.id);
394
+ async updateMany(collectionName, selector, modifier) {
395
+ const storage = this.storageAdapters.get(collectionName);
396
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
397
+ const items = await this.executeQuery(collectionName, selector);
398
+ if (items.length === 0) return [];
399
+ const { $setOnInsert, ...rest } = modifier;
400
+ const changed = await Promise.all(items.map(async (item) => {
401
+ const modified = require_modify.default(require_deepClone.default(item), rest);
402
+ if (item.id !== modified.id) {
403
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
404
+ }
405
+ return modified;
406
+ }));
407
+ await storage.replace(changed);
408
+ await this.checkQueryUpdates(collectionName, toChangeset(items, changed));
409
+ return changed;
434
410
  }
435
- resolveReplacement(item, replacement) {
436
- const modifiedItem = {
411
+ async replaceOne(collectionName, selector, replacement) {
412
+ const storage = this.storageAdapters.get(collectionName);
413
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
414
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
415
+ if (item == null) return [];
416
+ const modified = {
437
417
  ...replacement,
438
418
  id: replacement.id ?? item.id
439
419
  };
440
- return {
441
- upserts: [modifiedItem],
442
- deletes: modifiedItem.id === item.id ? [] : [item.id]
443
- };
444
- }
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();
420
+ if (item.id !== modified.id) {
421
+ if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
451
422
  }
423
+ await storage.replace([modified]);
424
+ await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
425
+ return [modified];
452
426
  }
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 = require_queryId.default(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;
427
+ async removeOne(collectionName, selector) {
428
+ const storage = this.storageAdapters.get(collectionName);
429
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
430
+ const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
431
+ if (item == null) return [];
432
+ await storage.remove([item]);
433
+ await this.checkQueryUpdates(collectionName, {
434
+ upserts: [],
435
+ deletes: [item.id]
436
+ });
437
+ return [item];
478
438
  }
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, require_batchOnNextTick.default(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(require_queryId.default(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(require_queryId.default(selector, options));
540
- this.execInBackground("unregisterQuery", collection.name, [selector, options]);
541
- },
542
- getQueryState: (selector, options) => {
543
- return (this.queries[collection.name]?.get(require_queryId.default(selector, options)))?.state || "active";
544
- },
545
- getQueryError: (selector, options) => {
546
- return (this.queries[collection.name]?.get(require_queryId.default(selector, options)))?.error || null;
547
- },
548
- getQueryResult: (selector, options) => {
549
- const query = this.queries[collection.name]?.get(require_queryId.default(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(require_queryId.default(selector, options))?.stateChangeCallbacks || [], callback] });
558
- return () => {
559
- const currentCallbacks = this.queries[collection.name]?.get(require_queryId.default(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
- };
439
+ async removeMany(collectionName, selector) {
440
+ const storage = this.storageAdapters.get(collectionName);
441
+ if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
442
+ const items = await this.executeQuery(collectionName, selector);
443
+ if (items.length === 0) return [];
444
+ await storage.remove(items);
445
+ await this.checkQueryUpdates(collectionName, {
446
+ upserts: [],
447
+ deletes: items.map((item) => item.id)
448
+ });
449
+ return items;
577
450
  }
578
451
  };
579
452
  //#endregion
580
- exports.default = WorkerDataAdapter;
453
+ exports.default = AsyncDataAdapter;