@signaldb/core 1.0.0 → 1.1.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.
Files changed (76) hide show
  1. package/README.md +2 -2
  2. package/dist/.vite/manifest.json +78 -64
  3. package/dist/Collection/index.d.ts +24 -16
  4. package/dist/Collection/types.d.ts +1 -1
  5. package/dist/devtools.d.ts +4 -0
  6. package/dist/index.cjs.js +16 -11
  7. package/dist/index.cjs10.js +27 -6
  8. package/dist/index.cjs11.js +6 -3
  9. package/dist/index.cjs12.js +3 -35
  10. package/dist/index.cjs13.js +141 -16
  11. package/dist/index.cjs14.js +38 -5
  12. package/dist/index.cjs15.js +2 -42
  13. package/dist/index.cjs16.js +5 -11
  14. package/dist/index.cjs17.js +40 -17
  15. package/dist/index.cjs18.js +11 -245
  16. package/dist/index.cjs19.js +16 -67
  17. package/dist/index.cjs2.js +104 -615
  18. package/dist/index.cjs20.js +251 -67
  19. package/dist/index.cjs21.js +68 -8
  20. package/dist/index.cjs22.js +83 -22
  21. package/dist/index.cjs23.js +8 -135
  22. package/dist/index.cjs24.js +25 -16
  23. package/dist/index.cjs25.js +144 -5
  24. package/dist/index.cjs26.js +5 -17
  25. package/dist/index.cjs27.js +15 -21
  26. package/dist/index.cjs28.js +21 -39
  27. package/dist/index.cjs29.js +40 -27
  28. package/dist/index.cjs3.js +645 -136
  29. package/dist/index.cjs30.js +27 -7
  30. package/dist/index.cjs31.js +9 -0
  31. package/dist/index.cjs4.js +166 -63
  32. package/dist/index.cjs5.js +66 -3
  33. package/dist/index.cjs6.js +2 -2
  34. package/dist/index.cjs7.js +2 -2
  35. package/dist/index.cjs8.js +2 -2
  36. package/dist/index.cjs9.js +3 -28
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.mjs +17 -12
  39. package/dist/index10.mjs +27 -6
  40. package/dist/index11.mjs +6 -3
  41. package/dist/index12.mjs +3 -34
  42. package/dist/index13.mjs +141 -16
  43. package/dist/index14.mjs +37 -5
  44. package/dist/index15.mjs +2 -41
  45. package/dist/index16.mjs +5 -11
  46. package/dist/index17.mjs +39 -17
  47. package/dist/index18.mjs +11 -244
  48. package/dist/index19.mjs +16 -66
  49. package/dist/index2.mjs +80 -615
  50. package/dist/index20.mjs +251 -67
  51. package/dist/index21.mjs +67 -8
  52. package/dist/index22.mjs +82 -22
  53. package/dist/index23.mjs +8 -135
  54. package/dist/index24.mjs +25 -15
  55. package/dist/index25.mjs +144 -5
  56. package/dist/index26.mjs +5 -17
  57. package/dist/index27.mjs +15 -21
  58. package/dist/index28.mjs +21 -39
  59. package/dist/index29.mjs +40 -27
  60. package/dist/index3.mjs +645 -136
  61. package/dist/index30.mjs +27 -7
  62. package/dist/index31.mjs +10 -0
  63. package/dist/index4.mjs +166 -62
  64. package/dist/index5.mjs +65 -3
  65. package/dist/index6.mjs +2 -2
  66. package/dist/index7.mjs +2 -2
  67. package/dist/index8.mjs +2 -2
  68. package/dist/index9.mjs +3 -28
  69. package/dist/persistence/combinePersistenceAdapters.d.ts +7 -7
  70. package/dist/types/MemoryAdapter.d.ts +2 -2
  71. package/dist/utils/EventEmitter.d.ts +75 -0
  72. package/dist/utils/deepClone.d.ts +2 -2
  73. package/dist/utils/set.d.ts +2 -2
  74. package/dist/utils/uniqueBy.d.ts +2 -2
  75. package/package.json +1 -1
  76. package/dist/types/EventEmitter.d.ts +0 -25
@@ -1,26 +1,151 @@
1
1
  "use strict";
