@signaldb/core 1.2.4 → 1.3.1
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 +28 -47
- package/dist/index.cjs.js +12 -18
- package/dist/index.cjs10.js +6 -27
- package/dist/index.cjs11.js +3 -6
- package/dist/index.cjs12.js +136 -3
- package/dist/index.cjs13.js +61 -131
- package/dist/index.cjs14.js +5 -38
- package/dist/index.cjs15.js +42 -2
- package/dist/index.cjs16.js +11 -5
- package/dist/index.cjs17.js +17 -40
- package/dist/index.cjs18.js +262 -11
- package/dist/index.cjs19.js +66 -16
- package/dist/index.cjs2.js +688 -96
- package/dist/index.cjs20.js +71 -247
- package/dist/index.cjs21.js +8 -68
- package/dist/index.cjs22.js +22 -83
- package/dist/index.cjs23.js +144 -8
- package/dist/index.cjs24.js +5 -25
- package/dist/index.cjs25.js +16 -144
- package/dist/index.cjs26.js +27 -5
- package/dist/index.cjs27.js +39 -15
- package/dist/index.cjs28.js +25 -24
- package/dist/index.cjs29.js +7 -40
- package/dist/index.cjs3.js +139 -664
- package/dist/index.cjs4.js +62 -166
- package/dist/index.cjs5.js +3 -67
- package/dist/index.cjs6.js +2 -2
- package/dist/index.cjs7.js +2 -2
- package/dist/index.cjs8.js +2 -2
- package/dist/index.cjs9.js +27 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +12 -18
- package/dist/index10.mjs +6 -27
- package/dist/index11.mjs +3 -6
- package/dist/index12.mjs +136 -3
- package/dist/index13.mjs +60 -131
- package/dist/index14.mjs +5 -37
- package/dist/index15.mjs +41 -2
- package/dist/index16.mjs +11 -5
- package/dist/index17.mjs +17 -39
- package/dist/index18.mjs +261 -11
- package/dist/index19.mjs +65 -16
- package/dist/index2.mjs +688 -94
- package/dist/index20.mjs +71 -247
- package/dist/index21.mjs +8 -67
- package/dist/index22.mjs +22 -82
- package/dist/index23.mjs +144 -8
- package/dist/index24.mjs +5 -25
- package/dist/index25.mjs +16 -144
- package/dist/index26.mjs +27 -5
- package/dist/index27.mjs +39 -15
- package/dist/index28.mjs +25 -24
- package/dist/index29.mjs +7 -40
- package/dist/index3.mjs +139 -664
- package/dist/index4.mjs +61 -166
- package/dist/index5.mjs +3 -66
- package/dist/index6.mjs +2 -2
- package/dist/index7.mjs +2 -2
- package/dist/index8.mjs +2 -2
- package/dist/index9.mjs +27 -3
- package/dist/utils/getMatchingKeys.d.ts +11 -6
- package/package.json +1 -1
- package/dist/devtools.d.ts +0 -4
- package/dist/index.cjs30.js +0 -29
- package/dist/index.cjs31.js +0 -9
- package/dist/index30.mjs +0 -30
- package/dist/index31.mjs +0 -10
package/dist/index2.mjs
CHANGED
|
@@ -1,110 +1,704 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import EventEmitter from "./index12.mjs";
|
|
5
|
+
import match from "./index14.mjs";
|
|
6
|
+
import modify from "./index10.mjs";
|
|
7
|
+
import isEqual from "./index9.mjs";
|
|
8
|
+
import randomId from "./index11.mjs";
|
|
9
|
+
import deepClone from "./index15.mjs";
|
|
10
|
+
import serializeValue from "./index16.mjs";
|
|
11
|
+
import createSignal from "./index17.mjs";
|
|
12
|
+
import Cursor from "./index18.mjs";
|
|
13
|
+
import getIndexInfo from "./index19.mjs";
|
|
14
|
+
import { createExternalIndex } from "./index13.mjs";
|
|
15
|
+
import { default as default2 } from "./index13.mjs";
|
|
16
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
17
|
+
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
16
18
|
}
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
function applyUpdates(currentItems, { added, modified, removed }) {
|
|
20
|
+
const items = [...currentItems];
|
|
21
|
+
added.forEach((item) => {
|
|
22
|
+
items.push(item);
|
|
23
|
+
});
|
|
24
|
+
modified.forEach((item) => {
|
|
25
|
+
const index = items.findIndex(({ id }) => id === item.id);
|
|
26
|
+
if (index === -1)
|
|
27
|
+
return;
|
|
28
|
+
items[index] = item;
|
|
29
|
+
});
|
|
30
|
+
removed.forEach((item) => {
|
|
31
|
+
const index = items.findIndex(({ id }) => id === item.id);
|
|
32
|
+
if (index === -1)
|
|
33
|
+
return;
|
|
34
|
+
items.splice(index, 1);
|
|
35
|
+
});
|
|
36
|
+
return items;
|
|
25
37
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
const _Collection = class _Collection extends EventEmitter {
|
|
39
|
+
/**
|
|
40
|
+
* Initializes a new instance of the `Collection` class with optional configuration.
|
|
41
|
+
* Sets up memory, persistence, reactivity, and indices as specified in the options.
|
|
42
|
+
* @template T - The type of the items stored in the collection.
|
|
43
|
+
* @template I - The type of the unique identifier for the items.
|
|
44
|
+
* @template U - The transformed item type after applying transformations (default is T).
|
|
45
|
+
* @param options - Optional configuration for the collection.
|
|
46
|
+
* @param options.name - An optional name for the collection.
|
|
47
|
+
* @param options.memory - The in-memory adapter for storing items.
|
|
48
|
+
* @param options.reactivity - The reactivity adapter for observing changes in the collection.
|
|
49
|
+
* @param options.transform - A transformation function to apply to items when retrieving them.
|
|
50
|
+
* @param options.persistence - The persistence adapter for saving and loading items.
|
|
51
|
+
* @param options.indices - An array of index providers for optimized querying.
|
|
52
|
+
* @param options.enableDebugMode - A boolean to enable or disable debug mode.
|
|
53
|
+
* @param options.fieldTracking - A boolean to enable or disable field tracking by default.
|
|
54
|
+
*/
|
|
55
|
+
constructor(options) {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
super();
|
|
58
|
+
__publicField(this, "name");
|
|
59
|
+
__publicField(this, "options");
|
|
60
|
+
__publicField(this, "persistenceAdapter", null);
|
|
61
|
+
__publicField(this, "isPullingSignal");
|
|
62
|
+
__publicField(this, "isPushingSignal");
|
|
63
|
+
__publicField(this, "indexProviders", []);
|
|
64
|
+
__publicField(this, "indicesOutdated", false);
|
|
65
|
+
__publicField(this, "idIndex", /* @__PURE__ */ new Map());
|
|
66
|
+
__publicField(this, "debugMode");
|
|
67
|
+
__publicField(this, "batchOperationInProgress", false);
|
|
68
|
+
__publicField(this, "isDisposed", false);
|
|
69
|
+
__publicField(this, "postBatchCallbacks", /* @__PURE__ */ new Set());
|
|
70
|
+
__publicField(this, "fieldTracking", false);
|
|
71
|
+
__publicField(this, "persistenceReadyPromise");
|
|
72
|
+
_Collection.collections.push(this);
|
|
73
|
+
this.name = (options == null ? void 0 : options.name) ?? `${this.constructor.name}-${randomId()}`;
|
|
74
|
+
this.options = {
|
|
75
|
+
memory: [],
|
|
76
|
+
...options
|
|
77
|
+
};
|
|
78
|
+
this.fieldTracking = this.options.fieldTracking ?? _Collection.fieldTracking;
|
|
79
|
+
this.debugMode = this.options.enableDebugMode ?? _Collection.debugMode;
|
|
80
|
+
this.indexProviders = [
|
|
81
|
+
createExternalIndex("id", this.idIndex),
|
|
82
|
+
...this.options.indices || []
|
|
83
|
+
];
|
|
84
|
+
this.rebuildIndices();
|
|
85
|
+
this.isPullingSignal = createSignal((_a = this.options.reactivity) == null ? void 0 : _a.create(), !!(options == null ? void 0 : options.persistence));
|
|
86
|
+
this.isPushingSignal = createSignal((_b = this.options.reactivity) == null ? void 0 : _b.create(), false);
|
|
87
|
+
this.on("persistence.pullStarted", () => {
|
|
88
|
+
this.isPullingSignal.set(true);
|
|
89
|
+
});
|
|
90
|
+
this.on("persistence.pullCompleted", () => {
|
|
91
|
+
this.isPullingSignal.set(false);
|
|
92
|
+
});
|
|
93
|
+
this.on("persistence.pushStarted", () => {
|
|
94
|
+
this.isPushingSignal.set(true);
|
|
95
|
+
});
|
|
96
|
+
this.on("persistence.pushCompleted", () => {
|
|
97
|
+
this.isPushingSignal.set(false);
|
|
98
|
+
});
|
|
99
|
+
this.persistenceAdapter = this.options.persistence ?? null;
|
|
100
|
+
if (this.persistenceAdapter) {
|
|
101
|
+
let ongoingSaves = 0;
|
|
102
|
+
let isInitialized = false;
|
|
103
|
+
const pendingUpdates = { added: [], modified: [], removed: [] };
|
|
104
|
+
const loadPersistentData = async (data) => {
|
|
105
|
+
if (!this.persistenceAdapter)
|
|
106
|
+
throw new Error("Persistence adapter not found");
|
|
107
|
+
this.emit("persistence.pullStarted");
|
|
108
|
+
const { items, changes } = data ?? await this.persistenceAdapter.load();
|
|
109
|
+
if (items) {
|
|
110
|
+
if (ongoingSaves > 0)
|
|
111
|
+
return;
|
|
112
|
+
this.memory().splice(0, this.memoryArray().length, ...items);
|
|
113
|
+
this.idIndex.clear();
|
|
114
|
+
this.memory().map((item, index) => {
|
|
115
|
+
this.idIndex.set(serializeValue(item.id), /* @__PURE__ */ new Set([index]));
|
|
116
|
+
});
|
|
117
|
+
} else if (changes) {
|
|
118
|
+
changes.added.forEach((item) => {
|
|
119
|
+
const index = this.memory().findIndex((document) => document.id === item.id);
|
|
120
|
+
if (index !== -1) {
|
|
121
|
+
this.memory().splice(index, 1, item);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.memory().push(item);
|
|
125
|
+
const itemIndex = this.memory().findIndex((document) => document === item);
|
|
126
|
+
this.idIndex.set(serializeValue(item.id), /* @__PURE__ */ new Set([itemIndex]));
|
|
127
|
+
});
|
|
128
|
+
changes.modified.forEach((item) => {
|
|
129
|
+
const index = this.memory().findIndex((document) => document.id === item.id);
|
|
130
|
+
if (index === -1)
|
|
131
|
+
throw new Error("Cannot resolve index for item");
|
|
132
|
+
this.memory().splice(index, 1, item);
|
|
133
|
+
});
|
|
134
|
+
changes.removed.forEach((item) => {
|
|
135
|
+
const index = this.memory().findIndex((document) => document.id === item.id);
|
|
136
|
+
if (index === -1)
|
|
137
|
+
throw new Error("Cannot resolve index for item");
|
|
138
|
+
this.memory().splice(index, 1);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
this.rebuildIndices();
|
|
142
|
+
this.emit("persistence.received");
|
|
143
|
+
setTimeout(() => this.emit("persistence.pullCompleted"), 0);
|
|
144
|
+
};
|
|
145
|
+
const saveQueue = {
|
|
146
|
+
added: [],
|
|
147
|
+
modified: [],
|
|
148
|
+
removed: []
|
|
149
|
+
};
|
|
150
|
+
let isFlushing = false;
|
|
151
|
+
const flushQueue = () => {
|
|
152
|
+
if (!this.persistenceAdapter)
|
|
153
|
+
throw new Error("Persistence adapter not found");
|
|
154
|
+
if (ongoingSaves <= 0)
|
|
155
|
+
this.emit("persistence.pushStarted");
|
|
156
|
+
if (isFlushing)
|
|
157
|
+
return;
|
|
158
|
+
if (!hasPendingUpdates(saveQueue))
|
|
159
|
+
return;
|
|
160
|
+
isFlushing = true;
|
|
161
|
+
ongoingSaves += 1;
|
|
162
|
+
const currentItems = this.memoryArray();
|
|
163
|
+
const changes = { ...saveQueue };
|
|
164
|
+
saveQueue.added = [];
|
|
165
|
+
saveQueue.modified = [];
|
|
166
|
+
saveQueue.removed = [];
|
|
167
|
+
this.persistenceAdapter.save(currentItems, changes).then(() => {
|
|
168
|
+
this.emit("persistence.transmitted");
|
|
169
|
+
}).catch((error) => {
|
|
170
|
+
this.emit("persistence.error", error instanceof Error ? error : new Error(error));
|
|
171
|
+
}).finally(() => {
|
|
172
|
+
ongoingSaves -= 1;
|
|
173
|
+
isFlushing = false;
|
|
174
|
+
flushQueue();
|
|
175
|
+
if (ongoingSaves <= 0)
|
|
176
|
+
this.emit("persistence.pushCompleted");
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
this.on("added", (item) => {
|
|
180
|
+
if (!isInitialized) {
|
|
181
|
+
pendingUpdates.added.push(item);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
saveQueue.added.push(item);
|
|
185
|
+
flushQueue();
|
|
186
|
+
});
|
|
187
|
+
this.on("changed", (item) => {
|
|
188
|
+
if (!isInitialized) {
|
|
189
|
+
pendingUpdates.modified.push(item);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
saveQueue.modified.push(item);
|
|
193
|
+
flushQueue();
|
|
194
|
+
});
|
|
195
|
+
this.on("removed", (item) => {
|
|
196
|
+
if (!isInitialized) {
|
|
197
|
+
pendingUpdates.removed.push(item);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
saveQueue.removed.push(item);
|
|
201
|
+
flushQueue();
|
|
202
|
+
});
|
|
203
|
+
this.persistenceAdapter.register((data) => loadPersistentData(data)).then(async () => {
|
|
204
|
+
if (!this.persistenceAdapter)
|
|
205
|
+
throw new Error("Persistence adapter not found");
|
|
206
|
+
let currentItems = this.memoryArray();
|
|
207
|
+
await loadPersistentData();
|
|
208
|
+
while (hasPendingUpdates(pendingUpdates)) {
|
|
209
|
+
const added = pendingUpdates.added.splice(0);
|
|
210
|
+
const modified = pendingUpdates.modified.splice(0);
|
|
211
|
+
const removed = pendingUpdates.removed.splice(0);
|
|
212
|
+
currentItems = applyUpdates(this.memoryArray(), { added, modified, removed });
|
|
213
|
+
await this.persistenceAdapter.save(currentItems, { added, modified, removed }).then(() => {
|
|
214
|
+
this.emit("persistence.transmitted");
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
await loadPersistentData();
|
|
218
|
+
isInitialized = true;
|
|
219
|
+
setTimeout(() => this.emit("persistence.init"), 0);
|
|
220
|
+
}).catch((error) => {
|
|
221
|
+
this.emit("persistence.error", error instanceof Error ? error : new Error(error));
|
|
222
|
+
});
|
|
36
223
|
}
|
|
224
|
+
this.persistenceReadyPromise = new Promise((resolve, reject) => {
|
|
225
|
+
if (!this.persistenceAdapter)
|
|
226
|
+
return resolve();
|
|
227
|
+
this.once("persistence.init", resolve);
|
|
228
|
+
this.once("persistence.error", reject);
|
|
229
|
+
});
|
|
230
|
+
_Collection.onCreationCallbacks.forEach((callback) => callback(this));
|
|
231
|
+
}
|
|
232
|
+
static getCollections() {
|
|
233
|
+
return _Collection.collections;
|
|
234
|
+
}
|
|
235
|
+
static onCreation(callback) {
|
|
236
|
+
_Collection.onCreationCallbacks.push(callback);
|
|
237
|
+
}
|
|
238
|
+
static onDispose(callback) {
|
|
239
|
+
_Collection.onDisposeCallbacks.push(callback);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Executes a batch operation, allowing multiple modifications to the collection
|
|
243
|
+
* while deferring index rebuilding until all operations in the batch are completed.
|
|
244
|
+
* This improves performance by avoiding repetitive index recalculations and
|
|
245
|
+
* provides atomicity for the batch of operations.
|
|
246
|
+
* @param callback - The batch operation to execute.
|
|
247
|
+
*/
|
|
248
|
+
static batch(callback) {
|
|
249
|
+
_Collection.batchOperationInProgress = true;
|
|
250
|
+
_Collection.collections.reduce((memo, collection) => () => collection.batch(() => memo()), callback)();
|
|
251
|
+
_Collection.batchOperationInProgress = false;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Checks whether the collection is currently performing a pull operation
|
|
255
|
+
* ⚡️ this function is reactive!
|
|
256
|
+
* (loading data from the persistence adapter).
|
|
257
|
+
* @returns A boolean indicating if the collection is in the process of pulling data.
|
|
258
|
+
*/
|
|
259
|
+
isPulling() {
|
|
260
|
+
return this.isPullingSignal.get() ?? false;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Checks whether the collection is currently performing a push operation
|
|
264
|
+
* ⚡️ this function is reactive!
|
|
265
|
+
* (saving data to the persistence adapter).
|
|
266
|
+
* @returns A boolean indicating if the collection is in the process of pushing data.
|
|
267
|
+
*/
|
|
268
|
+
isPushing() {
|
|
269
|
+
return this.isPushingSignal.get() ?? false;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Checks whether the collection is currently performing either a pull or push operation,
|
|
273
|
+
* ⚡️ this function is reactive!
|
|
274
|
+
* indicating that it is loading or saving data.
|
|
275
|
+
* @returns A boolean indicating if the collection is in the process of loading or saving data.
|
|
276
|
+
*/
|
|
277
|
+
isLoading() {
|
|
278
|
+
const isPulling = this.isPulling();
|
|
279
|
+
const isPushing = this.isPushing();
|
|
280
|
+
return isPulling || isPushing;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Retrieves the current debug mode status of the collection.
|
|
284
|
+
* @returns A boolean indicating whether debug mode is enabled for the collection.
|
|
285
|
+
*/
|
|
286
|
+
getDebugMode() {
|
|
287
|
+
return this.debugMode;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Enables or disables debug mode for the collection.
|
|
291
|
+
* When debug mode is enabled, additional debugging information and events are emitted.
|
|
292
|
+
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) debug mode.
|
|
293
|
+
*/
|
|
294
|
+
setDebugMode(enable) {
|
|
295
|
+
this.debugMode = enable;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Enables or disables field tracking for the collection.
|
|
299
|
+
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking.
|
|
300
|
+
*/
|
|
301
|
+
setFieldTracking(enable) {
|
|
302
|
+
this.fieldTracking = enable;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Resolves when the persistence adapter finished initializing
|
|
306
|
+
* and the collection is ready to be used.
|
|
307
|
+
* @returns A promise that resolves when the collection is ready.
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* const collection = new Collection({
|
|
311
|
+
* persistence: // ...
|
|
312
|
+
* })
|
|
313
|
+
* await collection.isReady()
|
|
314
|
+
*
|
|
315
|
+
* collection.insert({ name: 'Item 1' })
|
|
316
|
+
*/
|
|
317
|
+
async isReady() {
|
|
318
|
+
return this.persistenceReadyPromise;
|
|
319
|
+
}
|
|
320
|
+
profile(fn, measureFunction) {
|
|
321
|
+
if (!this.debugMode)
|
|
322
|
+
return fn();
|
|
323
|
+
const startTime = performance.now();
|
|
324
|
+
const result = fn();
|
|
325
|
+
const endTime = performance.now();
|
|
326
|
+
measureFunction(endTime - startTime);
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
executeInDebugMode(fn) {
|
|
330
|
+
if (!this.debugMode)
|
|
331
|
+
return;
|
|
332
|
+
const callstack = new Error().stack || "";
|
|
333
|
+
fn(callstack);
|
|
334
|
+
}
|
|
335
|
+
rebuildIndices() {
|
|
336
|
+
this.indicesOutdated = true;
|
|
337
|
+
if (this.batchOperationInProgress)
|
|
338
|
+
return;
|
|
339
|
+
this.rebuildAllIndices();
|
|
340
|
+
}
|
|
341
|
+
rebuildAllIndices() {
|
|
342
|
+
this.idIndex.clear();
|
|
343
|
+
this.memory().map((item, index) => {
|
|
344
|
+
this.idIndex.set(serializeValue(item.id), /* @__PURE__ */ new Set([index]));
|
|
345
|
+
});
|
|
346
|
+
this.indexProviders.forEach((index) => index.rebuild(this.memoryArray()));
|
|
347
|
+
this.indicesOutdated = false;
|
|
37
348
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (!isFailedDevToolsImportError(error)) {
|
|
46
|
-
throw error;
|
|
349
|
+
getIndexInfo(selector) {
|
|
350
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
351
|
+
return {
|
|
352
|
+
matched: true,
|
|
353
|
+
positions: [...this.idIndex.get(serializeValue(selector.id)) || []],
|
|
354
|
+
optimizedSelector: {}
|
|
355
|
+
};
|
|
47
356
|
}
|
|
357
|
+
if (selector == null || this.indicesOutdated) {
|
|
358
|
+
return {
|
|
359
|
+
matched: false,
|
|
360
|
+
positions: [],
|
|
361
|
+
optimizedSelector: {}
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
return getIndexInfo(this.indexProviders, selector);
|
|
48
365
|
}
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
366
|
+
getItemAndIndex(selector) {
|
|
367
|
+
const memory = this.memoryArray();
|
|
368
|
+
const indexInfo = this.getIndexInfo(selector);
|
|
369
|
+
const items = indexInfo.matched ? indexInfo.positions.map((index2) => memory[index2]) : memory;
|
|
370
|
+
const item = items.find((document) => match(document, selector));
|
|
371
|
+
const foundInIndex = indexInfo.matched && indexInfo.positions.find((itemIndex) => memory[itemIndex] === item);
|
|
372
|
+
const index = foundInIndex || memory.findIndex((document) => document === item);
|
|
373
|
+
if (item == null)
|
|
374
|
+
return { item: null, index: -1 };
|
|
375
|
+
if (index === -1)
|
|
376
|
+
throw new Error("Cannot resolve index for item");
|
|
377
|
+
return { item, index };
|
|
57
378
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
require(packageName);
|
|
66
|
-
return true;
|
|
67
|
-
} catch (error) {
|
|
68
|
-
if (isFailedDevToolsImportError(error)) {
|
|
69
|
-
return false;
|
|
379
|
+
deleteFromIdIndex(id, index) {
|
|
380
|
+
this.idIndex.delete(serializeValue(id));
|
|
381
|
+
if (!this.batchOperationInProgress)
|
|
382
|
+
return;
|
|
383
|
+
this.idIndex.forEach(([currenIndex], key) => {
|
|
384
|
+
if (currenIndex > index) {
|
|
385
|
+
this.idIndex.set(key, /* @__PURE__ */ new Set([currenIndex - 1]));
|
|
70
386
|
}
|
|
71
|
-
|
|
72
|
-
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
memory() {
|
|
390
|
+
return this.options.memory;
|
|
391
|
+
}
|
|
392
|
+
memoryArray() {
|
|
393
|
+
return this.memory().map((item) => item);
|
|
394
|
+
}
|
|
395
|
+
transform(item) {
|
|
396
|
+
if (!this.options.transform)
|
|
397
|
+
return item;
|
|
398
|
+
return this.options.transform(item);
|
|
399
|
+
}
|
|
400
|
+
getItems(selector) {
|
|
401
|
+
return this.profile(() => {
|
|
402
|
+
const indexInfo = this.getIndexInfo(selector);
|
|
403
|
+
const matchItems = (item) => {
|
|
404
|
+
if (indexInfo.optimizedSelector == null)
|
|
405
|
+
return true;
|
|
406
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
407
|
+
return true;
|
|
408
|
+
const matches = match(item, indexInfo.optimizedSelector);
|
|
409
|
+
return matches;
|
|
410
|
+
};
|
|
411
|
+
this.emit("getItems", selector);
|
|
412
|
+
const memory = this.memoryArray();
|
|
413
|
+
if (!indexInfo.matched) {
|
|
414
|
+
if (isEqual(selector, {}))
|
|
415
|
+
return memory;
|
|
416
|
+
return memory.filter(matchItems);
|
|
417
|
+
}
|
|
418
|
+
const items = indexInfo.positions.map((index) => memory[index]);
|
|
419
|
+
if (isEqual(indexInfo.optimizedSelector, {}))
|
|
420
|
+
return items;
|
|
421
|
+
return items.filter(matchItems);
|
|
422
|
+
}, (measuredTime) => this.executeInDebugMode((callstack) => this.emit("_debug.getItems", callstack, selector, measuredTime)));
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Disposes the collection, unregisters persistence adapters, clears memory, and
|
|
426
|
+
* cleans up all resources used by the collection.
|
|
427
|
+
* @returns A promise that resolves when the collection is disposed.
|
|
428
|
+
*/
|
|
429
|
+
async dispose() {
|
|
430
|
+
var _a;
|
|
431
|
+
if ((_a = this.persistenceAdapter) == null ? void 0 : _a.unregister)
|
|
432
|
+
await this.persistenceAdapter.unregister();
|
|
433
|
+
this.persistenceAdapter = null;
|
|
434
|
+
this.memory().map(() => this.memory().pop());
|
|
435
|
+
this.idIndex.clear();
|
|
436
|
+
this.indexProviders = [];
|
|
437
|
+
this.isDisposed = true;
|
|
438
|
+
this.removeAllListeners();
|
|
439
|
+
_Collection.collections = _Collection.collections.filter((collection) => collection !== this);
|
|
440
|
+
_Collection.onDisposeCallbacks.forEach((callback) => callback(this));
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Finds multiple items in the collection based on a selector and optional options.
|
|
444
|
+
* Returns a cursor for reactive data queries.
|
|
445
|
+
* @template O - The options type for the find operation.
|
|
446
|
+
* @param [selector] - The criteria to select items.
|
|
447
|
+
* @param [options] - Options for the find operation, such as limit and sort.
|
|
448
|
+
* @returns A cursor to fetch and observe the matching items.
|
|
449
|
+
*/
|
|
450
|
+
find(selector, options) {
|
|
451
|
+
if (this.isDisposed)
|
|
452
|
+
throw new Error("Collection is disposed");
|
|
453
|
+
if (selector !== void 0 && (!selector || typeof selector !== "object"))
|
|
454
|
+
throw new Error("Invalid selector");
|
|
455
|
+
const cursor = new Cursor(() => this.getItems(selector), {
|
|
456
|
+
reactive: this.options.reactivity,
|
|
457
|
+
fieldTracking: this.fieldTracking,
|
|
458
|
+
...options,
|
|
459
|
+
transform: this.transform.bind(this),
|
|
460
|
+
bindEvents: (requery) => {
|
|
461
|
+
const handleRequery = () => {
|
|
462
|
+
if (this.batchOperationInProgress) {
|
|
463
|
+
this.postBatchCallbacks.add(requery);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
requery();
|
|
467
|
+
};
|
|
468
|
+
this.addListener("persistence.received", handleRequery);
|
|
469
|
+
this.addListener("added", handleRequery);
|
|
470
|
+
this.addListener("changed", handleRequery);
|
|
471
|
+
this.addListener("removed", handleRequery);
|
|
472
|
+
this.emit("observer.created", selector, options);
|
|
473
|
+
return () => {
|
|
474
|
+
this.removeListener("persistence.received", handleRequery);
|
|
475
|
+
this.removeListener("added", handleRequery);
|
|
476
|
+
this.removeListener("changed", handleRequery);
|
|
477
|
+
this.removeListener("removed", handleRequery);
|
|
478
|
+
this.emit("observer.disposed", selector, options);
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
this.emit("find", selector, options, cursor);
|
|
483
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.find", callstack, selector, options, cursor));
|
|
484
|
+
return cursor;
|
|
73
485
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
486
|
+
/**
|
|
487
|
+
* Finds a single item in the collection based on a selector and optional options.
|
|
488
|
+
* ⚡️ this function is reactive!
|
|
489
|
+
* Returns the found item or undefined if no item matches.
|
|
490
|
+
* @template O - The options type for the find operation.
|
|
491
|
+
* @param selector - The criteria to select the item.
|
|
492
|
+
* @param [options] - Options for the find operation, such as projection.
|
|
493
|
+
* @returns The found item or `undefined`.
|
|
494
|
+
*/
|
|
495
|
+
findOne(selector, options) {
|
|
496
|
+
if (this.isDisposed)
|
|
497
|
+
throw new Error("Collection is disposed");
|
|
498
|
+
const cursor = this.find(selector, {
|
|
499
|
+
limit: 1,
|
|
500
|
+
...options
|
|
501
|
+
});
|
|
502
|
+
const returnValue = cursor.fetch()[0] || void 0;
|
|
503
|
+
this.emit("findOne", selector, options, returnValue);
|
|
504
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.findOne", callstack, selector, options, returnValue));
|
|
505
|
+
return returnValue;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Performs a batch operation, deferring index rebuilds and allowing multiple
|
|
509
|
+
* modifications to be made atomically. Executes any post-batch callbacks afterwards.
|
|
510
|
+
* @param callback - The batch operation to execute.
|
|
511
|
+
*/
|
|
512
|
+
batch(callback) {
|
|
513
|
+
this.batchOperationInProgress = true;
|
|
514
|
+
callback();
|
|
515
|
+
this.batchOperationInProgress = false;
|
|
516
|
+
this.rebuildAllIndices();
|
|
517
|
+
this.postBatchCallbacks.forEach((callback_) => callback_());
|
|
518
|
+
this.postBatchCallbacks.clear();
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Inserts a single item into the collection. Generates a unique ID if not provided.
|
|
522
|
+
* @param item - The item to insert.
|
|
523
|
+
* @returns The ID of the inserted item.
|
|
524
|
+
* @throws {Error} If the collection is disposed or the item has an invalid ID.
|
|
525
|
+
*/
|
|
526
|
+
insert(item) {
|
|
527
|
+
if (this.isDisposed)
|
|
528
|
+
throw new Error("Collection is disposed");
|
|
529
|
+
if (!item)
|
|
530
|
+
throw new Error("Invalid item");
|
|
531
|
+
const newItem = { id: randomId(), ...item };
|
|
532
|
+
if (this.idIndex.has(serializeValue(newItem.id)))
|
|
533
|
+
throw new Error("Item with same id already exists");
|
|
534
|
+
this.memory().push(newItem);
|
|
535
|
+
const itemIndex = this.memory().findIndex((document) => document === newItem);
|
|
536
|
+
this.idIndex.set(serializeValue(newItem.id), /* @__PURE__ */ new Set([itemIndex]));
|
|
537
|
+
this.rebuildIndices();
|
|
538
|
+
this.emit("added", newItem);
|
|
539
|
+
this.emit("insert", newItem);
|
|
540
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.insert", callstack, newItem));
|
|
541
|
+
return newItem.id;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Inserts multiple items into the collection. Generates unique IDs for items if not provided.
|
|
545
|
+
* @param items - The items to insert.
|
|
546
|
+
* @returns An array of IDs of the inserted items.
|
|
547
|
+
* @throws {Error} If the collection is disposed or the items are invalid.
|
|
548
|
+
*/
|
|
549
|
+
insertMany(items) {
|
|
550
|
+
if (this.isDisposed)
|
|
551
|
+
throw new Error("Collection is disposed");
|
|
552
|
+
if (!items)
|
|
553
|
+
throw new Error("Invalid items");
|
|
554
|
+
if (items.length === 0) {
|
|
555
|
+
return [];
|
|
83
556
|
}
|
|
557
|
+
const ids = [];
|
|
558
|
+
this.batch(() => {
|
|
559
|
+
items.forEach((item) => {
|
|
560
|
+
ids.push(this.insert(item));
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
return ids;
|
|
84
564
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
565
|
+
/**
|
|
566
|
+
* Updates a single item in the collection that matches the given selector.
|
|
567
|
+
* @param selector - The criteria to select the item to update.
|
|
568
|
+
* @param modifier - The modifications to apply to the item.
|
|
569
|
+
* @returns The number of items updated (0 or 1).
|
|
570
|
+
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
571
|
+
*/
|
|
572
|
+
updateOne(selector, modifier) {
|
|
573
|
+
if (this.isDisposed)
|
|
574
|
+
throw new Error("Collection is disposed");
|
|
575
|
+
if (!selector)
|
|
576
|
+
throw new Error("Invalid selector");
|
|
577
|
+
if (!modifier)
|
|
578
|
+
throw new Error("Invalid modifier");
|
|
579
|
+
const { item, index } = this.getItemAndIndex(selector);
|
|
580
|
+
if (item == null)
|
|
581
|
+
return 0;
|
|
582
|
+
const modifiedItem = modify(deepClone(item), modifier);
|
|
583
|
+
const existingItem = this.findOne({ id: modifiedItem.id }, { reactive: false });
|
|
584
|
+
if (!isEqual(existingItem, { ...existingItem, id: modifiedItem.id }))
|
|
585
|
+
throw new Error("Item with same id already exists");
|
|
586
|
+
this.memory().splice(index, 1, modifiedItem);
|
|
587
|
+
this.rebuildIndices();
|
|
588
|
+
this.emit("changed", modifiedItem, modifier);
|
|
589
|
+
this.emit("updateOne", selector, modifier);
|
|
590
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.updateOne", callstack, selector, modifier));
|
|
591
|
+
return 1;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Updates multiple items in the collection that match the given selector.
|
|
595
|
+
* @param selector - The criteria to select the items to update.
|
|
596
|
+
* @param modifier - The modifications to apply to the items.
|
|
597
|
+
* @returns The number of items updated.
|
|
598
|
+
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
599
|
+
*/
|
|
600
|
+
updateMany(selector, modifier) {
|
|
601
|
+
if (this.isDisposed)
|
|
602
|
+
throw new Error("Collection is disposed");
|
|
603
|
+
if (!selector)
|
|
604
|
+
throw new Error("Invalid selector");
|
|
605
|
+
if (!modifier)
|
|
606
|
+
throw new Error("Invalid modifier");
|
|
607
|
+
const items = this.getItems(selector);
|
|
608
|
+
const modifiedItems = [];
|
|
609
|
+
items.forEach((item) => {
|
|
610
|
+
const { index } = this.getItemAndIndex({ id: item.id });
|
|
611
|
+
if (index === -1)
|
|
612
|
+
throw new Error("Cannot resolve index for item");
|
|
613
|
+
const modifiedItem = modify(deepClone(item), modifier);
|
|
614
|
+
this.memory().splice(index, 1, modifiedItem);
|
|
615
|
+
modifiedItems.push(modifiedItem);
|
|
616
|
+
});
|
|
617
|
+
this.rebuildIndices();
|
|
618
|
+
modifiedItems.forEach((modifiedItem) => {
|
|
619
|
+
this.emit("changed", modifiedItem, modifier);
|
|
620
|
+
});
|
|
621
|
+
this.emit("updateMany", selector, modifier);
|
|
622
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.updateMany", callstack, selector, modifier));
|
|
623
|
+
return modifiedItems.length;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Removes a single item from the collection that matches the given selector.
|
|
627
|
+
* @param selector - The criteria to select the item to remove.
|
|
628
|
+
* @returns The number of items removed (0 or 1).
|
|
629
|
+
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
630
|
+
*/
|
|
631
|
+
removeOne(selector) {
|
|
632
|
+
if (this.isDisposed)
|
|
633
|
+
throw new Error("Collection is disposed");
|
|
634
|
+
if (!selector)
|
|
635
|
+
throw new Error("Invalid selector");
|
|
636
|
+
const { item, index } = this.getItemAndIndex(selector);
|
|
637
|
+
if (item != null) {
|
|
638
|
+
this.memory().splice(index, 1);
|
|
639
|
+
this.deleteFromIdIndex(item.id, index);
|
|
640
|
+
this.rebuildIndices();
|
|
641
|
+
this.emit("removed", item);
|
|
94
642
|
}
|
|
95
|
-
|
|
643
|
+
this.emit("removeOne", selector);
|
|
644
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.removeOne", callstack, selector));
|
|
645
|
+
return item == null ? 0 : 1;
|
|
96
646
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
|
|
647
|
+
/**
|
|
648
|
+
* Removes multiple items from the collection that match the given selector.
|
|
649
|
+
* @param selector - The criteria to select the items to remove.
|
|
650
|
+
* @returns The number of items removed.
|
|
651
|
+
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
652
|
+
*/
|
|
653
|
+
removeMany(selector) {
|
|
654
|
+
if (this.isDisposed)
|
|
655
|
+
throw new Error("Collection is disposed");
|
|
656
|
+
if (!selector)
|
|
657
|
+
throw new Error("Invalid selector");
|
|
658
|
+
const items = this.getItems(selector);
|
|
659
|
+
items.forEach((item) => {
|
|
660
|
+
const index = this.memory().findIndex((document) => document === item);
|
|
661
|
+
if (index === -1)
|
|
662
|
+
throw new Error("Cannot resolve index for item");
|
|
663
|
+
this.memory().splice(index, 1);
|
|
664
|
+
this.deleteFromIdIndex(item.id, index);
|
|
665
|
+
this.rebuildIndices();
|
|
666
|
+
});
|
|
667
|
+
items.forEach((item) => {
|
|
668
|
+
this.emit("removed", item);
|
|
669
|
+
});
|
|
670
|
+
this.emit("removeMany", selector);
|
|
671
|
+
this.executeInDebugMode((callstack) => this.emit("_debug.removeMany", callstack, selector));
|
|
672
|
+
return items.length;
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
__publicField(_Collection, "collections", []);
|
|
676
|
+
__publicField(_Collection, "debugMode", false);
|
|
677
|
+
__publicField(_Collection, "batchOperationInProgress", false);
|
|
678
|
+
__publicField(_Collection, "fieldTracking", false);
|
|
679
|
+
__publicField(_Collection, "onCreationCallbacks", []);
|
|
680
|
+
__publicField(_Collection, "onDisposeCallbacks", []);
|
|
681
|
+
/**
|
|
682
|
+
* Enables debug mode for all collections.
|
|
683
|
+
*/
|
|
684
|
+
__publicField(_Collection, "enableDebugMode", () => {
|
|
685
|
+
_Collection.debugMode = true;
|
|
686
|
+
_Collection.collections.forEach((collection) => {
|
|
687
|
+
collection.setDebugMode(true);
|
|
106
688
|
});
|
|
107
|
-
}
|
|
689
|
+
});
|
|
690
|
+
/**
|
|
691
|
+
* Enables field tracking for all collections.
|
|
692
|
+
* @param enable - A boolean indicating whether to enable field tracking.
|
|
693
|
+
*/
|
|
694
|
+
__publicField(_Collection, "setFieldTracking", (enable) => {
|
|
695
|
+
_Collection.fieldTracking = enable;
|
|
696
|
+
_Collection.collections.forEach((collection) => {
|
|
697
|
+
collection.setFieldTracking(enable);
|
|
698
|
+
});
|
|
699
|
+
});
|
|
700
|
+
let Collection = _Collection;
|
|
108
701
|
export {
|
|
109
|
-
|
|
702
|
+
default2 as createIndex,
|
|
703
|
+
Collection as default
|
|
110
704
|
};
|