@signaldb/core 2.0.0-beta.3 → 2.0.0-beta.5
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/dist/.vite/manifest.json +24 -18
- package/dist/Collection/Observer.d.ts +2 -0
- package/dist/Collection/index.d.ts +11 -10
- package/dist/Collection/types.d.ts +7 -0
- package/dist/DefaultDataAdapter.d.ts +2 -0
- package/dist/index.cjs.js +8 -5
- package/dist/index.cjs12.js +41 -346
- package/dist/index.cjs13.js +308 -362
- package/dist/index.cjs14.js +387 -177
- package/dist/index.cjs15.js +182 -364
- package/dist/index.cjs16.js +287 -478
- package/dist/index.cjs17.js +563 -136
- package/dist/index.cjs18.js +154 -23
- package/dist/index.cjs19.js +23 -39
- package/dist/index.cjs2.js +1 -1
- package/dist/index.cjs20.js +39 -13
- package/dist/index.cjs21.js +14 -102
- package/dist/index.cjs22.js +93 -118
- package/dist/index.cjs23.js +127 -5
- package/dist/index.cjs24.js +5 -25
- package/dist/index.cjs25.js +24 -7
- package/dist/index.cjs26.js +8 -27
- package/dist/index.cjs27.js +25 -42
- package/dist/index.cjs28.js +44 -6
- package/dist/index.cjs29.js +6 -7
- package/dist/index.cjs3.js +9 -26
- package/dist/index.cjs34.js +9 -0
- package/dist/index.cjs7.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +10 -7
- package/dist/index12.mjs +40 -346
- package/dist/index13.mjs +308 -362
- package/dist/index14.mjs +387 -177
- package/dist/index15.mjs +182 -364
- package/dist/index16.mjs +287 -478
- package/dist/index17.mjs +563 -136
- package/dist/index18.mjs +154 -23
- package/dist/index19.mjs +23 -38
- package/dist/index2.mjs +1 -1
- package/dist/index20.mjs +38 -13
- package/dist/index21.mjs +14 -102
- package/dist/index22.mjs +93 -117
- package/dist/index23.mjs +126 -5
- package/dist/index24.mjs +5 -25
- package/dist/index25.mjs +24 -7
- package/dist/index26.mjs +8 -27
- package/dist/index27.mjs +25 -42
- package/dist/index28.mjs +44 -6
- package/dist/index29.mjs +6 -7
- package/dist/index3.mjs +9 -26
- package/dist/index34.mjs +10 -0
- package/dist/index7.mjs +1 -1
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/package.json +1 -1
package/dist/index.cjs15.js
CHANGED
|
@@ -1,388 +1,206 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const getIndexInfo = require("./index.cjs22.js");
|
|
8
|
-
const getMatchingKeys = require("./index.cjs26.js");
|
|
9
|
-
const sortItems = require("./index.cjs25.js");
|
|
10
|
-
const project = require("./index.cjs24.js");
|
|
11
|
-
const compact = require("./index.cjs28.js");
|
|
12
|
-
class WorkerDataAdapterHost {
|
|
13
|
-
workerContext;
|
|
2
|
+
const queryId = require("./index.cjs21.js");
|
|
3
|
+
const randomId = require("./index.cjs8.js");
|
|
4
|
+
const batchOnNextTick = require("./index.cjs28.js");
|
|
5
|
+
class WorkerDataAdapter {
|
|
6
|
+
worker;
|
|
14
7
|
options;
|
|
15
8
|
id;
|
|
9
|
+
isDisposed = false;
|
|
10
|
+
workerReady;
|
|
16
11
|
log = () => {
|
|
17
12
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
console.error(error);
|
|
24
|
-
};
|
|
25
|
-
constructor(workerContext, options) {
|
|
26
|
-
this.workerContext = workerContext;
|
|
13
|
+
collectionReady = /* @__PURE__ */ new Map();
|
|
14
|
+
batchExecutionHelpers = /* @__PURE__ */ new Map();
|
|
15
|
+
queries = {};
|
|
16
|
+
constructor(worker, options) {
|
|
17
|
+
this.worker = worker;
|
|
27
18
|
this.options = options;
|
|
28
19
|
this.id = this.options.id || "default-worker-data-adapter";
|
|
29
|
-
if (this.options.onError) {
|
|
30
|
-
this.onError = this.options.onError;
|
|
31
|
-
}
|
|
32
20
|
if (this.options.log)
|
|
33
21
|
this.log = this.options.log;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
22
|
+
this.workerReady = new Promise((resolve, reject) => {
|
|
23
|
+
const timeoutId = setTimeout(() => {
|
|
24
|
+
reject(new Error("WorkerDataAdapter initialization timed out"));
|
|
25
|
+
}, 5e3);
|
|
26
|
+
const handleMessage = (event) => {
|
|
27
|
+
const { type, workerId } = event.data;
|
|
28
|
+
if (workerId !== this.id)
|
|
29
|
+
return;
|
|
30
|
+
if (type === "ready") {
|
|
31
|
+
resolve();
|
|
32
|
+
clearTimeout(timeoutId);
|
|
33
|
+
this.worker.removeEventListener("message", handleMessage);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
this.worker.addEventListener("message", handleMessage);
|
|
44
37
|
});
|
|
45
|
-
this.respond("ready", null, null, "ready");
|
|
46
|
-
}
|
|
47
|
-
respond(id, data, error = null, type = "response") {
|
|
48
|
-
this.workerContext.postMessage({ id, workerId: this.id, type, data, error });
|
|
49
38
|
}
|
|
50
|
-
async
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
async exec(method, collectionName, ...args) {
|
|
40
|
+
await this.workerReady;
|
|
41
|
+
if (method !== "isReady") {
|
|
42
|
+
const collectionReady = this.collectionReady.get(collectionName);
|
|
43
|
+
if (!collectionReady)
|
|
44
|
+
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
45
|
+
await collectionReady;
|
|
57
46
|
}
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
const result = await fn.apply(this, args);
|
|
62
|
-
this.respond(id, result);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
this.respond(id, null, error);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async getIndexInfo(collectionName, selector) {
|
|
68
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
69
|
-
if (!storageAdapter)
|
|
70
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
71
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
72
|
-
return {
|
|
73
|
-
matched: true,
|
|
74
|
-
ids: compact([selector.id]),
|
|
75
|
-
optimizedSelector: {}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
if (selector == null) {
|
|
79
|
-
return {
|
|
80
|
-
matched: false,
|
|
81
|
-
ids: [],
|
|
82
|
-
optimizedSelector: {}
|
|
83
|
-
};
|
|
47
|
+
if (this.isDisposed) {
|
|
48
|
+
throw new Error("WorkerDataAdapter is disposed");
|
|
84
49
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
includedIds.push(pos);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
for (const key of keys.include) {
|
|
105
|
-
const idSet = index.get(key);
|
|
106
|
-
if (idSet) {
|
|
107
|
-
for (const id of idSet) {
|
|
108
|
-
includedIds.push(id);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (keys.exclude != null) {
|
|
114
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
115
|
-
for (const key of keys.exclude) {
|
|
116
|
-
const idSet = index.get(key);
|
|
117
|
-
if (idSet) {
|
|
118
|
-
for (const id of idSet) {
|
|
119
|
-
excludeIds.add(id);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const messageId = randomId();
|
|
52
|
+
const handleMessage = (event) => {
|
|
53
|
+
const { id, workerId, type, data, error } = event.data;
|
|
54
|
+
if (workerId !== this.id)
|
|
55
|
+
return;
|
|
56
|
+
if (type !== "response")
|
|
57
|
+
return;
|
|
58
|
+
if (id !== messageId)
|
|
59
|
+
return;
|
|
60
|
+
this.log(method, "result", data ?? error);
|
|
61
|
+
if (error) {
|
|
62
|
+
reject(error);
|
|
63
|
+
} else {
|
|
64
|
+
resolve(data);
|
|
122
65
|
}
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
return {
|
|
126
|
-
matched: true,
|
|
127
|
-
ids: includedIds,
|
|
128
|
-
fields: [field],
|
|
129
|
-
keepSelector: filteresForNull
|
|
130
|
-
};
|
|
131
|
-
}), selector);
|
|
132
|
-
}
|
|
133
|
-
async queryItems(collectionName, selector) {
|
|
134
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
135
|
-
if (!storageAdapter)
|
|
136
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
137
|
-
const indexInfo = await this.getIndexInfo(collectionName, selector);
|
|
138
|
-
const matchItems = (item) => {
|
|
139
|
-
if (indexInfo.optimizedSelector == null)
|
|
140
|
-
return true;
|
|
141
|
-
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
142
|
-
return true;
|
|
143
|
-
const matches = match(item, indexInfo.optimizedSelector);
|
|
144
|
-
return matches;
|
|
145
|
-
};
|
|
146
|
-
if (indexInfo.matched) {
|
|
147
|
-
const items = await storageAdapter.readIds(indexInfo.ids);
|
|
148
|
-
if (isEqual(indexInfo.optimizedSelector, {}))
|
|
149
|
-
return items;
|
|
150
|
-
return items.filter(matchItems);
|
|
151
|
-
} else {
|
|
152
|
-
const allItems = await storageAdapter.readAll();
|
|
153
|
-
if (isEqual(selector, {}))
|
|
154
|
-
return allItems;
|
|
155
|
-
return allItems.filter(matchItems);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
async executeQuery(collectionName, selector, options) {
|
|
159
|
-
if (selector === null)
|
|
160
|
-
return [];
|
|
161
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
162
|
-
const { sort, skip, limit, fields } = options || {};
|
|
163
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
164
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
165
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
166
|
-
const idExcluded = fields && fields.id === 0;
|
|
167
|
-
return limited.map((item) => {
|
|
168
|
-
if (!fields)
|
|
169
|
-
return item;
|
|
170
|
-
return {
|
|
171
|
-
...idExcluded ? {} : { id: item.id },
|
|
172
|
-
...project(item, fields)
|
|
66
|
+
this.worker.removeEventListener("message", handleMessage);
|
|
173
67
|
};
|
|
68
|
+
this.worker.addEventListener("message", handleMessage);
|
|
69
|
+
this.worker.postMessage({
|
|
70
|
+
id: messageId,
|
|
71
|
+
workerId: this.id,
|
|
72
|
+
method,
|
|
73
|
+
args: [collectionName, ...args]
|
|
74
|
+
});
|
|
174
75
|
});
|
|
175
76
|
}
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
if (!
|
|
179
|
-
throw new Error(`Collection ${collectionName} not
|
|
180
|
-
|
|
181
|
-
let query = this.queries.get(collectionName)?.get(id);
|
|
182
|
-
if (!query) {
|
|
183
|
-
query = { selector, options };
|
|
184
|
-
this.queries.get(collectionName)?.set(id, query);
|
|
185
|
-
}
|
|
186
|
-
return query;
|
|
77
|
+
enqueueBatched(collectionName, method, args) {
|
|
78
|
+
const helper = this.batchExecutionHelpers.get(collectionName);
|
|
79
|
+
if (!helper)
|
|
80
|
+
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
81
|
+
return helper.enqueue(method, args);
|
|
187
82
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const
|
|
83
|
+
// ---------- end batching integration ----------
|
|
84
|
+
updateQuery(collectionName, query, update) {
|
|
85
|
+
const id = queryId(query.selector, query.options);
|
|
86
|
+
const collectionQueries = this.queries[collectionName];
|
|
191
87
|
if (!collectionQueries)
|
|
192
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
193
|
-
this.respond(id, { collectionName, selector, options, state, error, items }, null, "queryUpdate");
|
|
194
|
-
}
|
|
195
|
-
ensureStorageAdapter(name) {
|
|
196
|
-
if (this.storageAdapters.has(name))
|
|
197
|
-
return;
|
|
198
|
-
const adapter = this.options.storage(name);
|
|
199
|
-
if (!adapter)
|
|
200
88
|
return;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
await Promise.all(affectedQueries.map(async ({ selector, options }) => {
|
|
214
|
-
const queryItems = await this.executeQuery(collectionName, selector, options);
|
|
215
|
-
this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
|
|
216
|
-
}));
|
|
89
|
+
const existing = collectionQueries.get(id);
|
|
90
|
+
const newState = {
|
|
91
|
+
state: "active",
|
|
92
|
+
error: null,
|
|
93
|
+
items: [],
|
|
94
|
+
stateChangeCallbacks: [],
|
|
95
|
+
eventHandler: existing?.eventHandler,
|
|
96
|
+
...existing,
|
|
97
|
+
...update
|
|
98
|
+
};
|
|
99
|
+
collectionQueries.set(id, newState);
|
|
100
|
+
this.queries[collectionName] = collectionQueries;
|
|
217
101
|
}
|
|
218
|
-
|
|
219
|
-
this.
|
|
220
|
-
this.
|
|
221
|
-
this.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (item.id !== modifiedItem.id) {
|
|
278
|
-
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
279
|
-
if (existingItems.length > 0) {
|
|
280
|
-
return new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return [modifiedItem];
|
|
284
|
-
}));
|
|
285
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
286
|
-
if (modifiedItems.length > 0) {
|
|
287
|
-
await storageAdapter.replace(modifiedItems);
|
|
288
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
289
|
-
}
|
|
290
|
-
return result;
|
|
291
|
-
};
|
|
292
|
-
updateMany = async (collectionName, parameters) => {
|
|
293
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
294
|
-
if (!storageAdapter)
|
|
295
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
296
|
-
const previousItems = [];
|
|
297
|
-
const result = await Promise.all(parameters.map(async ([selector, modifier]) => {
|
|
298
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
299
|
-
if (items.length === 0)
|
|
300
|
-
return [];
|
|
301
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
302
|
-
try {
|
|
303
|
-
const changedItems = await Promise.all(items.map(async (item) => {
|
|
304
|
-
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
305
|
-
if (item.id !== modifiedItem.id) {
|
|
306
|
-
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
307
|
-
if (existingItems.length > 0) {
|
|
308
|
-
throw new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
previousItems.push(item);
|
|
312
|
-
return modifiedItem;
|
|
313
|
-
}));
|
|
314
|
-
return changedItems;
|
|
315
|
-
} catch (error) {
|
|
316
|
-
return error;
|
|
317
|
-
}
|
|
318
|
-
}));
|
|
319
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
320
|
-
if (modifiedItems.length > 0) {
|
|
321
|
-
await storageAdapter.replace(modifiedItems);
|
|
322
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
323
|
-
}
|
|
324
|
-
return result;
|
|
325
|
-
};
|
|
326
|
-
replaceOne = async (collectionName, parameters) => {
|
|
327
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
328
|
-
if (!storageAdapter)
|
|
329
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
330
|
-
const previousItems = [];
|
|
331
|
-
const result = await Promise.all(parameters.map(async ([selector, replacement]) => {
|
|
332
|
-
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
333
|
-
if (item == null)
|
|
334
|
-
return [];
|
|
335
|
-
previousItems.push(item);
|
|
336
|
-
const modifiedItem = {
|
|
337
|
-
...replacement,
|
|
338
|
-
id: replacement.id ?? item.id
|
|
339
|
-
};
|
|
340
|
-
if (item.id !== modifiedItem.id) {
|
|
341
|
-
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
342
|
-
if (existingItems.length > 0) {
|
|
343
|
-
return new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
102
|
+
createCollectionBackend(collection, indices) {
|
|
103
|
+
this.queries[collection.name] = /* @__PURE__ */ new Map();
|
|
104
|
+
void this.exec("registerCollection", collection.name, indices);
|
|
105
|
+
this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
|
|
106
|
+
this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
|
|
107
|
+
return {
|
|
108
|
+
insert: async (item) => {
|
|
109
|
+
return this.enqueueBatched(collection.name, "insert", [item]);
|
|
110
|
+
},
|
|
111
|
+
updateOne: async (selector, modifier) => {
|
|
112
|
+
return this.enqueueBatched(collection.name, "updateOne", [selector, modifier]);
|
|
113
|
+
},
|
|
114
|
+
updateMany: async (selector, modifier) => {
|
|
115
|
+
return this.enqueueBatched(collection.name, "updateMany", [selector, modifier]);
|
|
116
|
+
},
|
|
117
|
+
replaceOne: async (selector, replacement) => {
|
|
118
|
+
return this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]);
|
|
119
|
+
},
|
|
120
|
+
removeOne: async (selector) => {
|
|
121
|
+
return this.enqueueBatched(collection.name, "removeOne", [selector]);
|
|
122
|
+
},
|
|
123
|
+
removeMany: async (selector) => {
|
|
124
|
+
return this.enqueueBatched(collection.name, "removeMany", [selector]);
|
|
125
|
+
},
|
|
126
|
+
// methods for registering and unregistering queries that will be called from the collection during find/findOne
|
|
127
|
+
registerQuery: (selector, options) => {
|
|
128
|
+
this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
|
|
129
|
+
void this.exec("registerQuery", collection.name, selector, options);
|
|
130
|
+
const handler = (event) => {
|
|
131
|
+
const { type, data, workerId, error } = event.data;
|
|
132
|
+
if (type !== "queryUpdate")
|
|
133
|
+
return;
|
|
134
|
+
if (data == null)
|
|
135
|
+
return;
|
|
136
|
+
const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
|
|
137
|
+
if (workerId !== this.id)
|
|
138
|
+
return;
|
|
139
|
+
if (collectionName !== collection.name)
|
|
140
|
+
return;
|
|
141
|
+
if (queryId(responseSelector, responseOptions) !== queryId(selector, options))
|
|
142
|
+
return;
|
|
143
|
+
this.log("queryUpdate", responseSelector, responseOptions, state, data ?? error);
|
|
144
|
+
this.updateQuery(collection.name, {
|
|
145
|
+
selector: responseSelector,
|
|
146
|
+
options: responseOptions
|
|
147
|
+
}, { state, error, items });
|
|
148
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
149
|
+
if (!query)
|
|
150
|
+
return;
|
|
151
|
+
query.stateChangeCallbacks.forEach((callback) => callback(state));
|
|
152
|
+
};
|
|
153
|
+
this.worker.addEventListener("message", handler);
|
|
154
|
+
this.updateQuery(collection.name, { selector, options }, { eventHandler: handler });
|
|
155
|
+
},
|
|
156
|
+
unregisterQuery: (selector, options) => {
|
|
157
|
+
const qid = queryId(selector, options);
|
|
158
|
+
const query = this.queries[collection.name]?.get(qid);
|
|
159
|
+
if (query?.eventHandler) {
|
|
160
|
+
this.worker.removeEventListener("message", query.eventHandler);
|
|
344
161
|
}
|
|
162
|
+
this.queries[collection.name]?.delete(qid);
|
|
163
|
+
void this.exec("unregisterQuery", collection.name, selector, options);
|
|
164
|
+
},
|
|
165
|
+
getQueryState: (selector, options) => {
|
|
166
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
167
|
+
return query?.state || "active";
|
|
168
|
+
},
|
|
169
|
+
getQueryError: (selector, options) => {
|
|
170
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
171
|
+
return query?.error || null;
|
|
172
|
+
},
|
|
173
|
+
getQueryResult: (selector, options) => {
|
|
174
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
175
|
+
return query?.items || [];
|
|
176
|
+
},
|
|
177
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
178
|
+
this.updateQuery(collection.name, { selector, options }, {
|
|
179
|
+
stateChangeCallbacks: [
|
|
180
|
+
...this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks || [],
|
|
181
|
+
callback
|
|
182
|
+
]
|
|
183
|
+
});
|
|
184
|
+
return () => {
|
|
185
|
+
const currentCallbacks = this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks;
|
|
186
|
+
if (!currentCallbacks)
|
|
187
|
+
throw new Error("State change callbacks are not defined!");
|
|
188
|
+
this.updateQuery(collection.name, { selector, options }, {
|
|
189
|
+
stateChangeCallbacks: currentCallbacks.filter((existingCallback) => existingCallback !== callback)
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
|
|
194
|
+
// lifecycle methods
|
|
195
|
+
dispose: async () => {
|
|
196
|
+
await this.exec("unregisterCollection", collection.name);
|
|
197
|
+
this.isDisposed = true;
|
|
198
|
+
this.worker.terminate();
|
|
199
|
+
},
|
|
200
|
+
isReady: async () => {
|
|
201
|
+
await this.exec("isReady", collection.name);
|
|
345
202
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
349
|
-
if (modifiedItems.length > 0) {
|
|
350
|
-
await storageAdapter.replace(modifiedItems);
|
|
351
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
352
|
-
}
|
|
353
|
-
return result;
|
|
354
|
-
};
|
|
355
|
-
removeOne = async (collectionName, selectors) => {
|
|
356
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
357
|
-
if (!storageAdapter)
|
|
358
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
359
|
-
const result = await Promise.all(selectors.map(async ([selector]) => {
|
|
360
|
-
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items2) => items2[0] ?? null);
|
|
361
|
-
if (item == null)
|
|
362
|
-
return [];
|
|
363
|
-
return [item];
|
|
364
|
-
}));
|
|
365
|
-
const items = result.flat();
|
|
366
|
-
if (items.length > 0) {
|
|
367
|
-
await storageAdapter.remove(items);
|
|
368
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
369
|
-
}
|
|
370
|
-
return result;
|
|
371
|
-
};
|
|
372
|
-
removeMany = async (collectionName, selectors) => {
|
|
373
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
374
|
-
if (!storageAdapter)
|
|
375
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
376
|
-
const result = await Promise.all(selectors.map(async ([selector]) => this.executeQuery(collectionName, selector)));
|
|
377
|
-
const items = result.flat();
|
|
378
|
-
if (items.length > 0) {
|
|
379
|
-
await storageAdapter.remove(items);
|
|
380
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
381
|
-
}
|
|
382
|
-
return result;
|
|
383
|
-
};
|
|
384
|
-
isReady = async (collectionName) => {
|
|
385
|
-
return this.storageAdapterReady.get(collectionName);
|
|
386
|
-
};
|
|
203
|
+
};
|
|
204
|
+
}
|
|
387
205
|
}
|
|
388
|
-
module.exports =
|
|
206
|
+
module.exports = WorkerDataAdapter;
|