@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.
- package/README.md +3 -3
- package/dist/.vite/manifest.json +61 -59
- package/dist/AsyncDataAdapter.d.ts +15 -2
- package/dist/AutoFetchDataAdapter.d.ts +13 -2
- package/dist/Collection/Observer.d.ts +1 -1
- package/dist/Collection/index.d.ts +11 -12
- package/dist/DataAdapter.d.ts +19 -3
- package/dist/WorkerDataAdapterHost.d.ts +19 -6
- package/dist/index.cjs.js +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +4 -4
- package/dist/index12.cjs.js +1 -1
- package/dist/index12.mjs +1 -1
- package/dist/index15.cjs.js +6 -2
- package/dist/index15.mjs +6 -2
- package/dist/index17.cjs.js +4 -2
- package/dist/index17.mjs +4 -3
- package/dist/index18.cjs.js +1 -0
- package/dist/index18.mjs +1 -1
- package/dist/index28.cjs.js +15 -5
- package/dist/index28.mjs +15 -5
- package/dist/index29.cjs.js +29 -16
- package/dist/index29.mjs +29 -16
- package/dist/index33.cjs.js +17 -49
- package/dist/index33.mjs +17 -49
- package/dist/index34.cjs.js +45 -445
- package/dist/index34.mjs +45 -445
- package/dist/index35.cjs.js +132 -65
- package/dist/index35.mjs +132 -65
- package/dist/index36.cjs.js +385 -531
- package/dist/index36.mjs +386 -533
- package/dist/index37.cjs.js +68 -17
- package/dist/index37.mjs +68 -17
- package/dist/index38.cjs.js +536 -319
- package/dist/index38.mjs +538 -321
- package/dist/index39.cjs.js +301 -466
- package/dist/index39.mjs +301 -466
- package/dist/index40.cjs.js +484 -0
- package/dist/index40.mjs +484 -0
- package/dist/index5.cjs.js +3 -3
- package/dist/index5.mjs +3 -3
- package/dist/index6.cjs.js +1 -0
- package/dist/index6.mjs +1 -1
- package/dist/types/StorageAdapter.d.ts +80 -0
- package/dist/utils/executeStorageQuery.d.ts +28 -0
- package/package.json +1 -1
package/dist/index36.mjs
CHANGED
|
@@ -1,580 +1,433 @@
|
|
|
1
|
-
import {
|
|
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
|
|
5
|
+
import incrementalQueryUpdate from "./index25.mjs";
|
|
7
6
|
import queryId from "./index26.mjs";
|
|
8
|
-
import
|
|
9
|
-
//#region src/
|
|
7
|
+
import executeStorageQuery from "./index35.mjs";
|
|
8
|
+
//#region src/AsyncDataAdapter.ts
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
|
28
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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 =
|
|
49
|
-
|
|
50
|
-
|
|
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.
|
|
77
|
+
this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
|
|
78
|
+
this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
|
|
60
79
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
228
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
278
|
-
const
|
|
279
|
-
if (!
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
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
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
const
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
-
|
|
429
|
-
|
|
364
|
+
items: [modified],
|
|
365
|
+
previousItems: [item]
|
|
430
366
|
};
|
|
431
367
|
}
|
|
432
|
-
|
|
433
|
-
|
|
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
|
-
|
|
436
|
-
const
|
|
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
|
-
|
|
442
|
-
|
|
403
|
+
items: [modified],
|
|
404
|
+
previousItems: [item]
|
|
443
405
|
};
|
|
444
406
|
}
|
|
445
|
-
async
|
|
446
|
-
const
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
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
|
-
|
|
480
|
-
this.
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
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 {
|
|
433
|
+
export { QueryError, AsyncDataAdapter as default };
|