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