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