@signaldb/core 1.7.2 → 1.8.1

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