2
- const events = require("events");
3
- class EventEmitter extends events.EventEmitter {
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ class EventEmitter extends EventTarget {
6
+ constructor() {
7
+ super(...arguments);
8
+ __publicField(this, "_maxListeners", 100);
9
+ /**
10
+ * We store a map of:
11
+ * eventName => (originalListener => wrappedListener)
12
+ *
13
+ * The "wrappedListener" is the actual function passed to `addEventListener()`.
14
+ */
15
+ __publicField(this, "_listenerStore", /* @__PURE__ */ new Map());
16
+ }
17
+ setMaxListeners(max) {
18
+ this._maxListeners = max;
19
+ return this;
20
+ }
21
+ /**
22
+ * Subscribe to an event with a listener function.
23
+ * @param eventName The event name (key of E).
24
+ * @param listener A function that receives the emitted arguments.
25
+ * @returns The emitter instance (for chaining).
26
+ */
27
+ on(eventName, listener) {
28
+ let listenersMap = this._listenerStore.get(eventName);
29
+ if (!listenersMap) {
30
+ listenersMap = /* @__PURE__ */ new Map();
31
+ this._listenerStore.set(eventName, listenersMap);
32
+ }
33
+ const wrappedListener = (event) => {
34
+ const customEvent = event;
35
+ listener(...customEvent.detail ?? []);
36
+ };
37
+ listenersMap.set(listener, wrappedListener);
38
+ this.addEventListener(eventName, wrappedListener);
39
+ if (listenersMap.size > this._maxListeners) {
40
+ console.warn(`Possible EventEmitter memory leak detected. ${listenersMap.size} ${String(eventName)} listeners added. Use emitter.setMaxListeners() to increase limit.`);
41
+ }
42
+ return this;
43
+ }
44
+ /**
45
+ * Subscribe to an event with a listener function.
46
+ * @param eventName The event name (key of E).
47
+ * @param listener A function that receives the emitted arguments.
48
+ * @returns The emitter instance (for chaining).
49
+ */
50
+ addListener(eventName, listener) {
51
+ return this.on(eventName, listener);
52
+ }
53
+ /**
54
+ * Subscribe to an event, handling it only once. Automatically removes
55
+ * the listener after it fires the first time.
56
+ * @param eventName The event name (key of E).
57
+ * @param listener A function that receives the emitted arguments.
58
+ * @returns The emitter instance (for chaining).
59
+ */
60
+ once(eventName, listener) {
61
+ const onceWrapper = (...args) => {
62
+ listener(...args);
63
+ this.off(eventName, onceWrapper);
64
+ };
65
+ return this.on(eventName, onceWrapper);
66
+ }
4
67
  /**
5
- * Registers a listener for the specified event.
6
- * @template K - The key of the event in the `Events` record.
7
- * @param event - The name of the event to listen for.
8
- * @param listener - The listener function to execute when the event is emitted.
9
- * @returns The `EventEmitter` instance, allowing for method chaining.
68
+ * Unsubscribe a previously subscribed listener.
69
+ * @param eventName The event name (key of E).
70
+ * @param listener The original function passed to `on` or `once`.
71
+ * @returns The emitter instance (for chaining).
10
72
  */
11
- on(event, listener) {
12
- super.on(event, listener);
73
+ off(eventName, listener) {
74
+ const listenersMap = this._listenerStore.get(eventName);
75
+ if (!listenersMap)
76
+ return this;
77
+ const wrappedListener = listenersMap.get(listener);
78
+ if (wrappedListener) {
79
+ this.removeEventListener(eventName, wrappedListener);
80
+ listenersMap.delete(listener);
81
+ }
82
+ if (listenersMap.size === 0) {
83
+ this._listenerStore.delete(eventName);
84
+ }
13
85
  return this;
14
86
  }
15
87
  /**
16
- * Emits the specified event, triggering all registered listeners for that event.
17
- * @template K - The key of the event in the `Events` record.
18
- * @param event - The name of the event to emit.
19
- * @param args - The arguments to pass to the event listeners.
20
- * @returns A boolean indicating whether any listeners were triggered.
88
+ * Unsubscribe a previously subscribed listener.
89
+ * @param eventName The event name (key of E).
90
+ * @param listener The original function passed to `on` or `once`.
91
+ * @returns The emitter instance (for chaining).
21
92
  */
22
- emit(event, ...args) {
23
- return super.emit(event, ...args);
93
+ removeListener(eventName, listener) {
94
+ return this.off(eventName, listener);
95
+ }
96
+ /**
97
+ * Emit (dispatch) an event with a variable number of arguments.
98
+ * @param eventName The event name (key of E).
99
+ * @param args The arguments to pass to subscribed listeners.
100
+ * @returns A boolean indicating if the event was not cancelled.
101
+ */
102
+ emit(eventName, ...args) {
103
+ const event = new CustomEvent(eventName, { detail: args });
104
+ return this.dispatchEvent(event);
105
+ }
106
+ /**
107
+ * Returns the array of listener functions currently registered for a given event.
108
+ * @param eventName The event name (key of E).
109
+ * @returns An array of listener functions.
110
+ */
111
+ listeners(eventName) {
112
+ const listenersMap = this._listenerStore.get(eventName);
113
+ if (!listenersMap)
114
+ return [];
115
+ return [...listenersMap.keys()];
116
+ }
117
+ /**
118
+ * Returns the number of listeners for a given event.
119
+ * @param eventName The event name (key of E).
120
+ * @returns The number of listeners.
121
+ */
122
+ listenerCount(eventName) {
123
+ const listenersMap = this._listenerStore.get(eventName);
124
+ return listenersMap ? listenersMap.size : 0;
125
+ }
126
+ /**
127
+ * Removes all listeners for a given event, or all events if none is specified.
128
+ * @param eventName Optional. If omitted, clears all events’ listeners.
129
+ * @returns The emitter instance (for chaining).
130
+ */
131
+ removeAllListeners(eventName) {
132
+ if (eventName === void 0) {
133
+ for (const [eventName_, listenersMap] of this._listenerStore.entries()) {
134
+ for (const wrappedListener of listenersMap.values()) {
135
+ this.removeEventListener(eventName_, wrappedListener);
136
+ }
137
+ }
138
+ this._listenerStore.clear();
139
+ } else {
140
+ const listenersMap = this._listenerStore.get(eventName);
141
+ if (listenersMap) {
142
+ for (const wrappedListener of listenersMap.values()) {
143
+ this.removeEventListener(eventName, wrappedListener);
144
+ }
145
+ this._listenerStore.delete(eventName);
146
+ }
147
+ }
148
+ return this;
24
149
  }
25
150
  }
26
151
  module.exports = EventEmitter;
@@ -1,7 +1,40 @@
1
1
  "use strict";
2
- const mingo = require("mingo");
3
- function match(item, selector) {
4
- const query = new mingo.Query(selector);
5
- return query.test(item);
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const createIndexProvider = require("./index.cjs6.js");
4
+ const get = require("./index.cjs27.js");
5
+ const getMatchingKeys = require("./index.cjs28.js");
6
+ const serializeValue = require("./index.cjs18.js");
7
+ function createExternalIndex(field, index) {
8
+ return createIndexProvider({
9
+ query(selector) {
10
+ const keys = getMatchingKeys(field, selector);
11
+ if (keys == null)
12
+ return { matched: false };
13
+ const itemPositions = keys.reduce((memo, key) => [...memo, ...index.get(key) || []], []);
14
+ return {
15
+ matched: true,
16
+ positions: itemPositions,
17
+ fields: [field]
18
+ };
19
+ },
20
+ rebuild() {
21
+ }
22
+ });
6
23
  }
7
- module.exports = match;
24
+ function createIndex(field) {
25
+ const index = /* @__PURE__ */ new Map();
26
+ return {
27
+ ...createExternalIndex(field, index),
28
+ rebuild(items) {
29
+ index.clear();
30
+ items.forEach((item, i) => {
31
+ const value = serializeValue(get(item, field));
32
+ const current = index.get(value) || /* @__PURE__ */ new Set();
33
+ current.add(i);
34
+ index.set(value, current);
35
+ });
36
+ }
37
+ };
38
+ }
39
+ exports.createExternalIndex = createExternalIndex;
40
+ exports.default = createIndex;
@@ -1,43 +1,3 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- function clone(value) {
4
- if (typeof value === "function")
5
- throw new Error("Cloning functions is not supported");
6
- if (value === null || typeof value !== "object")
7
- return value;
8
- if (value instanceof Date)
9
- return new Date(value.getTime());
10
- if (Array.isArray(value))
11
- return value.map((item) => clone(item));
12
- if (value instanceof Map) {
13
- const result2 = /* @__PURE__ */ new Map();
14
- value.forEach((val, key) => {
15
- result2.set(key, clone(val));
16
- });
17
- return result2;
18
- }
19
- if (value instanceof Set) {
20
- const result2 = /* @__PURE__ */ new Set();
21
- value.forEach((val) => {
22
- result2.add(clone(val));
23
- });
24
- return result2;
25
- }
26
- if (value instanceof RegExp)
27
- return new RegExp(value);
28
- const result = {};
29
- for (const key in value) {
30
- if (Object.hasOwnProperty.call(value, key)) {
31
- result[key] = clone(value[key]);
32
- }
33
- }
34
- return result;
35
- }
36
- function deepClone(obj) {
37
- if (typeof structuredClone === "function")
38
- return structuredClone(obj);
39
- /* istanbul ignore next -- @preserve */
40
- return clone(obj);
41
- }
42
- exports.clone = clone;
43
- exports.default = deepClone;
2
+ const __viteBrowserExternal = {};
3
+ module.exports = __viteBrowserExternal;
@@ -1,13 +1,7 @@
1
1
  "use strict";
2
- function serializeValue(value) {
3
- if (typeof value === "string")
4
- return value;
5
- if (typeof value === "number")
6
- return value.toString();
7
- if (typeof value === "boolean")
8
- return value.toString();
9
- if (value instanceof Date)
10
- return value.toISOString();
11
- return JSON.stringify(value);
2
+ const mingo = require("mingo");
3
+ function match(item, selector) {
4
+ const query = new mingo.Query(selector);
5
+ return query.test(item);
12
6
  }
13
- module.exports = serializeValue;
7
+ module.exports = match;
@@ -1,20 +1,43 @@
1
1
  "use strict";
2
- function createSignal(dependency, initialValue, isEqual = Object.is) {
3
- let value = initialValue;
4
- const signal = {
5
- get() {
6
- if (dependency)
7
- dependency.depend();
8
- return value;
9
- },
10
- set(newValue) {
11
- if (isEqual(value, newValue))
12
- return;
13
- value = newValue;
14
- if (dependency)
15
- dependency.notify();
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ function clone(value) {
4
+ if (typeof value === "function")
5
+ throw new Error("Cloning functions is not supported");
6
+ if (value === null || typeof value !== "object")
7
+ return value;
8
+ if (value instanceof Date)
9
+ return new Date(value.getTime());
10
+ if (Array.isArray(value))
11
+ return value.map((item) => clone(item));
12
+ if (value instanceof Map) {
13
+ const result2 = /* @__PURE__ */ new Map();
14
+ value.forEach((currentValue, key) => {
15
+ result2.set(key, clone(currentValue));
16
+ });
17
+ return result2;
18
+ }
19
+ if (value instanceof Set) {
20
+ const result2 = /* @__PURE__ */ new Set();
21
+ value.forEach((currentValue) => {
22
+ result2.add(clone(currentValue));
23
+ });
24
+ return result2;
25
+ }
26
+ if (value instanceof RegExp)
27
+ return new RegExp(value);
28
+ const result = {};
29
+ for (const key in value) {
30
+ if (Object.hasOwnProperty.call(value, key)) {
31
+ result[key] = clone(value[key]);
16
32
  }
17
- };
18
- return signal;
33
+ }
34
+ return result;
19
35
  }
20
- module.exports = createSignal;
36
+ function deepClone(object) {
37
+ if (typeof structuredClone === "function")
38
+ return structuredClone(object);
39
+ /* istanbul ignore next -- @preserve */
40
+ return clone(object);
41
+ }
42
+ exports.clone = clone;
43
+ exports.default = deepClone;
@@ -1,247 +1,13 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const sortItems = require("./index.cjs21.js");
4
- const project = require("./index.cjs22.js");
5
- const Observer = require("./index.cjs23.js");
6
- function isInReactiveScope(reactivity) {
7
- if (!reactivity)
8
- return false;
9
- if (!reactivity.isInScope)
10
- return true;
11
- return reactivity.isInScope();
2
+ function serializeValue(value) {
3
+ if (typeof value === "string")
4
+ return value;
5
+ if (typeof value === "number")
6
+ return value.toString();
7
+ if (typeof value === "boolean")
8
+ return value.toString();
9
+ if (value instanceof Date)
10
+ return value.toISOString();
11
+ return JSON.stringify(value);
12
12
  }
13
- class Cursor {
14
- /**
15
- * Creates a new instance of the `Cursor` class.
16
- * Provides utilities for querying, observing, and transforming items from a collection.
17
- * @template T - The type of the items in the collection.
18
- * @template U - The transformed item type after applying transformations (default is T).
19
- * @param getItems - A function that retrieves the filtered list of items.
20
- * @param options - Optional configuration for the cursor.
21
- * @param options.transform - A transformation function to apply to each item when retrieving them.
22
- * @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
23
- * @param options.fields - A projection object defining which fields of the item should be included or excluded.
24
- * @param options.sort - A sort specifier to determine the order of the items.
25
- * @param options.skip - The number of items to skip from the beginning of the result set.
26
- * @param options.limit - The maximum number of items to return in the result set.
27
- * @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
28
- * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
29
- */
30
- constructor(getItems, options) {
31
- this.onCleanupCallbacks = [];
32
- this.getFilteredItems = getItems;
33
- this.options = options || {};
34
- }
35
- addGetters(item) {
36
- if (!isInReactiveScope(this.options.reactive))
37
- return item;
38
- const depend = this.depend.bind(this);
39
- return Object.entries(item).reduce((memo, [key, value]) => {
40
- Object.defineProperty(memo, key, {
41
- get() {
42
- depend({
43
- changedField: (notify) => (changedItem, changedFieldName) => {
44
- if (changedFieldName !== key || changedItem.id !== item.id)
45
- return;
46
- notify();
47
- }
48
- });
49
- return value;
50
- },
51
- enumerable: true,
52
- configurable: true
53
- });
54
- return memo;
55
- }, {});
56
- }
57
- transform(rawItem) {
58
- const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
59
- if (!this.options.transform)
60
- return item;
61
- return this.options.transform(item);
62
- }
63
- getItems() {
64
- const items = this.getFilteredItems();
65
- const { sort, skip, limit } = this.options;
66
- const sorted = sort ? sortItems(items, sort) : items;
67
- const skipped = skip ? sorted.slice(skip) : sorted;
68
- const limited = limit ? skipped.slice(0, limit) : skipped;
69
- const idExcluded = this.options.fields && this.options.fields.id === 0;
70
- return limited.map((item) => {
71
- if (!this.options.fields)
72
- return item;
73
- return Object.assign(Object.assign({}, idExcluded ? {} : { id: item.id }), project(item, this.options.fields));
74
- });
75
- }
76
- depend(changeEvents) {
77
- if (!this.options.reactive)
78
- return;
79
- if (!isInReactiveScope(this.options.reactive))
80
- return;
81
- const signal = this.options.reactive.create();
82
- signal.depend();
83
- const notify = () => signal.notify();
84
- function buildNotifier(event) {
85
- const eventHandler = changeEvents[event];
86
- return (...args) => {
87
- if (eventHandler === true) {
88
- notify();
89
- return;
90
- }
91
- if (typeof eventHandler !== "function")
92
- return;
93
- eventHandler(notify)(...args);
94
- };
95
- }
96
- const stop = this.observeRawChanges({
97
- added: buildNotifier("added"),
98
- addedBefore: buildNotifier("addedBefore"),
99
- changed: buildNotifier("changed"),
100
- changedField: buildNotifier("changedField"),
101
- movedBefore: buildNotifier("movedBefore"),
102
- removed: buildNotifier("removed")
103
- }, true);
104
- if (this.options.reactive.onDispose) {
105
- this.options.reactive.onDispose(() => stop(), signal);
106
- }
107
- this.onCleanup(stop);
108
- }
109
- ensureObserver() {
110
- if (!this.observer) {
111
- const observer = new Observer(() => {
112
- const requery = () => {
113
- observer.runChecks(this.getItems());
114
- };
115
- const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
116
- return () => {
117
- if (cleanup)
118
- cleanup();
119
- };
120
- });
121
- this.onCleanup(() => observer.stop());
122
- this.observer = observer;
123
- }
124
- return this.observer;
125
- }
126
- observeRawChanges(callbacks, skipInitial = false) {
127
- const observer = this.ensureObserver();
128
- observer.addCallbacks(callbacks, skipInitial);
129
- observer.runChecks(this.getItems());
130
- return () => {
131
- observer.removeCallbacks(callbacks);
132
- if (!observer.isEmpty())
133
- return;
134
- observer.stop();
135
- this.observer = void 0;
136
- };
137
- }
138
- /**
139
- * Cleans up all resources associated with the cursor, such as reactive bindings
140
- * and event listeners. This method should be called when the cursor is no longer needed
141
- * to prevent memory leaks.
142
- */
143
- cleanup() {
144
- this.onCleanupCallbacks.forEach((callback) => {
145
- callback();
146
- });
147
- this.onCleanupCallbacks = [];
148
- }
149
- /**
150
- * Registers a cleanup callback to be executed when the `cleanup` method is called.
151
- * Useful for managing resources and ensuring proper cleanup of bindings or listeners.
152
- * @param callback - A function to be executed during cleanup.
153
- */
154
- onCleanup(callback) {
155
- this.onCleanupCallbacks.push(callback);
156
- }
157
- /**
158
- * Iterates over each item in the cursor's result set, applying the provided callback
159
- * function to each transformed item.
160
- * ⚡️ this function is reactive!
161
- * @param callback - A function to execute for each item in the result set.
162
- * @param callback.item - The transformed item.
163
- */
164
- forEach(callback) {
165
- const items = this.getItems();
166
- this.depend(Object.assign({ addedBefore: true, removed: true, movedBefore: true }, this.options.fieldTracking ? {} : { changed: true }));
167
- items.forEach((item) => {
168
- callback(this.transform(item));
169
- });
170
- }
171
- /**
172
- * Creates a new array populated with the results of applying the provided callback
173
- * function to each transformed item in the cursor's result set.
174
- * ⚡️ this function is reactive!
175
- * @template V - The type of the items in the resulting array.
176
- * @param callback - A function to execute for each item in the result set.
177
- * @param callback.item - The transformed item.
178
- * @returns An array of results after applying the callback to each item.
179
- */
180
- map(callback) {
181
- const results = [];
182
- this.forEach((item) => {
183
- results.push(callback(item));
184
- });
185
- return results;
186
- }
187
- /**
188
- * Fetches all transformed items from the cursor's result set as an array.
189
- * Automatically applies filtering, sorting, and limiting as per the cursor's options.
190
- * ⚡️ this function is reactive!
191
- * @returns An array of transformed items in the result set.
192
- */
193
- fetch() {
194
- return this.map((item) => item);
195
- }
196
- /**
197
- * Counts the total number of items in the cursor's result set after applying
198
- * filtering and other criteria.
199
- * ⚡️ this function is reactive!
200
- * @returns The total number of items in the result set.
201
- */
202
- count() {
203
- const items = this.getItems();
204
- this.depend({
205
- added: true,
206
- removed: true
207
- });
208
- return items.length;
209
- }
210
- /**
211
- * Observes changes to the cursor's result set and triggers the specified callbacks
212
- * when items are added, removed, or updated. Supports reactivity and transformation.
213
- * @param callbacks - An object containing the callback functions to handle different change events.
214
- * @param callbacks.added - Triggered when an item is added to the result set.
215
- * @param callbacks.removed - Triggered when an item is removed from the result set.
216
- * @param callbacks.changed - Triggered when an item in the result set is modified.
217
- * @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
218
- * @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
219
- * @param callbacks.changedField - Triggered when a specific field of an item changes.
220
- * @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
221
- * @returns A function to stop observing changes.
222
- */
223
- observeChanges(callbacks, skipInitial = false) {
224
- return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
225
- if (!callback)
226
- return memo;
227
- return Object.assign(Object.assign({}, memo), { [callbackName]: (item, before) => {
228
- const transformedValue = this.transform(item);
229
- const hasBeforeParam = before !== void 0;
230
- const transformedBeforeValue = hasBeforeParam && before ? this.transform(before) : null;
231
- return callback(transformedValue, ...hasBeforeParam ? [transformedBeforeValue] : []);
232
- } });
233
- }, {}), skipInitial);
234
- }
235
- /**
236
- * Forces the cursor to re-evaluate its result set by re-fetching items
237
- * from the collection. This is useful when the underlying data or query
238
- * criteria have changed, and you want to ensure the cursor reflects the latest state.
239
- */
240
- requery() {
241
- if (!this.observer)
242
- return;
243
- this.observer.runChecks(this.getItems());
244
- }
245
- }
246
- exports.default = Cursor;
247
- exports.isInReactiveScope = isInReactiveScope;
13
+ module.exports = serializeValue;