@signaldb/svelte 2.0.0-beta.6 → 2.0.0-beta.7
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/base/core/src/AsyncDataAdapter.d.ts +64 -0
- package/dist/base/core/src/AsyncDataAdapter.js +450 -0
- package/dist/base/core/src/AutoFetchDataAdapter.d.ts +112 -0
- package/dist/base/core/src/AutoFetchDataAdapter.js +652 -0
- package/dist/base/core/src/Collection/Cursor.d.ts +113 -0
- package/dist/base/core/src/Collection/Cursor.js +293 -0
- package/dist/base/core/src/Collection/Observer.d.ts +64 -0
- package/dist/base/core/src/Collection/Observer.js +181 -0
- package/dist/base/core/src/Collection/index.d.ts +294 -0
- package/dist/base/core/src/Collection/index.js +580 -0
- package/dist/base/core/src/Collection/types.d.ts +28 -0
- package/dist/base/core/src/Collection/types.js +2 -0
- package/dist/base/core/src/DataAdapter.d.ts +35 -0
- package/dist/base/core/src/DataAdapter.js +2 -0
- package/dist/base/core/src/DefaultDataAdapter.d.ts +35 -0
- package/dist/base/core/src/DefaultDataAdapter.js +398 -0
- package/dist/base/core/src/WorkerDataAdapter.d.ts +25 -0
- package/dist/base/core/src/WorkerDataAdapter.js +211 -0
- package/dist/base/core/src/WorkerDataAdapterHost.d.ts +62 -0
- package/dist/base/core/src/WorkerDataAdapterHost.js +409 -0
- package/dist/base/core/src/createIndex.d.ts +7 -0
- package/dist/base/core/src/createIndex.js +120 -0
- package/dist/base/core/src/createIndexProvider.d.ts +8 -0
- package/dist/base/core/src/createIndexProvider.js +11 -0
- package/dist/base/core/src/createReactivityAdapter.d.ts +8 -0
- package/dist/base/core/src/createReactivityAdapter.js +11 -0
- package/dist/base/core/src/createStorageAdapter.d.ts +9 -0
- package/dist/base/core/src/createStorageAdapter.js +11 -0
- package/dist/base/core/src/getIndexInfo.d.ts +39 -0
- package/dist/base/core/src/getIndexInfo.js +176 -0
- package/dist/base/core/src/index.d.ts +22 -0
- package/dist/base/core/src/index.js +39 -0
- package/dist/base/core/src/types/Dependency.d.ts +4 -0
- package/dist/base/core/src/types/Dependency.js +2 -0
- package/dist/base/core/src/types/IndexProvider.d.ts +26 -0
- package/dist/base/core/src/types/IndexProvider.js +2 -0
- package/dist/base/core/src/types/Modifier.d.ts +46 -0
- package/dist/base/core/src/types/Modifier.js +3 -0
- package/dist/base/core/src/types/ReactivityAdapter.d.ts +6 -0
- package/dist/base/core/src/types/ReactivityAdapter.js +2 -0
- package/dist/base/core/src/types/Selector.d.ts +46 -0
- package/dist/base/core/src/types/Selector.js +3 -0
- package/dist/base/core/src/types/Signal.d.ts +4 -0
- package/dist/base/core/src/types/Signal.js +2 -0
- package/dist/base/core/src/types/StorageAdapter.d.ts +20 -0
- package/dist/base/core/src/types/StorageAdapter.js +2 -0
- package/dist/base/core/src/utils/EventEmitter.d.ts +71 -0
- package/dist/base/core/src/utils/EventEmitter.js +144 -0
- package/dist/base/core/src/utils/batchOnNextTick.d.ts +16 -0
- package/dist/base/core/src/utils/batchOnNextTick.js +72 -0
- package/dist/base/core/src/utils/compact.d.ts +9 -0
- package/dist/base/core/src/utils/compact.js +21 -0
- package/dist/base/core/src/utils/createSignal.d.ts +14 -0
- package/dist/base/core/src/utils/createSignal.js +38 -0
- package/dist/base/core/src/utils/deepClone.d.ts +17 -0
- package/dist/base/core/src/utils/deepClone.js +68 -0
- package/dist/base/core/src/utils/get.d.ts +9 -0
- package/dist/base/core/src/utils/get.js +27 -0
- package/dist/base/core/src/utils/getMatchingKeys.d.ts +19 -0
- package/dist/base/core/src/utils/getMatchingKeys.js +49 -0
- package/dist/base/core/src/utils/intersection.d.ts +9 -0
- package/dist/base/core/src/utils/intersection.js +16 -0
- package/dist/base/core/src/utils/isEqual.d.ts +14 -0
- package/dist/base/core/src/utils/isEqual.js +43 -0
- package/dist/base/core/src/utils/isFieldExpression.d.ts +11 -0
- package/dist/base/core/src/utils/isFieldExpression.js +52 -0
- package/dist/base/core/src/utils/match.d.ts +12 -0
- package/dist/base/core/src/utils/match.js +16 -0
- package/dist/base/core/src/utils/modify.d.ts +14 -0
- package/dist/base/core/src/utils/modify.js +29 -0
- package/dist/base/core/src/utils/project.d.ts +15 -0
- package/dist/base/core/src/utils/project.js +43 -0
- package/dist/base/core/src/utils/queryId.d.ts +9 -0
- package/dist/base/core/src/utils/queryId.js +30 -0
- package/dist/base/core/src/utils/randomId.d.ts +7 -0
- package/dist/base/core/src/utils/randomId.js +12 -0
- package/dist/base/core/src/utils/reactiveOrAsync.d.ts +59 -0
- package/dist/base/core/src/utils/reactiveOrAsync.js +93 -0
- package/dist/base/core/src/utils/serializeValue.d.ts +12 -0
- package/dist/base/core/src/utils/serializeValue.js +27 -0
- package/dist/base/core/src/utils/set.d.ts +13 -0
- package/dist/base/core/src/utils/set.js +43 -0
- package/dist/base/core/src/utils/sortItems.d.ts +12 -0
- package/dist/base/core/src/utils/sortItems.js +23 -0
- package/dist/base/core/src/utils/uniqueBy.d.ts +10 -0
- package/dist/base/core/src/utils/uniqueBy.js +19 -0
- package/dist/reactivity-adapters/svelte/index.svelte.d.ts +10 -0
- package/dist/reactivity-adapters/svelte/index.svelte.js +36 -0
- package/package.json +3 -3
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const createIndex_1 = __importDefault(require("./createIndex"));
|
|
7
|
+
const getIndexInfo_1 = __importDefault(require("./getIndexInfo"));
|
|
8
|
+
const deepClone_1 = __importDefault(require("./utils/deepClone"));
|
|
9
|
+
const EventEmitter_1 = __importDefault(require("./utils/EventEmitter"));
|
|
10
|
+
const isEqual_1 = __importDefault(require("./utils/isEqual"));
|
|
11
|
+
const match_1 = __importDefault(require("./utils/match"));
|
|
12
|
+
const modify_1 = __importDefault(require("./utils/modify"));
|
|
13
|
+
const project_1 = __importDefault(require("./utils/project"));
|
|
14
|
+
const queryId_1 = __importDefault(require("./utils/queryId"));
|
|
15
|
+
const serializeValue_1 = __importDefault(require("./utils/serializeValue"));
|
|
16
|
+
const sortItems_1 = __importDefault(require("./utils/sortItems"));
|
|
17
|
+
/**
|
|
18
|
+
* Checks if there are any pending updates in the given changeset.
|
|
19
|
+
* @template T - The type of the items in the changeset.
|
|
20
|
+
* @param pendingUpdates - The changeset to check for pending updates.
|
|
21
|
+
* @returns `true` if there are pending updates, otherwise `false`.
|
|
22
|
+
*/
|
|
23
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
24
|
+
return pendingUpdates.added.length > 0
|
|
25
|
+
|| pendingUpdates.modified.length > 0
|
|
26
|
+
|| pendingUpdates.removed.length > 0;
|
|
27
|
+
}
|
|
28
|
+
class DefaultDataAdapter {
|
|
29
|
+
items = new Map();
|
|
30
|
+
options;
|
|
31
|
+
storageAdapters = new Map();
|
|
32
|
+
collections = new Set();
|
|
33
|
+
indices = new Map();
|
|
34
|
+
activeQueries = new Map();
|
|
35
|
+
queryEmitters = new Map();
|
|
36
|
+
queuedQueryUpdates = new Map();
|
|
37
|
+
cachedQueryResults = new Map();
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.options = options || {};
|
|
40
|
+
}
|
|
41
|
+
ensureStorageAdapter(name) {
|
|
42
|
+
if (this.storageAdapters.get(name))
|
|
43
|
+
return; // already created
|
|
44
|
+
if (!this.options.storage)
|
|
45
|
+
return; // no storage function provided
|
|
46
|
+
const adapter = this.options.storage(name);
|
|
47
|
+
if (!adapter)
|
|
48
|
+
return; // no adapter returned
|
|
49
|
+
this.storageAdapters.set(name, adapter);
|
|
50
|
+
}
|
|
51
|
+
rebuildIndices(collection) {
|
|
52
|
+
const items = this.items.get(collection.name);
|
|
53
|
+
if (!items)
|
|
54
|
+
throw new Error(`Items not found for collection ${collection.name}`);
|
|
55
|
+
const indices = this.indices.get(collection.name);
|
|
56
|
+
if (!indices)
|
|
57
|
+
throw new Error(`Indices not found for collection ${collection.name}`);
|
|
58
|
+
indices.forEach(index => index.rebuild([...items.values()]));
|
|
59
|
+
}
|
|
60
|
+
async setupStorageAdapter(collection) {
|
|
61
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
62
|
+
if (!storageAdapter)
|
|
63
|
+
return; // no persistence adapter available
|
|
64
|
+
return storageAdapter.setup()
|
|
65
|
+
.then(async () => {
|
|
66
|
+
await this.fetchItemsFromStorage(collection);
|
|
67
|
+
})
|
|
68
|
+
.catch((error) => {
|
|
69
|
+
if (!this.options.onError) {
|
|
70
|
+
// eslint-disable-next-line no-console
|
|
71
|
+
console.error(`Error during data persistence operation in collection ${collection.name}`, error);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
getIndexInfo(collection, selector) {
|
|
78
|
+
if (selector != null
|
|
79
|
+
&& Object.keys(selector).length === 1
|
|
80
|
+
&& 'id' in selector
|
|
81
|
+
&& typeof selector.id !== 'object') {
|
|
82
|
+
return {
|
|
83
|
+
matched: true,
|
|
84
|
+
ids: [(0, serializeValue_1.default)(selector.id)],
|
|
85
|
+
optimizedSelector: {},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (selector == null) {
|
|
89
|
+
return {
|
|
90
|
+
matched: false,
|
|
91
|
+
ids: [],
|
|
92
|
+
optimizedSelector: {},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return (0, getIndexInfo_1.default)((this.indices.get(collection.name) ?? []).map(i => i.query.bind(i)), selector);
|
|
96
|
+
}
|
|
97
|
+
applyIndexDeltas(collection, changes) {
|
|
98
|
+
const indices = this.indices.get(collection.name) ?? [];
|
|
99
|
+
for (const index of indices) {
|
|
100
|
+
if (changes.modified)
|
|
101
|
+
index.update(changes.modified);
|
|
102
|
+
if (changes.added && changes.added.length > 0)
|
|
103
|
+
index.insert(changes.added);
|
|
104
|
+
if (changes.removed && changes.removed.length > 0)
|
|
105
|
+
index.remove(changes.removed);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
getItem(collection, selector) {
|
|
109
|
+
const memory = this.items.get(collection.name) ?? new Map();
|
|
110
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
111
|
+
const items = indexInfo.matched
|
|
112
|
+
? indexInfo.ids.map(id => memory.get((0, serializeValue_1.default)(id))).filter(i => i != null)
|
|
113
|
+
: [...memory.values()];
|
|
114
|
+
const item = items.find(document => (0, match_1.default)(document, selector));
|
|
115
|
+
return item;
|
|
116
|
+
}
|
|
117
|
+
queryItems(collection, selector) {
|
|
118
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
119
|
+
const matchItems = (item) => {
|
|
120
|
+
if (indexInfo.optimizedSelector == null)
|
|
121
|
+
return true; // if no selector is given, return all items
|
|
122
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
123
|
+
return true; // if selector is empty, return all items
|
|
124
|
+
const matches = (0, match_1.default)(item, indexInfo.optimizedSelector);
|
|
125
|
+
return matches;
|
|
126
|
+
};
|
|
127
|
+
const items = this.items.get(collection.name);
|
|
128
|
+
// no index available, use complete memory
|
|
129
|
+
if (!indexInfo.matched) {
|
|
130
|
+
if ((0, isEqual_1.default)(selector, {}))
|
|
131
|
+
return [...items.values()];
|
|
132
|
+
return [...items.values()].filter(matchItems);
|
|
133
|
+
}
|
|
134
|
+
const foundItems = indexInfo.ids.map(ids => items.get((0, serializeValue_1.default)(ids))).filter(i => i != null);
|
|
135
|
+
if ((0, isEqual_1.default)(indexInfo.optimizedSelector, {}))
|
|
136
|
+
return foundItems;
|
|
137
|
+
return foundItems.filter(matchItems);
|
|
138
|
+
}
|
|
139
|
+
executeQuery(collection, selector, options) {
|
|
140
|
+
const items = this.queryItems(collection, selector || {});
|
|
141
|
+
const { sort, skip, limit, fields } = options || {};
|
|
142
|
+
const sorted = sort ? (0, sortItems_1.default)(items, sort) : items;
|
|
143
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
144
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
145
|
+
const idExcluded = fields && fields.id === 0;
|
|
146
|
+
return limited.map((item) => {
|
|
147
|
+
if (!fields)
|
|
148
|
+
return item;
|
|
149
|
+
return {
|
|
150
|
+
...idExcluded ? {} : { id: item.id },
|
|
151
|
+
...(0, project_1.default)(item, fields),
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
flushQueuedQueryUpdates(collection) {
|
|
156
|
+
if (!this.queuedQueryUpdates.get(collection.name))
|
|
157
|
+
return;
|
|
158
|
+
const changes = this.queuedQueryUpdates.get(collection.name);
|
|
159
|
+
if (!changes || !hasPendingUpdates(changes))
|
|
160
|
+
return;
|
|
161
|
+
this.queuedQueryUpdates.set(collection.name, { added: [], modified: [], removed: [] });
|
|
162
|
+
const flatItems = [...changes.added, ...changes.modified, ...changes.removed];
|
|
163
|
+
const itemIds = new Set(flatItems.map(i => i.id));
|
|
164
|
+
const queries = [
|
|
165
|
+
...this.activeQueries.get(collection.name)?.values() ?? [],
|
|
166
|
+
].filter(({ selector, options }) => {
|
|
167
|
+
const idsInQuery = this.cachedQueryResults.get(collection.name)
|
|
168
|
+
?.get((0, queryId_1.default)(selector, options))
|
|
169
|
+
?.map(i => i.id) ?? [];
|
|
170
|
+
// update queries that contained changed items previously
|
|
171
|
+
if (idsInQuery.some(id => itemIds.has(id)))
|
|
172
|
+
return true;
|
|
173
|
+
return flatItems.some(item => (0, match_1.default)(item, selector));
|
|
174
|
+
});
|
|
175
|
+
queries.forEach(({ selector, options }) => {
|
|
176
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
executeAndCacheQuery(collection, selector, options) {
|
|
180
|
+
const result = this.executeQuery(collection, selector, options);
|
|
181
|
+
this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || new Map());
|
|
182
|
+
this.cachedQueryResults.get(collection.name)?.set((0, queryId_1.default)(selector, options), result);
|
|
183
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
184
|
+
if (!emitter)
|
|
185
|
+
return;
|
|
186
|
+
emitter.emit('change', selector, options, 'complete');
|
|
187
|
+
}
|
|
188
|
+
updateQueries(collection, changes) {
|
|
189
|
+
this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || { added: [], modified: [], removed: [] });
|
|
190
|
+
this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
|
|
191
|
+
this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
|
|
192
|
+
this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
|
|
193
|
+
this.flushQueuedQueryUpdates(collection);
|
|
194
|
+
}
|
|
195
|
+
createCollectionBackend(collection, indices) {
|
|
196
|
+
this.collections.add(collection);
|
|
197
|
+
this.ensureStorageAdapter(collection.name);
|
|
198
|
+
this.items.set(collection.name, this.items.get(collection.name) ?? new Map());
|
|
199
|
+
this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new EventEmitter_1.default());
|
|
200
|
+
// we might have many active queries
|
|
201
|
+
this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
|
|
202
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || new Map());
|
|
203
|
+
this.indices.set(collection.name, indices.map(field => (0, createIndex_1.default)(field)));
|
|
204
|
+
this.rebuildIndices(collection);
|
|
205
|
+
const persistenceReadyPromise = this.setupStorageAdapter(collection);
|
|
206
|
+
const backend = {
|
|
207
|
+
// CRUD operations
|
|
208
|
+
insert: async (newItem) => {
|
|
209
|
+
const items = this.items.get(collection.name);
|
|
210
|
+
if (!items)
|
|
211
|
+
throw new Error(`Items not found for collection ${collection.name}`);
|
|
212
|
+
if (items.has((0, serializeValue_1.default)(newItem.id))) {
|
|
213
|
+
throw new Error(`Item with id '${newItem.id}' already exists`);
|
|
214
|
+
}
|
|
215
|
+
this.items.get(collection.name)?.set((0, serializeValue_1.default)(newItem.id), newItem);
|
|
216
|
+
await this.storageAdapters.get(collection.name)?.insert([newItem]);
|
|
217
|
+
this.applyIndexDeltas(collection, { added: [newItem] });
|
|
218
|
+
this.updateQueries(collection, {
|
|
219
|
+
added: [],
|
|
220
|
+
modified: [newItem],
|
|
221
|
+
removed: [],
|
|
222
|
+
});
|
|
223
|
+
return newItem;
|
|
224
|
+
},
|
|
225
|
+
updateOne: async (selector, modifier) => {
|
|
226
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
227
|
+
const item = this.getItem(collection, selector);
|
|
228
|
+
if (item == null)
|
|
229
|
+
return []; // no item found
|
|
230
|
+
const modifiedItem = (0, modify_1.default)((0, deepClone_1.default)(item), restModifier);
|
|
231
|
+
const hasItemWithSameId = item.id !== modifiedItem.id
|
|
232
|
+
&& this.getItem(collection, { id: modifiedItem.id }) != null;
|
|
233
|
+
if (hasItemWithSameId) {
|
|
234
|
+
throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
235
|
+
}
|
|
236
|
+
this.items.get(collection.name)?.set((0, serializeValue_1.default)(modifiedItem.id), modifiedItem);
|
|
237
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
238
|
+
this.applyIndexDeltas(collection, { modified: [{ oldItem: item, newItem: modifiedItem }] });
|
|
239
|
+
this.updateQueries(collection, {
|
|
240
|
+
added: [],
|
|
241
|
+
modified: [modifiedItem],
|
|
242
|
+
removed: [],
|
|
243
|
+
});
|
|
244
|
+
return [modifiedItem];
|
|
245
|
+
},
|
|
246
|
+
updateMany: async (selector, modifier) => {
|
|
247
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
248
|
+
const items = this.executeQuery(collection, selector, {});
|
|
249
|
+
const changedItems = items.map((item) => {
|
|
250
|
+
const modifiedItem = (0, modify_1.default)((0, deepClone_1.default)(item), restModifier);
|
|
251
|
+
const hasItemWithSameId = item.id !== modifiedItem.id
|
|
252
|
+
&& this.getItem(collection, { id: modifiedItem.id }) != null;
|
|
253
|
+
if (hasItemWithSameId) {
|
|
254
|
+
throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
255
|
+
}
|
|
256
|
+
return modifiedItem;
|
|
257
|
+
});
|
|
258
|
+
changedItems.forEach((item) => {
|
|
259
|
+
this.items.get(collection.name)?.set((0, serializeValue_1.default)(item.id), item);
|
|
260
|
+
});
|
|
261
|
+
await this.storageAdapters.get(collection.name)?.replace(changedItems);
|
|
262
|
+
const pairs = items.map((oldItem, index) => ({ oldItem, newItem: changedItems[index] }));
|
|
263
|
+
this.applyIndexDeltas(collection, { modified: pairs });
|
|
264
|
+
this.updateQueries(collection, {
|
|
265
|
+
added: [],
|
|
266
|
+
modified: changedItems,
|
|
267
|
+
removed: [],
|
|
268
|
+
});
|
|
269
|
+
return changedItems;
|
|
270
|
+
},
|
|
271
|
+
replaceOne: async (selector, replacement) => {
|
|
272
|
+
const item = this.getItem(collection, selector);
|
|
273
|
+
if (item == null)
|
|
274
|
+
return []; // no item found
|
|
275
|
+
const hasItemWithSameId = item.id !== replacement.id
|
|
276
|
+
&& replacement.id != null
|
|
277
|
+
&& this.getItem(collection, { id: replacement.id }) != null;
|
|
278
|
+
if (hasItemWithSameId) {
|
|
279
|
+
throw new Error(`Item with id '${replacement.id}' already exists`);
|
|
280
|
+
}
|
|
281
|
+
const modifiedItem = { id: item.id, ...replacement };
|
|
282
|
+
this.items.get(collection.name)?.set((0, serializeValue_1.default)(modifiedItem.id), modifiedItem);
|
|
283
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
284
|
+
this.applyIndexDeltas(collection, { modified: [{ oldItem: item, newItem: modifiedItem }] });
|
|
285
|
+
this.updateQueries(collection, {
|
|
286
|
+
added: [],
|
|
287
|
+
modified: [modifiedItem],
|
|
288
|
+
removed: [],
|
|
289
|
+
});
|
|
290
|
+
return [modifiedItem];
|
|
291
|
+
},
|
|
292
|
+
removeOne: async (selector) => {
|
|
293
|
+
const item = this.getItem(collection, selector);
|
|
294
|
+
if (item == null)
|
|
295
|
+
return []; // no item found
|
|
296
|
+
this.items.get(collection.name)?.delete((0, serializeValue_1.default)(item.id));
|
|
297
|
+
await this.storageAdapters.get(collection.name)?.remove([item]);
|
|
298
|
+
this.applyIndexDeltas(collection, { removed: [item] });
|
|
299
|
+
this.updateQueries(collection, {
|
|
300
|
+
added: [],
|
|
301
|
+
modified: [],
|
|
302
|
+
removed: [item],
|
|
303
|
+
});
|
|
304
|
+
return [item];
|
|
305
|
+
},
|
|
306
|
+
removeMany: async (selector) => {
|
|
307
|
+
const items = backend.getQueryResult(selector, {});
|
|
308
|
+
items.forEach((item) => {
|
|
309
|
+
this.items.get(collection.name)?.delete((0, serializeValue_1.default)(item.id));
|
|
310
|
+
});
|
|
311
|
+
await this.storageAdapters.get(collection.name)?.remove(items);
|
|
312
|
+
this.applyIndexDeltas(collection, { removed: items });
|
|
313
|
+
this.updateQueries(collection, {
|
|
314
|
+
added: [],
|
|
315
|
+
modified: [],
|
|
316
|
+
removed: items,
|
|
317
|
+
});
|
|
318
|
+
return items;
|
|
319
|
+
},
|
|
320
|
+
// methods for registering and unregistering queries that will be called from the collection during find/findOne
|
|
321
|
+
registerQuery: (selector, options) => {
|
|
322
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || new Map());
|
|
323
|
+
this.activeQueries.get(collection.name)?.set((0, queryId_1.default)(selector, options), {
|
|
324
|
+
selector,
|
|
325
|
+
options,
|
|
326
|
+
});
|
|
327
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
328
|
+
},
|
|
329
|
+
unregisterQuery: (selector, options) => {
|
|
330
|
+
if (!this.activeQueries.get(collection.name))
|
|
331
|
+
return;
|
|
332
|
+
this.activeQueries.get(collection.name)?.delete((0, queryId_1.default)(selector, options));
|
|
333
|
+
},
|
|
334
|
+
getQueryState: () => 'complete',
|
|
335
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
336
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
337
|
+
if (!emitter)
|
|
338
|
+
throw new Error(`Query emitter not found for collection ${collection.name}`);
|
|
339
|
+
const handler = (querySelector, queryOptions, state) => {
|
|
340
|
+
if ((0, queryId_1.default)(querySelector, queryOptions) !== (0, queryId_1.default)(selector, options))
|
|
341
|
+
return;
|
|
342
|
+
callback(state);
|
|
343
|
+
};
|
|
344
|
+
emitter.on('change', handler);
|
|
345
|
+
return () => {
|
|
346
|
+
emitter.off('change', handler);
|
|
347
|
+
};
|
|
348
|
+
},
|
|
349
|
+
getQueryError: () => null,
|
|
350
|
+
getQueryResult: (selector, options) => {
|
|
351
|
+
const isQueryActive = this.activeQueries.get(collection.name)
|
|
352
|
+
?.has((0, queryId_1.default)(selector, options));
|
|
353
|
+
if (isQueryActive) {
|
|
354
|
+
const results = this.cachedQueryResults
|
|
355
|
+
.get(collection.name)
|
|
356
|
+
?.get((0, queryId_1.default)(selector, options));
|
|
357
|
+
if (!results)
|
|
358
|
+
throw new Error('Cached query results are not defined!');
|
|
359
|
+
return results;
|
|
360
|
+
}
|
|
361
|
+
return this.executeQuery(collection, selector, options);
|
|
362
|
+
},
|
|
363
|
+
executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
|
|
364
|
+
// lifecycle methods
|
|
365
|
+
dispose: async () => {
|
|
366
|
+
const adapter = this.storageAdapters.get(collection.name);
|
|
367
|
+
if (adapter?.teardown)
|
|
368
|
+
await adapter.teardown();
|
|
369
|
+
this.storageAdapters.delete(collection.name);
|
|
370
|
+
this.items.delete(collection.name);
|
|
371
|
+
this.indices.delete(collection.name);
|
|
372
|
+
this.activeQueries.delete(collection.name);
|
|
373
|
+
this.queryEmitters.delete(collection.name);
|
|
374
|
+
this.queuedQueryUpdates.delete(collection.name);
|
|
375
|
+
this.cachedQueryResults.delete(collection.name);
|
|
376
|
+
},
|
|
377
|
+
isReady: () => persistenceReadyPromise,
|
|
378
|
+
};
|
|
379
|
+
return backend;
|
|
380
|
+
}
|
|
381
|
+
async fetchItemsFromStorage(collection) {
|
|
382
|
+
if (!collection) {
|
|
383
|
+
await Promise.all([...this.collections]
|
|
384
|
+
.map(currentCollection => this.fetchItemsFromStorage(currentCollection)));
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
388
|
+
if (!storageAdapter)
|
|
389
|
+
return; // no persistence adapter available
|
|
390
|
+
const items = await storageAdapter.readAll();
|
|
391
|
+
this.items.set(collection.name, items.reduce((map, item) => {
|
|
392
|
+
map.set((0, serializeValue_1.default)(item.id), item);
|
|
393
|
+
return map;
|
|
394
|
+
}, new Map()));
|
|
395
|
+
this.rebuildIndices(collection);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
exports.default = DefaultDataAdapter;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { BaseItem } from './Collection';
|
|
2
|
+
import type Collection from './Collection';
|
|
3
|
+
import type DataAdapter from './DataAdapter';
|
|
4
|
+
import type { CollectionBackend } from './DataAdapter';
|
|
5
|
+
interface WorkerDataAdapterOptions {
|
|
6
|
+
id?: string;
|
|
7
|
+
log?: (message: string, ...args: any[]) => void;
|
|
8
|
+
}
|
|
9
|
+
export default class WorkerDataAdapter implements DataAdapter {
|
|
10
|
+
private worker;
|
|
11
|
+
private options;
|
|
12
|
+
private id;
|
|
13
|
+
private isDisposed;
|
|
14
|
+
private workerReady;
|
|
15
|
+
private log;
|
|
16
|
+
private collectionReady;
|
|
17
|
+
private batchExecutionHelpers;
|
|
18
|
+
private queries;
|
|
19
|
+
constructor(worker: Worker, options: WorkerDataAdapterOptions);
|
|
20
|
+
private exec;
|
|
21
|
+
private enqueueBatched;
|
|
22
|
+
private updateQuery;
|
|
23
|
+
createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const queryId_1 = __importDefault(require("./utils/queryId"));
|
|
7
|
+
const randomId_1 = __importDefault(require("./utils/randomId"));
|
|
8
|
+
const batchOnNextTick_1 = __importDefault(require("./utils/batchOnNextTick"));
|
|
9
|
+
class WorkerDataAdapter {
|
|
10
|
+
worker;
|
|
11
|
+
options;
|
|
12
|
+
id;
|
|
13
|
+
isDisposed = false;
|
|
14
|
+
workerReady;
|
|
15
|
+
log = () => { };
|
|
16
|
+
collectionReady = new Map();
|
|
17
|
+
batchExecutionHelpers = new Map();
|
|
18
|
+
queries = {};
|
|
19
|
+
constructor(worker, options) {
|
|
20
|
+
this.worker = worker;
|
|
21
|
+
this.options = options;
|
|
22
|
+
this.id = this.options.id || 'default-worker-data-adapter';
|
|
23
|
+
if (this.options.log)
|
|
24
|
+
this.log = this.options.log;
|
|
25
|
+
this.workerReady = new Promise((resolve, reject) => {
|
|
26
|
+
const timeoutId = setTimeout(() => {
|
|
27
|
+
reject(new Error('WorkerDataAdapter initialization timed out'));
|
|
28
|
+
}, 5000);
|
|
29
|
+
const handleMessage = (event) => {
|
|
30
|
+
const { type, workerId } = event.data;
|
|
31
|
+
if (workerId !== this.id)
|
|
32
|
+
return;
|
|
33
|
+
if (type === 'ready') {
|
|
34
|
+
resolve();
|
|
35
|
+
clearTimeout(timeoutId);
|
|
36
|
+
this.worker.removeEventListener('message', handleMessage);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
this.worker.addEventListener('message', handleMessage);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async exec(method, collectionName, ...args) {
|
|
43
|
+
await this.workerReady;
|
|
44
|
+
if (method !== 'isReady') {
|
|
45
|
+
const collectionReady = this.collectionReady.get(collectionName);
|
|
46
|
+
if (!collectionReady)
|
|
47
|
+
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
48
|
+
await collectionReady;
|
|
49
|
+
}
|
|
50
|
+
if (this.isDisposed) {
|
|
51
|
+
throw new Error('WorkerDataAdapter is disposed');
|
|
52
|
+
}
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const messageId = (0, randomId_1.default)();
|
|
55
|
+
const handleMessage = (event) => {
|
|
56
|
+
const { id, workerId, type, data, error } = event.data;
|
|
57
|
+
if (workerId !== this.id)
|
|
58
|
+
return;
|
|
59
|
+
if (type !== 'response')
|
|
60
|
+
return;
|
|
61
|
+
if (id !== messageId)
|
|
62
|
+
return;
|
|
63
|
+
this.log(method, 'result', data ?? error);
|
|
64
|
+
if (error) {
|
|
65
|
+
reject(error);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
resolve(data);
|
|
69
|
+
}
|
|
70
|
+
this.worker.removeEventListener('message', handleMessage);
|
|
71
|
+
};
|
|
72
|
+
this.worker.addEventListener('message', handleMessage);
|
|
73
|
+
this.worker.postMessage({
|
|
74
|
+
id: messageId,
|
|
75
|
+
workerId: this.id,
|
|
76
|
+
method,
|
|
77
|
+
args: [collectionName, ...args],
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
enqueueBatched(collectionName, method, args) {
|
|
82
|
+
const helper = this.batchExecutionHelpers.get(collectionName);
|
|
83
|
+
if (!helper)
|
|
84
|
+
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
85
|
+
return helper.enqueue(method, args);
|
|
86
|
+
}
|
|
87
|
+
// ---------- end batching integration ----------
|
|
88
|
+
updateQuery(collectionName, query, update) {
|
|
89
|
+
const id = (0, queryId_1.default)(query.selector, query.options);
|
|
90
|
+
const collectionQueries = this.queries[collectionName];
|
|
91
|
+
if (!collectionQueries)
|
|
92
|
+
return;
|
|
93
|
+
const existing = collectionQueries.get(id);
|
|
94
|
+
const newState = {
|
|
95
|
+
state: 'active',
|
|
96
|
+
error: null,
|
|
97
|
+
items: [],
|
|
98
|
+
stateChangeCallbacks: [],
|
|
99
|
+
eventHandler: existing?.eventHandler,
|
|
100
|
+
...existing,
|
|
101
|
+
...update,
|
|
102
|
+
};
|
|
103
|
+
collectionQueries.set(id, newState);
|
|
104
|
+
this.queries[collectionName] = collectionQueries;
|
|
105
|
+
}
|
|
106
|
+
createCollectionBackend(collection, indices) {
|
|
107
|
+
this.queries[collection.name] = new Map();
|
|
108
|
+
void this.exec('registerCollection', collection.name, indices);
|
|
109
|
+
this.collectionReady.set(collection.name, this.exec('isReady', collection.name));
|
|
110
|
+
this.batchExecutionHelpers.set(collection.name, (0, batchOnNextTick_1.default)(async (method, args) => this.exec(method, collection.name, args)));
|
|
111
|
+
return {
|
|
112
|
+
insert: async (item) => {
|
|
113
|
+
return this.enqueueBatched(collection.name, 'insert', [item]);
|
|
114
|
+
},
|
|
115
|
+
updateOne: async (selector, modifier) => {
|
|
116
|
+
return this.enqueueBatched(collection.name, 'updateOne', [selector, modifier]);
|
|
117
|
+
},
|
|
118
|
+
updateMany: async (selector, modifier) => {
|
|
119
|
+
return this.enqueueBatched(collection.name, 'updateMany', [selector, modifier]);
|
|
120
|
+
},
|
|
121
|
+
replaceOne: async (selector, replacement) => {
|
|
122
|
+
return this.enqueueBatched(collection.name, 'replaceOne', [selector, replacement]);
|
|
123
|
+
},
|
|
124
|
+
removeOne: async (selector) => {
|
|
125
|
+
return this.enqueueBatched(collection.name, 'removeOne', [selector]);
|
|
126
|
+
},
|
|
127
|
+
removeMany: async (selector) => {
|
|
128
|
+
return this.enqueueBatched(collection.name, 'removeMany', [selector]);
|
|
129
|
+
},
|
|
130
|
+
// methods for registering and unregistering queries that will be called from the collection during find/findOne
|
|
131
|
+
registerQuery: (selector, options) => {
|
|
132
|
+
this.updateQuery(collection.name, { selector, options }, { state: 'active', error: null, items: [] });
|
|
133
|
+
void this.exec('registerQuery', collection.name, selector, options);
|
|
134
|
+
const handler = (event) => {
|
|
135
|
+
const { type, data, workerId, error } = event.data;
|
|
136
|
+
if (type !== 'queryUpdate')
|
|
137
|
+
return;
|
|
138
|
+
if (data == null)
|
|
139
|
+
return;
|
|
140
|
+
const { collectionName, selector: responseSelector, options: responseOptions, state, items, } = data;
|
|
141
|
+
if (workerId !== this.id)
|
|
142
|
+
return;
|
|
143
|
+
if (collectionName !== collection.name)
|
|
144
|
+
return;
|
|
145
|
+
if ((0, queryId_1.default)(responseSelector, responseOptions) !== (0, queryId_1.default)(selector, options))
|
|
146
|
+
return;
|
|
147
|
+
this.log('queryUpdate', responseSelector, responseOptions, state, data ?? error);
|
|
148
|
+
this.updateQuery(collection.name, {
|
|
149
|
+
selector: responseSelector,
|
|
150
|
+
options: responseOptions,
|
|
151
|
+
}, { state, error, items });
|
|
152
|
+
const query = this.queries[collection.name]?.get((0, queryId_1.default)(selector, options));
|
|
153
|
+
if (!query)
|
|
154
|
+
return;
|
|
155
|
+
query.stateChangeCallbacks.forEach(callback => callback(state));
|
|
156
|
+
};
|
|
157
|
+
this.worker.addEventListener('message', handler);
|
|
158
|
+
this.updateQuery(collection.name, { selector, options }, { eventHandler: handler });
|
|
159
|
+
},
|
|
160
|
+
unregisterQuery: (selector, options) => {
|
|
161
|
+
const qid = (0, queryId_1.default)(selector, options);
|
|
162
|
+
const query = this.queries[collection.name]?.get(qid);
|
|
163
|
+
if (query?.eventHandler) {
|
|
164
|
+
this.worker.removeEventListener('message', query.eventHandler);
|
|
165
|
+
}
|
|
166
|
+
this.queries[collection.name]?.delete(qid);
|
|
167
|
+
void this.exec('unregisterQuery', collection.name, selector, options);
|
|
168
|
+
},
|
|
169
|
+
getQueryState: (selector, options) => {
|
|
170
|
+
const query = this.queries[collection.name]?.get((0, queryId_1.default)(selector, options));
|
|
171
|
+
return query?.state || 'active';
|
|
172
|
+
},
|
|
173
|
+
getQueryError: (selector, options) => {
|
|
174
|
+
const query = this.queries[collection.name]?.get((0, queryId_1.default)(selector, options));
|
|
175
|
+
return query?.error || null;
|
|
176
|
+
},
|
|
177
|
+
getQueryResult: (selector, options) => {
|
|
178
|
+
const query = this.queries[collection.name]?.get((0, queryId_1.default)(selector, options));
|
|
179
|
+
return query?.items || [];
|
|
180
|
+
},
|
|
181
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
182
|
+
this.updateQuery(collection.name, { selector, options }, {
|
|
183
|
+
stateChangeCallbacks: [
|
|
184
|
+
...this.queries[collection.name]?.get((0, queryId_1.default)(selector, options))?.stateChangeCallbacks || [],
|
|
185
|
+
callback,
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
return () => {
|
|
189
|
+
const currentCallbacks = this.queries[collection.name]?.get((0, queryId_1.default)(selector, options))?.stateChangeCallbacks;
|
|
190
|
+
if (!currentCallbacks)
|
|
191
|
+
throw new Error('State change callbacks are not defined!');
|
|
192
|
+
this.updateQuery(collection.name, { selector, options }, {
|
|
193
|
+
stateChangeCallbacks: currentCallbacks
|
|
194
|
+
.filter(existingCallback => existingCallback !== callback),
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
executeQuery: (selector, options) => this.exec('executeQuery', collection.name, selector, options),
|
|
199
|
+
// lifecycle methods
|
|
200
|
+
dispose: async () => {
|
|
201
|
+
await this.exec('unregisterCollection', collection.name);
|
|
202
|
+
this.isDisposed = true;
|
|
203
|
+
this.worker.terminate();
|
|
204
|
+
},
|
|
205
|
+
isReady: async () => {
|
|
206
|
+
await this.exec('isReady', collection.name);
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.default = WorkerDataAdapter;
|