@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/index.cjs2.js
CHANGED
|
@@ -3,783 +3,262 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
5
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const getIndexInfo = require("./index.cjs19.js");
|
|
16
|
-
const createIndex = require("./index.cjs13.js");
|
|
17
|
-
function hasPendingUpdates(pendingUpdates) {
|
|
18
|
-
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
6
|
+
const sortItems = require("./index.cjs15.js");
|
|
7
|
+
const project = require("./index.cjs16.js");
|
|
8
|
+
const Observer = require("./index.cjs17.js");
|
|
9
|
+
function isInReactiveScope(reactivity) {
|
|
10
|
+
if (!reactivity)
|
|
11
|
+
return false;
|
|
12
|
+
if (!reactivity.isInScope)
|
|
13
|
+
return true;
|
|
14
|
+
return reactivity.isInScope();
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
const items = [...currentItems];
|
|
22
|
-
added.forEach((item) => {
|
|
23
|
-
items.push(item);
|
|
24
|
-
});
|
|
25
|
-
modified.forEach((item) => {
|
|
26
|
-
const index = items.findIndex(({ id }) => id === item.id);
|
|
27
|
-
if (index === -1)
|
|
28
|
-
return;
|
|
29
|
-
items[index] = item;
|
|
30
|
-
});
|
|
31
|
-
removed.forEach((item) => {
|
|
32
|
-
const index = items.findIndex(({ id }) => id === item.id);
|
|
33
|
-
if (index === -1)
|
|
34
|
-
return;
|
|
35
|
-
items.splice(index, 1);
|
|
36
|
-
});
|
|
37
|
-
return items;
|
|
38
|
-
}
|
|
39
|
-
const _Collection = class _Collection extends EventEmitter {
|
|
16
|
+
class Cursor {
|
|
40
17
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @template T - The type of the items
|
|
44
|
-
* @template I - The type of the unique identifier for the items.
|
|
18
|
+
* Creates a new instance of the `Cursor` class.
|
|
19
|
+
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
20
|
+
* @template T - The type of the items in the collection.
|
|
45
21
|
* @template U - The transformed item type after applying transformations (default is T).
|
|
46
|
-
* @param
|
|
47
|
-
* @param options
|
|
48
|
-
* @param options.
|
|
49
|
-
* @param options.
|
|
50
|
-
* @param options.
|
|
51
|
-
* @param options.
|
|
52
|
-
* @param options.
|
|
53
|
-
* @param options.
|
|
54
|
-
* @param options.
|
|
22
|
+
* @param getItems - A function that retrieves the filtered list of items.
|
|
23
|
+
* @param options - Optional configuration for the cursor.
|
|
24
|
+
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
25
|
+
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
26
|
+
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
27
|
+
* @param options.sort - A sort specifier to determine the order of the items.
|
|
28
|
+
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
29
|
+
* @param options.limit - The maximum number of items to return in the result set.
|
|
30
|
+
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
31
|
+
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
55
32
|
*/
|
|
56
|
-
constructor(options) {
|
|
57
|
-
|
|
58
|
-
__publicField(this, "
|
|
33
|
+
constructor(getItems, options) {
|
|
34
|
+
__publicField(this, "observer");
|
|
35
|
+
__publicField(this, "getFilteredItems");
|
|
59
36
|
__publicField(this, "options");
|
|
60
|
-
__publicField(this, "
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
...options
|
|
77
|
-
};
|
|
78
|
-
this.fieldTracking = this.options.fieldTracking ?? _Collection.fieldTracking;
|
|
79
|
-
this.debugMode = this.options.enableDebugMode ?? _Collection.debugMode;
|
|
80
|
-
this.indexProviders = [
|
|
81
|
-
createIndex.createExternalIndex("id", this.idIndex),
|
|
82
|
-
...this.options.indices || []
|
|
83
|
-
];
|
|
84
|
-
this.rebuildIndices();
|
|
85
|
-
this.isPullingSignal = createSignal(this.options.reactivity, !!(options == null ? void 0 : options.persistence));
|
|
86
|
-
this.isPushingSignal = createSignal(this.options.reactivity, 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;
|
|
37
|
+
__publicField(this, "onCleanupCallbacks", []);
|
|
38
|
+
this.getFilteredItems = getItems;
|
|
39
|
+
this.options = options || {};
|
|
40
|
+
}
|
|
41
|
+
addGetters(item) {
|
|
42
|
+
if (!isInReactiveScope(this.options.reactive))
|
|
43
|
+
return item;
|
|
44
|
+
const depend = this.depend.bind(this);
|
|
45
|
+
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
46
|
+
Object.defineProperty(memo, key, {
|
|
47
|
+
get() {
|
|
48
|
+
depend({
|
|
49
|
+
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
50
|
+
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
51
|
+
return;
|
|
52
|
+
notify();
|
|
123
53
|
}
|
|
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
54
|
});
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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));
|
|
55
|
+
return value;
|
|
56
|
+
},
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true
|
|
222
59
|
});
|
|
223
|
-
|
|
224
|
-
|
|
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;
|
|
60
|
+
return memo;
|
|
61
|
+
}, {});
|
|
234
62
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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;
|
|
63
|
+
transform(rawItem) {
|
|
64
|
+
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
65
|
+
if (!this.options.transform)
|
|
66
|
+
return item;
|
|
67
|
+
return this.options.transform(item);
|
|
319
68
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
return
|
|
69
|
+
getItems() {
|
|
70
|
+
const items = this.getFilteredItems();
|
|
71
|
+
const { sort, skip, limit } = this.options;
|
|
72
|
+
const sorted = sort ? sortItems(items, sort) : items;
|
|
73
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
74
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
75
|
+
const idExcluded = this.options.fields && this.options.fields.id === 0;
|
|
76
|
+
return limited.map((item) => {
|
|
77
|
+
if (!this.options.fields)
|
|
78
|
+
return item;
|
|
79
|
+
return {
|
|
80
|
+
...idExcluded ? {} : { id: item.id },
|
|
81
|
+
...project(item, this.options.fields)
|
|
82
|
+
};
|
|
83
|
+
});
|
|
328
84
|
}
|
|
329
|
-
|
|
330
|
-
if (!this.
|
|
85
|
+
depend(changeEvents) {
|
|
86
|
+
if (!this.options.reactive)
|
|
331
87
|
return;
|
|
332
|
-
|
|
333
|
-
fn(callstack);
|
|
334
|
-
}
|
|
335
|
-
rebuildIndices() {
|
|
336
|
-
this.indicesOutdated = true;
|
|
337
|
-
if (this.batchOperationInProgress)
|
|
88
|
+
if (!isInReactiveScope(this.options.reactive))
|
|
338
89
|
return;
|
|
339
|
-
this.
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
matched: true,
|
|
353
|
-
positions: [...this.idIndex.get(serializeValue(selector.id)) || []],
|
|
354
|
-
optimizedSelector: {}
|
|
90
|
+
const signal = this.options.reactive.create();
|
|
91
|
+
signal.depend();
|
|
92
|
+
const notify = () => signal.notify();
|
|
93
|
+
function buildNotifier(event) {
|
|
94
|
+
const eventHandler = changeEvents[event];
|
|
95
|
+
return (...args) => {
|
|
96
|
+
if (eventHandler === true) {
|
|
97
|
+
notify();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (typeof eventHandler !== "function")
|
|
101
|
+
return;
|
|
102
|
+
eventHandler(notify)(...args);
|
|
355
103
|
};
|
|
356
104
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
105
|
+
const stop = this.observeRawChanges({
|
|
106
|
+
added: buildNotifier("added"),
|
|
107
|
+
addedBefore: buildNotifier("addedBefore"),
|
|
108
|
+
changed: buildNotifier("changed"),
|
|
109
|
+
changedField: buildNotifier("changedField"),
|
|
110
|
+
movedBefore: buildNotifier("movedBefore"),
|
|
111
|
+
removed: buildNotifier("removed")
|
|
112
|
+
}, true);
|
|
113
|
+
if (this.options.reactive.onDispose) {
|
|
114
|
+
this.options.reactive.onDispose(() => stop(), signal);
|
|
363
115
|
}
|
|
364
|
-
|
|
365
|
-
}
|
|
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 };
|
|
378
|
-
}
|
|
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]));
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
memory() {
|
|
390
|
-
return this.options.memory;
|
|
391
|
-
}
|
|
392
|
-
memoryArray() {
|
|
393
|
-
return this.memory().map((item) => item);
|
|
116
|
+
this.onCleanup(stop);
|
|
394
117
|
}
|
|
395
|
-
|
|
396
|
-
if (!this.
|
|
397
|
-
|
|
398
|
-
|
|
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.default(() => 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();
|
|
118
|
+
ensureObserver() {
|
|
119
|
+
if (!this.observer) {
|
|
120
|
+
const observer = new Observer(() => {
|
|
121
|
+
const requery = () => {
|
|
122
|
+
observer.runChecks(this.getItems());
|
|
467
123
|
};
|
|
468
|
-
this.
|
|
469
|
-
this.addListener("added", handleRequery);
|
|
470
|
-
this.addListener("changed", handleRequery);
|
|
471
|
-
this.addListener("removed", handleRequery);
|
|
472
|
-
this.emit("observer.created", selector, options);
|
|
124
|
+
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
473
125
|
return () => {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
this.removeListener("changed", handleRequery);
|
|
477
|
-
this.removeListener("removed", handleRequery);
|
|
478
|
-
this.emit("observer.disposed", selector, options);
|
|
126
|
+
if (cleanup)
|
|
127
|
+
cleanup();
|
|
479
128
|
};
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
return
|
|
129
|
+
});
|
|
130
|
+
this.onCleanup(() => observer.stop());
|
|
131
|
+
this.observer = observer;
|
|
132
|
+
}
|
|
133
|
+
return this.observer;
|
|
134
|
+
}
|
|
135
|
+
observeRawChanges(callbacks, skipInitial = false) {
|
|
136
|
+
const observer = this.ensureObserver();
|
|
137
|
+
observer.addCallbacks(callbacks, skipInitial);
|
|
138
|
+
observer.runChecks(this.getItems());
|
|
139
|
+
return () => {
|
|
140
|
+
observer.removeCallbacks(callbacks);
|
|
141
|
+
if (!observer.isEmpty())
|
|
142
|
+
return;
|
|
143
|
+
observer.stop();
|
|
144
|
+
this.observer = void 0;
|
|
145
|
+
};
|
|
485
146
|
}
|
|
486
147
|
/**
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
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`.
|
|
148
|
+
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
149
|
+
* and event listeners. This method should be called when the cursor is no longer needed
|
|
150
|
+
* to prevent memory leaks.
|
|
494
151
|
*/
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const cursor = this.find(selector, {
|
|
499
|
-
limit: 1,
|
|
500
|
-
...options
|
|
152
|
+
cleanup() {
|
|
153
|
+
this.onCleanupCallbacks.forEach((callback) => {
|
|
154
|
+
callback();
|
|
501
155
|
});
|
|
502
|
-
|
|
503
|
-
this.emit("findOne", selector, options, returnValue);
|
|
504
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.findOne", callstack, selector, options, returnValue));
|
|
505
|
-
return returnValue;
|
|
156
|
+
this.onCleanupCallbacks = [];
|
|
506
157
|
}
|
|
507
158
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* @param callback -
|
|
159
|
+
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
160
|
+
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
161
|
+
* @param callback - A function to be executed during cleanup.
|
|
511
162
|
*/
|
|
512
|
-
|
|
513
|
-
this.
|
|
514
|
-
callback();
|
|
515
|
-
this.batchOperationInProgress = false;
|
|
516
|
-
this.rebuildAllIndices();
|
|
517
|
-
this.postBatchCallbacks.forEach((callback_) => callback_());
|
|
518
|
-
this.postBatchCallbacks.clear();
|
|
163
|
+
onCleanup(callback) {
|
|
164
|
+
this.onCleanupCallbacks.push(callback);
|
|
519
165
|
}
|
|
520
166
|
/**
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
* @
|
|
167
|
+
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
168
|
+
* function to each transformed item.
|
|
169
|
+
* ⚡️ this function is reactive!
|
|
170
|
+
* @param callback - A function to execute for each item in the result set.
|
|
171
|
+
* @param callback.item - The transformed item.
|
|
525
172
|
*/
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
this.idIndex.set(serializeValue(newItem.id), /* @__PURE__ */ new Set([itemIndex]));
|
|
538
|
-
this.rebuildIndices();
|
|
539
|
-
this.emit("added", newItem);
|
|
540
|
-
this.emit("insert", newItem);
|
|
541
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.insert", callstack, newItem));
|
|
542
|
-
return newItem.id;
|
|
173
|
+
forEach(callback) {
|
|
174
|
+
const items = this.getItems();
|
|
175
|
+
this.depend({
|
|
176
|
+
addedBefore: true,
|
|
177
|
+
removed: true,
|
|
178
|
+
movedBefore: true,
|
|
179
|
+
...this.options.fieldTracking ? {} : { changed: true }
|
|
180
|
+
});
|
|
181
|
+
items.forEach((item) => {
|
|
182
|
+
callback(this.transform(item));
|
|
183
|
+
});
|
|
543
184
|
}
|
|
544
185
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
* @
|
|
186
|
+
* Creates a new array populated with the results of applying the provided callback
|
|
187
|
+
* function to each transformed item in the cursor's result set.
|
|
188
|
+
* ⚡️ this function is reactive!
|
|
189
|
+
* @template V - The type of the items in the resulting array.
|
|
190
|
+
* @param callback - A function to execute for each item in the result set.
|
|
191
|
+
* @param callback.item - The transformed item.
|
|
192
|
+
* @returns An array of results after applying the callback to each item.
|
|
549
193
|
*/
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
throw new Error("Invalid items");
|
|
555
|
-
if (items.length === 0) {
|
|
556
|
-
return [];
|
|
557
|
-
}
|
|
558
|
-
const ids = [];
|
|
559
|
-
this.batch(() => {
|
|
560
|
-
items.forEach((item) => {
|
|
561
|
-
ids.push(this.insert(item));
|
|
562
|
-
});
|
|
194
|
+
map(callback) {
|
|
195
|
+
const results = [];
|
|
196
|
+
this.forEach((item) => {
|
|
197
|
+
results.push(callback(item));
|
|
563
198
|
});
|
|
564
|
-
return
|
|
199
|
+
return results;
|
|
565
200
|
}
|
|
566
201
|
/**
|
|
567
|
-
*
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
* @
|
|
571
|
-
* @param [options.upsert] - If `true`, creates a new item if no item matches the selector.
|
|
572
|
-
* @returns The number of items updated (0 or 1).
|
|
573
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
202
|
+
* Fetches all transformed items from the cursor's result set as an array.
|
|
203
|
+
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
204
|
+
* ⚡️ this function is reactive!
|
|
205
|
+
* @returns An array of transformed items in the result set.
|
|
574
206
|
*/
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
throw new Error("Collection is disposed");
|
|
578
|
-
if (!selector)
|
|
579
|
-
throw new Error("Invalid selector");
|
|
580
|
-
if (!modifier)
|
|
581
|
-
throw new Error("Invalid modifier");
|
|
582
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
583
|
-
const { item, index } = this.getItemAndIndex(selector);
|
|
584
|
-
if (item == null) {
|
|
585
|
-
if (options == null ? void 0 : options.upsert) {
|
|
586
|
-
const newItem = modify({}, {
|
|
587
|
-
...restModifier,
|
|
588
|
-
$set: {
|
|
589
|
-
...$setOnInsert,
|
|
590
|
-
...restModifier.$set
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
if (newItem.id != null && this.getItemAndIndex({ id: newItem.id }).item != null) {
|
|
594
|
-
throw new Error("Item with same id already exists");
|
|
595
|
-
}
|
|
596
|
-
this.insert(newItem);
|
|
597
|
-
}
|
|
598
|
-
} else {
|
|
599
|
-
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
600
|
-
if (item.id !== modifiedItem.id && this.getItemAndIndex({ id: modifiedItem.id }).item != null) {
|
|
601
|
-
throw new Error("Item with same id already exists");
|
|
602
|
-
}
|
|
603
|
-
this.emit("validate", modifiedItem);
|
|
604
|
-
this.memory().splice(index, 1, modifiedItem);
|
|
605
|
-
this.rebuildIndices();
|
|
606
|
-
this.emit("changed", modifiedItem, restModifier);
|
|
607
|
-
}
|
|
608
|
-
this.emit("updateOne", selector, modifier);
|
|
609
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateOne", callstack, selector, modifier));
|
|
610
|
-
if (item == null && !(options == null ? void 0 : options.upsert))
|
|
611
|
-
return 0;
|
|
612
|
-
return 1;
|
|
207
|
+
fetch() {
|
|
208
|
+
return this.map((item) => item);
|
|
613
209
|
}
|
|
614
210
|
/**
|
|
615
|
-
*
|
|
616
|
-
*
|
|
617
|
-
*
|
|
618
|
-
* @
|
|
619
|
-
* @param [options.upsert] - If `true`, creates new items if no items match the selector.
|
|
620
|
-
* @returns The number of items updated.
|
|
621
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
211
|
+
* Counts the total number of items in the cursor's result set after applying
|
|
212
|
+
* filtering and other criteria.
|
|
213
|
+
* ⚡️ this function is reactive!
|
|
214
|
+
* @returns The total number of items in the result set.
|
|
622
215
|
*/
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
if (!modifier)
|
|
629
|
-
throw new Error("Invalid modifier");
|
|
630
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
631
|
-
const items = this.getItems(selector);
|
|
632
|
-
if (items.length === 0 && (options == null ? void 0 : options.upsert)) {
|
|
633
|
-
const newItem = modify({}, {
|
|
634
|
-
...restModifier,
|
|
635
|
-
$set: {
|
|
636
|
-
...$setOnInsert,
|
|
637
|
-
...restModifier.$set
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
if (newItem.id != null && this.getItemAndIndex({ id: newItem.id }).item != null) {
|
|
641
|
-
throw new Error("Item with same id already exists");
|
|
642
|
-
}
|
|
643
|
-
this.insert(newItem);
|
|
644
|
-
}
|
|
645
|
-
const changes = items.map((item) => {
|
|
646
|
-
const { index } = this.getItemAndIndex({ id: item.id });
|
|
647
|
-
if (index === -1)
|
|
648
|
-
throw new Error(`Cannot resolve index for item with id '${item.id}'`);
|
|
649
|
-
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
650
|
-
if (item.id !== modifiedItem.id && this.getItemAndIndex({ id: modifiedItem.id }).item != null) {
|
|
651
|
-
throw new Error(`Item with same id '${modifiedItem.id}' already exists`);
|
|
652
|
-
}
|
|
653
|
-
this.emit("validate", modifiedItem);
|
|
654
|
-
return {
|
|
655
|
-
item: modifiedItem,
|
|
656
|
-
index
|
|
657
|
-
};
|
|
658
|
-
});
|
|
659
|
-
changes.forEach(({ item, index }) => {
|
|
660
|
-
this.memory().splice(index, 1, item);
|
|
216
|
+
count() {
|
|
217
|
+
const items = this.getItems();
|
|
218
|
+
this.depend({
|
|
219
|
+
added: true,
|
|
220
|
+
removed: true
|
|
661
221
|
});
|
|
662
|
-
|
|
663
|
-
changes.forEach(({ item }) => {
|
|
664
|
-
this.emit("changed", item, restModifier);
|
|
665
|
-
});
|
|
666
|
-
this.emit("updateMany", selector, modifier);
|
|
667
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateMany", callstack, selector, modifier));
|
|
668
|
-
return changes.length === 0 && (options == null ? void 0 : options.upsert) ? 1 : changes.length;
|
|
222
|
+
return items.length;
|
|
669
223
|
}
|
|
670
224
|
/**
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
* @param
|
|
674
|
-
* @param
|
|
675
|
-
* @param
|
|
676
|
-
* @
|
|
677
|
-
* @
|
|
225
|
+
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
226
|
+
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
227
|
+
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
228
|
+
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
229
|
+
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
230
|
+
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
231
|
+
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
232
|
+
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
233
|
+
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
234
|
+
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
235
|
+
* @returns A function to stop observing changes.
|
|
678
236
|
*/
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
237
|
+
observeChanges(callbacks, skipInitial = false) {
|
|
238
|
+
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
239
|
+
if (!callback)
|
|
240
|
+
return memo;
|
|
241
|
+
return {
|
|
242
|
+
...memo,
|
|
243
|
+
[callbackName]: (item, before) => {
|
|
244
|
+
const transformedValue = this.transform(item);
|
|
245
|
+
const hasBeforeParameter = before !== void 0;
|
|
246
|
+
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
247
|
+
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
689
248
|
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
} else {
|
|
693
|
-
if (item.id !== replacement.id && this.getItemAndIndex({ id: replacement.id }).item != null) {
|
|
694
|
-
throw new Error("Item with same id already exists");
|
|
695
|
-
}
|
|
696
|
-
const modifiedItem = { id: item.id, ...replacement };
|
|
697
|
-
this.emit("validate", modifiedItem);
|
|
698
|
-
this.memory().splice(index, 1, modifiedItem);
|
|
699
|
-
this.rebuildIndices();
|
|
700
|
-
this.emit("changed", modifiedItem, replacement);
|
|
701
|
-
}
|
|
702
|
-
this.emit("replaceOne", selector, replacement);
|
|
703
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.replaceOne", callstack, selector, replacement));
|
|
704
|
-
if (item == null && !(options == null ? void 0 : options.upsert))
|
|
705
|
-
return 0;
|
|
706
|
-
return 1;
|
|
707
|
-
}
|
|
708
|
-
/**
|
|
709
|
-
* Removes a single item from the collection that matches the given selector.
|
|
710
|
-
* @param selector - The criteria to select the item to remove.
|
|
711
|
-
* @returns The number of items removed (0 or 1).
|
|
712
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
713
|
-
*/
|
|
714
|
-
removeOne(selector) {
|
|
715
|
-
if (this.isDisposed)
|
|
716
|
-
throw new Error("Collection is disposed");
|
|
717
|
-
if (!selector)
|
|
718
|
-
throw new Error("Invalid selector");
|
|
719
|
-
const { item, index } = this.getItemAndIndex(selector);
|
|
720
|
-
if (item != null) {
|
|
721
|
-
this.memory().splice(index, 1);
|
|
722
|
-
this.deleteFromIdIndex(item.id, index);
|
|
723
|
-
this.rebuildIndices();
|
|
724
|
-
this.emit("removed", item);
|
|
725
|
-
}
|
|
726
|
-
this.emit("removeOne", selector);
|
|
727
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeOne", callstack, selector));
|
|
728
|
-
return item == null ? 0 : 1;
|
|
249
|
+
};
|
|
250
|
+
}, {}), skipInitial);
|
|
729
251
|
}
|
|
730
252
|
/**
|
|
731
|
-
*
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
253
|
+
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
254
|
+
* from the collection. This is useful when the underlying data or query
|
|
255
|
+
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
735
256
|
*/
|
|
736
|
-
|
|
737
|
-
if (this.
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
throw new Error("Invalid selector");
|
|
741
|
-
const items = this.getItems(selector);
|
|
742
|
-
items.forEach((item) => {
|
|
743
|
-
const index = this.memory().findIndex((document) => document === item);
|
|
744
|
-
if (index === -1)
|
|
745
|
-
throw new Error("Cannot resolve index for item");
|
|
746
|
-
this.memory().splice(index, 1);
|
|
747
|
-
this.deleteFromIdIndex(item.id, index);
|
|
748
|
-
this.rebuildIndices();
|
|
749
|
-
});
|
|
750
|
-
items.forEach((item) => {
|
|
751
|
-
this.emit("removed", item);
|
|
752
|
-
});
|
|
753
|
-
this.emit("removeMany", selector);
|
|
754
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeMany", callstack, selector));
|
|
755
|
-
return items.length;
|
|
257
|
+
requery() {
|
|
258
|
+
if (!this.observer)
|
|
259
|
+
return;
|
|
260
|
+
this.observer.runChecks(this.getItems());
|
|
756
261
|
}
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
__publicField(_Collection, "batchOperationInProgress", false);
|
|
761
|
-
__publicField(_Collection, "fieldTracking", false);
|
|
762
|
-
__publicField(_Collection, "onCreationCallbacks", []);
|
|
763
|
-
__publicField(_Collection, "onDisposeCallbacks", []);
|
|
764
|
-
/**
|
|
765
|
-
* Enables debug mode for all collections.
|
|
766
|
-
*/
|
|
767
|
-
__publicField(_Collection, "enableDebugMode", () => {
|
|
768
|
-
_Collection.debugMode = true;
|
|
769
|
-
_Collection.collections.forEach((collection) => {
|
|
770
|
-
collection.setDebugMode(true);
|
|
771
|
-
});
|
|
772
|
-
});
|
|
773
|
-
/**
|
|
774
|
-
* Enables field tracking for all collections.
|
|
775
|
-
* @param enable - A boolean indicating whether to enable field tracking.
|
|
776
|
-
*/
|
|
777
|
-
__publicField(_Collection, "setFieldTracking", (enable) => {
|
|
778
|
-
_Collection.fieldTracking = enable;
|
|
779
|
-
_Collection.collections.forEach((collection) => {
|
|
780
|
-
collection.setFieldTracking(enable);
|
|
781
|
-
});
|
|
782
|
-
});
|
|
783
|
-
let Collection = _Collection;
|
|
784
|
-
exports.createIndex = createIndex.default;
|
|
785
|
-
exports.default = Collection;
|
|
262
|
+
}
|
|
263
|
+
exports.default = Cursor;
|
|
264
|
+
exports.isInReactiveScope = isInReactiveScope;
|