@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/index.cjs2.js
CHANGED
|
@@ -1,631 +1,120 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
const index = require("./index.cjs3.js");
|
|
25
|
+
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
|
|
26
|
+
const _interopNamespaceDefaultOnly = (e) => Object.freeze(Object.defineProperty({ __proto__: null, default: e }, Symbol.toStringTag, { value: "Module" }));
|
|
27
|
+
/* istanbul ignore file -- @preserve */
|
|
28
|
+
const devtoolsInProductionWarning = `
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
[@signaldb/core] !! WARNING !!
|
|
32
|
+
You are running @signaldb/devtools in production mode.
|
|
33
|
+
This can have a negative impact on performance and may expose sensitive information.
|
|
34
|
+
Please move @signaldb/devtools to development dependencies to disable it in production.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
`;
|
|
38
|
+
function isNodeErrorWithCode(error) {
|
|
39
|
+
return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string";
|
|
16
40
|
}
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
added.forEach((item) => {
|
|
20
|
-
items.push(item);
|
|
21
|
-
});
|
|
22
|
-
modified.forEach((item) => {
|
|
23
|
-
const index = items.findIndex(({ id }) => id === item.id);
|
|
24
|
-
if (index === -1)
|
|
25
|
-
return;
|
|
26
|
-
items[index] = item;
|
|
27
|
-
});
|
|
28
|
-
removed.forEach((item) => {
|
|
29
|
-
const index = items.findIndex(({ id }) => id === item.id);
|
|
30
|
-
if (index === -1)
|
|
31
|
-
return;
|
|
32
|
-
items.splice(index, 1);
|
|
33
|
-
});
|
|
34
|
-
return items;
|
|
41
|
+
function isCommonJS() {
|
|
42
|
+
return typeof module !== "undefined" && !!module.exports;
|
|
35
43
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
Collection.collections.reduce((memo, collection) => () => collection.batch(() => memo()), callback)();
|
|
47
|
-
Collection.batchOperationInProgress = false;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Initializes a new instance of the `Collection` class with optional configuration.
|
|
51
|
-
* Sets up memory, persistence, reactivity, and indices as specified in the options.
|
|
52
|
-
* @template T - The type of the items stored in the collection.
|
|
53
|
-
* @template I - The type of the unique identifier for the items.
|
|
54
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
55
|
-
* @param options - Optional configuration for the collection.
|
|
56
|
-
* @param options.memory - The in-memory adapter for storing items.
|
|
57
|
-
* @param options.reactivity - The reactivity adapter for observing changes in the collection.
|
|
58
|
-
* @param options.transform - A transformation function to apply to items when retrieving them.
|
|
59
|
-
* @param options.persistence - The persistence adapter for saving and loading items.
|
|
60
|
-
* @param options.indices - An array of index providers for optimized querying.
|
|
61
|
-
* @param options.enableDebugMode - A boolean to enable or disable debug mode.
|
|
62
|
-
* @param options.fieldTracking - A boolean to enable or disable field tracking by default.
|
|
63
|
-
*/
|
|
64
|
-
constructor(options) {
|
|
65
|
-
var _a, _b, _c, _d, _e;
|
|
66
|
-
super();
|
|
67
|
-
this.persistenceAdapter = null;
|
|
68
|
-
this.indexProviders = [];
|
|
69
|
-
this.indicesOutdated = false;
|
|
70
|
-
this.idIndex = /* @__PURE__ */ new Map();
|
|
71
|
-
this.batchOperationInProgress = false;
|
|
72
|
-
this.isDisposed = false;
|
|
73
|
-
this.postBatchCallbacks = /* @__PURE__ */ new Set();
|
|
74
|
-
this.fieldTracking = false;
|
|
75
|
-
Collection.collections.push(this);
|
|
76
|
-
this.options = Object.assign({ memory: [] }, options);
|
|
77
|
-
this.fieldTracking = (_a = this.options.fieldTracking) !== null && _a !== void 0 ? _a : Collection.fieldTracking;
|
|
78
|
-
this.debugMode = (_b = this.options.enableDebugMode) !== null && _b !== void 0 ? _b : Collection.debugMode;
|
|
79
|
-
this.indexProviders = [
|
|
80
|
-
createIndex.createExternalIndex("id", this.idIndex),
|
|
81
|
-
...this.options.indices || []
|
|
82
|
-
];
|
|
83
|
-
this.rebuildIndices();
|
|
84
|
-
this.isPullingSignal = createSignal((_c = this.options.reactivity) === null || _c === void 0 ? void 0 : _c.create(), !!(options === null || options === void 0 ? void 0 : options.persistence));
|
|
85
|
-
this.isPushingSignal = createSignal((_d = this.options.reactivity) === null || _d === void 0 ? void 0 : _d.create(), 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 = (_e = this.options.persistence) !== null && _e !== void 0 ? _e : 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 !== null && data !== void 0 ? 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((doc) => doc.id === item.id);
|
|
119
|
-
if (index >= 0) {
|
|
120
|
-
this.memory().splice(index, 1, item);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
this.memory().push(item);
|
|
124
|
-
const itemIndex = this.memory().findIndex((doc) => doc === item);
|
|
125
|
-
this.idIndex.set(serializeValue(item.id), /* @__PURE__ */ new Set([itemIndex]));
|
|
126
|
-
});
|
|
127
|
-
changes.modified.forEach((item) => {
|
|
128
|
-
const index = this.memory().findIndex((doc) => doc.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((doc) => doc.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 = Object.assign({}, 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
|
-
});
|
|
215
|
-
}
|
|
216
|
-
await loadPersistentData();
|
|
217
|
-
isInitialized = true;
|
|
218
|
-
setTimeout(() => this.emit("persistence.init"), 0);
|
|
219
|
-
}).catch((error) => {
|
|
220
|
-
this.emit("persistence.error", error instanceof Error ? error : new Error(error));
|
|
221
|
-
});
|
|
44
|
+
async function checkDevtoolsAvailability() {
|
|
45
|
+
if (typeof window === "undefined") {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (isCommonJS()) {
|
|
49
|
+
try {
|
|
50
|
+
require.resolve("@signaldb/devtools");
|
|
51
|
+
return true;
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
222
54
|
}
|
|
223
55
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
isPulling() {
|
|
231
|
-
var _a;
|
|
232
|
-
return (_a = this.isPullingSignal.get()) !== null && _a !== void 0 ? _a : false;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Checks whether the collection is currently performing a push operation
|
|
236
|
-
* ⚡️ this function is reactive!
|
|
237
|
-
* (saving data to the persistence adapter).
|
|
238
|
-
* @returns A boolean indicating if the collection is in the process of pushing data.
|
|
239
|
-
*/
|
|
240
|
-
isPushing() {
|
|
241
|
-
var _a;
|
|
242
|
-
return (_a = this.isPushingSignal.get()) !== null && _a !== void 0 ? _a : false;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Checks whether the collection is currently performing either a pull or push operation,
|
|
246
|
-
* ⚡️ this function is reactive!
|
|
247
|
-
* indicating that it is loading or saving data.
|
|
248
|
-
* @returns A boolean indicating if the collection is in the process of loading or saving data.
|
|
249
|
-
*/
|
|
250
|
-
isLoading() {
|
|
251
|
-
const isPulling = this.isPulling();
|
|
252
|
-
const isPushing = this.isPushing();
|
|
253
|
-
return isPulling || isPushing;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Retrieves the current debug mode status of the collection.
|
|
257
|
-
* @returns A boolean indicating whether debug mode is enabled for the collection.
|
|
258
|
-
*/
|
|
259
|
-
getDebugMode() {
|
|
260
|
-
return this.debugMode;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Enables or disables debug mode for the collection.
|
|
264
|
-
* When debug mode is enabled, additional debugging information and events are emitted.
|
|
265
|
-
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) debug mode.
|
|
266
|
-
*/
|
|
267
|
-
setDebugMode(enable) {
|
|
268
|
-
this.debugMode = enable;
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Enables or disables field tracking for the collection.
|
|
272
|
-
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking.
|
|
273
|
-
*/
|
|
274
|
-
setFieldTracking(enable) {
|
|
275
|
-
this.fieldTracking = enable;
|
|
276
|
-
}
|
|
277
|
-
profile(fn, measureFunction) {
|
|
278
|
-
if (!this.debugMode)
|
|
279
|
-
return fn();
|
|
280
|
-
const startTime = performance.now();
|
|
281
|
-
const result = fn();
|
|
282
|
-
const endTime = performance.now();
|
|
283
|
-
measureFunction(endTime - startTime);
|
|
284
|
-
return result;
|
|
285
|
-
}
|
|
286
|
-
executeInDebugMode(fn) {
|
|
287
|
-
if (!this.debugMode)
|
|
288
|
-
return;
|
|
289
|
-
const callstack = new Error().stack || "";
|
|
290
|
-
fn(callstack);
|
|
291
|
-
}
|
|
292
|
-
rebuildIndices() {
|
|
293
|
-
this.indicesOutdated = true;
|
|
294
|
-
if (this.batchOperationInProgress)
|
|
295
|
-
return;
|
|
296
|
-
this.rebuildAllIndices();
|
|
297
|
-
}
|
|
298
|
-
rebuildAllIndices() {
|
|
299
|
-
this.idIndex.clear();
|
|
300
|
-
this.memory().map((item, index) => {
|
|
301
|
-
this.idIndex.set(serializeValue(item.id), /* @__PURE__ */ new Set([index]));
|
|
302
|
-
});
|
|
303
|
-
this.indexProviders.forEach((index) => index.rebuild(this.memoryArray()));
|
|
304
|
-
this.indicesOutdated = false;
|
|
305
|
-
}
|
|
306
|
-
getIndexInfo(selector) {
|
|
307
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
308
|
-
return {
|
|
309
|
-
matched: true,
|
|
310
|
-
positions: Array.from(this.idIndex.get(serializeValue(selector.id)) || []),
|
|
311
|
-
optimizedSelector: {}
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
if (selector == null || this.indicesOutdated) {
|
|
315
|
-
return {
|
|
316
|
-
matched: false,
|
|
317
|
-
positions: [],
|
|
318
|
-
optimizedSelector: {}
|
|
319
|
-
};
|
|
56
|
+
try {
|
|
57
|
+
await import("@signaldb/devtools");
|
|
58
|
+
return true;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
|
|
61
|
+
throw error;
|
|
320
62
|
}
|
|
321
|
-
return getIndexInfo.default(this.indexProviders, selector);
|
|
322
63
|
}
|
|
323
|
-
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (index === -1)
|
|
332
|
-
throw new Error("Cannot resolve index for item");
|
|
333
|
-
return { item, index };
|
|
64
|
+
try {
|
|
65
|
+
const modulePackage = await Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./index.cjs15.js")));
|
|
66
|
+
const createRequireFunction = modulePackage.default.createRequire;
|
|
67
|
+
const cjsRequire = createRequireFunction(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.cjs2.js", document.baseURI).href);
|
|
68
|
+
cjsRequire.resolve("@signaldb/devtools");
|
|
69
|
+
return true;
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
334
72
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
73
|
+
}
|
|
74
|
+
async function checkAndImportDevtools() {
|
|
75
|
+
const available = await checkDevtoolsAvailability();
|
|
76
|
+
if (!available)
|
|
77
|
+
return false;
|
|
78
|
+
if (isCommonJS()) {
|
|
79
|
+
try {
|
|
80
|
+
require("@signaldb/devtools");
|
|
81
|
+
return true;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
|
|
84
|
+
return false;
|
|
342
85
|
}
|
|
343
|
-
|
|
344
|
-
}
|
|
345
|
-
memory() {
|
|
346
|
-
return this.options.memory;
|
|
347
|
-
}
|
|
348
|
-
memoryArray() {
|
|
349
|
-
return this.memory().map((item) => item);
|
|
350
|
-
}
|
|
351
|
-
transform(item) {
|
|
352
|
-
if (!this.options.transform)
|
|
353
|
-
return item;
|
|
354
|
-
return this.options.transform(item);
|
|
355
|
-
}
|
|
356
|
-
getItems(selector) {
|
|
357
|
-
return this.profile(() => {
|
|
358
|
-
const indexInfo = this.getIndexInfo(selector);
|
|
359
|
-
const matchItems = (item) => {
|
|
360
|
-
if (indexInfo.optimizedSelector == null)
|
|
361
|
-
return true;
|
|
362
|
-
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
363
|
-
return true;
|
|
364
|
-
const matches = match(item, indexInfo.optimizedSelector);
|
|
365
|
-
return matches;
|
|
366
|
-
};
|
|
367
|
-
if (!indexInfo.matched)
|
|
368
|
-
return this.memory().filter(matchItems);
|
|
369
|
-
const memory = this.memoryArray();
|
|
370
|
-
const items = indexInfo.positions.map((index) => memory[index]);
|
|
371
|
-
this.emit("getItems", selector);
|
|
372
|
-
return items.filter(matchItems);
|
|
373
|
-
}, (measuredTime) => this.executeInDebugMode((callstack) => this.emit("_debug.getItems", callstack, selector, measuredTime)));
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Disposes the collection, unregisters persistence adapters, clears memory, and
|
|
377
|
-
* cleans up all resources used by the collection.
|
|
378
|
-
* @returns A promise that resolves when the collection is disposed.
|
|
379
|
-
*/
|
|
380
|
-
async dispose() {
|
|
381
|
-
var _a;
|
|
382
|
-
if ((_a = this.persistenceAdapter) === null || _a === void 0 ? void 0 : _a.unregister)
|
|
383
|
-
await this.persistenceAdapter.unregister();
|
|
384
|
-
this.persistenceAdapter = null;
|
|
385
|
-
this.memory().map(() => this.memory().pop());
|
|
386
|
-
this.idIndex.clear();
|
|
387
|
-
this.indexProviders = [];
|
|
388
|
-
this.isDisposed = true;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Finds multiple items in the collection based on a selector and optional options.
|
|
392
|
-
* Returns a cursor for reactive data queries.
|
|
393
|
-
* @template O - The options type for the find operation.
|
|
394
|
-
* @param [selector] - The criteria to select items.
|
|
395
|
-
* @param [options] - Options for the find operation, such as limit and sort.
|
|
396
|
-
* @returns A cursor to fetch and observe the matching items.
|
|
397
|
-
*/
|
|
398
|
-
find(selector, options) {
|
|
399
|
-
if (this.isDisposed)
|
|
400
|
-
throw new Error("Collection is disposed");
|
|
401
|
-
if (selector !== void 0 && (!selector || typeof selector !== "object"))
|
|
402
|
-
throw new Error("Invalid selector");
|
|
403
|
-
const cursor = new Cursor.default(() => this.getItems(selector), Object.assign(Object.assign({ reactive: this.options.reactivity, fieldTracking: this.fieldTracking }, options), { transform: this.transform.bind(this), bindEvents: (requery) => {
|
|
404
|
-
const handleRequery = () => {
|
|
405
|
-
if (this.batchOperationInProgress) {
|
|
406
|
-
this.postBatchCallbacks.add(requery);
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
requery();
|
|
410
|
-
};
|
|
411
|
-
this.addListener("persistence.received", handleRequery);
|
|
412
|
-
this.addListener("added", handleRequery);
|
|
413
|
-
this.addListener("changed", handleRequery);
|
|
414
|
-
this.addListener("removed", handleRequery);
|
|
415
|
-
this.emit("observer.created", selector, options);
|
|
416
|
-
return () => {
|
|
417
|
-
this.removeListener("persistence.received", handleRequery);
|
|
418
|
-
this.removeListener("added", handleRequery);
|
|
419
|
-
this.removeListener("changed", handleRequery);
|
|
420
|
-
this.removeListener("removed", handleRequery);
|
|
421
|
-
this.emit("observer.disposed", selector, options);
|
|
422
|
-
};
|
|
423
|
-
} }));
|
|
424
|
-
this.emit("find", selector, options, cursor);
|
|
425
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.find", callstack, selector, options, cursor));
|
|
426
|
-
return cursor;
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Finds a single item in the collection based on a selector and optional options.
|
|
430
|
-
* ⚡️ this function is reactive!
|
|
431
|
-
* Returns the found item or undefined if no item matches.
|
|
432
|
-
* @template O - The options type for the find operation.
|
|
433
|
-
* @param selector - The criteria to select the item.
|
|
434
|
-
* @param [options] - Options for the find operation, such as projection.
|
|
435
|
-
* @returns The found item or `undefined`.
|
|
436
|
-
*/
|
|
437
|
-
findOne(selector, options) {
|
|
438
|
-
if (this.isDisposed)
|
|
439
|
-
throw new Error("Collection is disposed");
|
|
440
|
-
const cursor = this.find(selector, Object.assign({ limit: 1 }, options));
|
|
441
|
-
const returnValue = cursor.fetch()[0] || void 0;
|
|
442
|
-
this.emit("findOne", selector, options, returnValue);
|
|
443
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.findOne", callstack, selector, options, returnValue));
|
|
444
|
-
return returnValue;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Performs a batch operation, deferring index rebuilds and allowing multiple
|
|
448
|
-
* modifications to be made atomically. Executes any post-batch callbacks afterwards.
|
|
449
|
-
* @param callback - The batch operation to execute.
|
|
450
|
-
*/
|
|
451
|
-
batch(callback) {
|
|
452
|
-
this.batchOperationInProgress = true;
|
|
453
|
-
callback();
|
|
454
|
-
this.batchOperationInProgress = false;
|
|
455
|
-
this.rebuildAllIndices();
|
|
456
|
-
this.postBatchCallbacks.forEach((cb) => cb());
|
|
457
|
-
this.postBatchCallbacks.clear();
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Inserts a single item into the collection. Generates a unique ID if not provided.
|
|
461
|
-
* @param item - The item to insert.
|
|
462
|
-
* @returns The ID of the inserted item.
|
|
463
|
-
* @throws {Error} If the collection is disposed or the item has an invalid ID.
|
|
464
|
-
*/
|
|
465
|
-
insert(item) {
|
|
466
|
-
if (this.isDisposed)
|
|
467
|
-
throw new Error("Collection is disposed");
|
|
468
|
-
if (!item)
|
|
469
|
-
throw new Error("Invalid item");
|
|
470
|
-
const newItem = Object.assign({ id: randomId() }, item);
|
|
471
|
-
if (this.idIndex.has(serializeValue(newItem.id)))
|
|
472
|
-
throw new Error("Item with same id already exists");
|
|
473
|
-
this.memory().push(newItem);
|
|
474
|
-
const itemIndex = this.memory().findIndex((doc) => doc === newItem);
|
|
475
|
-
this.idIndex.set(serializeValue(newItem.id), /* @__PURE__ */ new Set([itemIndex]));
|
|
476
|
-
this.rebuildIndices();
|
|
477
|
-
this.emit("added", newItem);
|
|
478
|
-
this.emit("insert", newItem);
|
|
479
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.insert", callstack, newItem));
|
|
480
|
-
return newItem.id;
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* Inserts multiple items into the collection. Generates unique IDs for items if not provided.
|
|
484
|
-
* @param items - The items to insert.
|
|
485
|
-
* @returns An array of IDs of the inserted items.
|
|
486
|
-
* @throws {Error} If the collection is disposed or the items are invalid.
|
|
487
|
-
*/
|
|
488
|
-
insertMany(items) {
|
|
489
|
-
if (this.isDisposed)
|
|
490
|
-
throw new Error("Collection is disposed");
|
|
491
|
-
if (!items)
|
|
492
|
-
throw new Error("Invalid items");
|
|
493
|
-
if (items.length === 0) {
|
|
494
|
-
return [];
|
|
86
|
+
throw error;
|
|
495
87
|
}
|
|
496
|
-
const ids = [];
|
|
497
|
-
this.batch(() => {
|
|
498
|
-
items.forEach((item) => {
|
|
499
|
-
ids.push(this.insert(item));
|
|
500
|
-
});
|
|
501
|
-
});
|
|
502
|
-
return ids;
|
|
503
|
-
}
|
|
504
|
-
/**
|
|
505
|
-
* Updates a single item in the collection that matches the given selector.
|
|
506
|
-
* @param selector - The criteria to select the item to update.
|
|
507
|
-
* @param modifier - The modifications to apply to the item.
|
|
508
|
-
* @returns The number of items updated (0 or 1).
|
|
509
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
510
|
-
*/
|
|
511
|
-
updateOne(selector, modifier) {
|
|
512
|
-
if (this.isDisposed)
|
|
513
|
-
throw new Error("Collection is disposed");
|
|
514
|
-
if (!selector)
|
|
515
|
-
throw new Error("Invalid selector");
|
|
516
|
-
if (!modifier)
|
|
517
|
-
throw new Error("Invalid modifier");
|
|
518
|
-
const { item, index } = this.getItemAndIndex(selector);
|
|
519
|
-
if (item == null)
|
|
520
|
-
return 0;
|
|
521
|
-
const modifiedItem = modify(deepClone.default(item), modifier);
|
|
522
|
-
const existingItem = this.findOne({ id: modifiedItem.id }, { reactive: false });
|
|
523
|
-
if (!isEqual(existingItem, Object.assign(Object.assign({}, existingItem), { id: modifiedItem.id })))
|
|
524
|
-
throw new Error("Item with same id already exists");
|
|
525
|
-
this.memory().splice(index, 1, modifiedItem);
|
|
526
|
-
this.rebuildIndices();
|
|
527
|
-
this.emit("changed", modifiedItem, modifier);
|
|
528
|
-
this.emit("updateOne", selector, modifier);
|
|
529
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateOne", callstack, selector, modifier));
|
|
530
|
-
return 1;
|
|
531
88
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
*/
|
|
539
|
-
updateMany(selector, modifier) {
|
|
540
|
-
if (this.isDisposed)
|
|
541
|
-
throw new Error("Collection is disposed");
|
|
542
|
-
if (!selector)
|
|
543
|
-
throw new Error("Invalid selector");
|
|
544
|
-
if (!modifier)
|
|
545
|
-
throw new Error("Invalid modifier");
|
|
546
|
-
const items = this.getItems(selector);
|
|
547
|
-
const modifiedItems = [];
|
|
548
|
-
items.forEach((item) => {
|
|
549
|
-
const { index } = this.getItemAndIndex({ id: item.id });
|
|
550
|
-
if (index === -1)
|
|
551
|
-
throw new Error("Cannot resolve index for item");
|
|
552
|
-
const modifiedItem = modify(deepClone.default(item), modifier);
|
|
553
|
-
this.memory().splice(index, 1, modifiedItem);
|
|
554
|
-
modifiedItems.push(modifiedItem);
|
|
555
|
-
});
|
|
556
|
-
this.rebuildIndices();
|
|
557
|
-
modifiedItems.forEach((modifiedItem) => {
|
|
558
|
-
this.emit("changed", modifiedItem, modifier);
|
|
559
|
-
});
|
|
560
|
-
this.emit("updateMany", selector, modifier);
|
|
561
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateMany", callstack, selector, modifier));
|
|
562
|
-
return modifiedItems.length;
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Removes a single item from the collection that matches the given selector.
|
|
566
|
-
* @param selector - The criteria to select the item to remove.
|
|
567
|
-
* @returns The number of items removed (0 or 1).
|
|
568
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
569
|
-
*/
|
|
570
|
-
removeOne(selector) {
|
|
571
|
-
if (this.isDisposed)
|
|
572
|
-
throw new Error("Collection is disposed");
|
|
573
|
-
if (!selector)
|
|
574
|
-
throw new Error("Invalid selector");
|
|
575
|
-
const { item, index } = this.getItemAndIndex(selector);
|
|
576
|
-
if (item != null) {
|
|
577
|
-
this.memory().splice(index, 1);
|
|
578
|
-
this.deleteFromIdIndex(item.id, index);
|
|
579
|
-
this.rebuildIndices();
|
|
580
|
-
this.emit("removed", item);
|
|
89
|
+
try {
|
|
90
|
+
await import("@signaldb/devtools");
|
|
91
|
+
return true;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (!isNodeErrorWithCode(error) || error.code !== "MODULE_NOT_FOUND") {
|
|
94
|
+
throw error;
|
|
581
95
|
}
|
|
582
|
-
this.emit("removeOne", selector);
|
|
583
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeOne", callstack, selector));
|
|
584
|
-
return item == null ? 0 : 1;
|
|
585
96
|
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
if (
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
const items = this.getItems(selector);
|
|
598
|
-
items.forEach((item) => {
|
|
599
|
-
const index = this.memory().findIndex((doc) => doc === item);
|
|
600
|
-
if (index === -1)
|
|
601
|
-
throw new Error("Cannot resolve index for item");
|
|
602
|
-
this.memory().splice(index, 1);
|
|
603
|
-
this.deleteFromIdIndex(item.id, index);
|
|
604
|
-
this.rebuildIndices();
|
|
605
|
-
});
|
|
606
|
-
items.forEach((item) => {
|
|
607
|
-
this.emit("removed", item);
|
|
608
|
-
});
|
|
609
|
-
this.emit("removeMany", selector);
|
|
610
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeMany", callstack, selector));
|
|
611
|
-
return items.length;
|
|
97
|
+
try {
|
|
98
|
+
const modulePackage = await Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./index.cjs15.js")));
|
|
99
|
+
const createRequireFunction = modulePackage.default.createRequire;
|
|
100
|
+
const cjsRequire = createRequireFunction(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.cjs2.js", document.baseURI).href);
|
|
101
|
+
cjsRequire("@signaldb/devtools");
|
|
102
|
+
return true;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (isNodeErrorWithCode(error) && error.code === "MODULE_NOT_FOUND") {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
throw error;
|
|
612
108
|
}
|
|
613
109
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
});
|
|
623
|
-
};
|
|
624
|
-
Collection.setFieldTracking = (enable) => {
|
|
625
|
-
Collection.fieldTracking = enable;
|
|
626
|
-
Collection.collections.forEach((collection) => {
|
|
627
|
-
collection.setFieldTracking(enable);
|
|
110
|
+
function loadDeveloperTools() {
|
|
111
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
112
|
+
void checkAndImportDevtools().then((devtoolsLoaded) => {
|
|
113
|
+
if (!devtoolsLoaded)
|
|
114
|
+
return;
|
|
115
|
+
index.default.enableDebugMode();
|
|
116
|
+
if (isProduction)
|
|
117
|
+
console.warn(devtoolsInProductionWarning);
|
|
628
118
|
});
|
|
629
|
-
}
|
|
630
|
-
exports
|
|
631
|
-
exports.default = Collection;
|
|
119
|
+
}
|
|
120
|
+
module.exports = loadDeveloperTools;
|