@signaldb/core 2.0.0-beta.5 → 2.0.0-beta.7

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 (104) hide show
  1. package/dist/.vite/manifest.json +118 -84
  2. package/dist/AsyncDataAdapter.d.ts +1 -1
  3. package/dist/index.cjs.js +33 -34
  4. package/dist/index.mjs +17 -35
  5. package/dist/index10.cjs.js +23 -0
  6. package/dist/index10.mjs +21 -17
  7. package/dist/index11.cjs.js +45 -0
  8. package/dist/index11.mjs +44 -15
  9. package/dist/index12.cjs.js +22 -0
  10. package/dist/index12.mjs +21 -47
  11. package/dist/index13.cjs.js +45 -0
  12. package/dist/index13.mjs +44 -365
  13. package/dist/index14.cjs.js +86 -0
  14. package/dist/index14.mjs +85 -416
  15. package/dist/index15.cjs.js +15 -0
  16. package/dist/index15.mjs +14 -206
  17. package/dist/index16.cjs.js +136 -0
  18. package/dist/index16.mjs +135 -388
  19. package/dist/index17.cjs.js +47 -0
  20. package/dist/index17.mjs +45 -578
  21. package/dist/index18.cjs.js +15 -0
  22. package/dist/index18.mjs +14 -157
  23. package/dist/index19.cjs.js +23 -0
  24. package/dist/index19.mjs +22 -26
  25. package/dist/index2.cjs.js +33 -0
  26. package/dist/index2.mjs +32 -258
  27. package/dist/index20.cjs.js +37 -0
  28. package/dist/index20.mjs +36 -41
  29. package/dist/index21.cjs.js +35 -0
  30. package/dist/index21.mjs +34 -16
  31. package/dist/index22.cjs.js +25 -0
  32. package/dist/index22.mjs +24 -104
  33. package/dist/index23.cjs.js +19 -0
  34. package/dist/index23.mjs +18 -128
  35. package/dist/index24.cjs.js +342 -0
  36. package/dist/index24.mjs +340 -6
  37. package/dist/index25.cjs.js +519 -0
  38. package/dist/index25.mjs +518 -27
  39. package/dist/index26.cjs.js +11 -0
  40. package/dist/index26.mjs +10 -10
  41. package/dist/index27.cjs.js +11 -0
  42. package/dist/index27.mjs +10 -29
  43. package/dist/index28.cjs.js +90 -0
  44. package/dist/index28.mjs +88 -46
  45. package/dist/index29.cjs.js +370 -0
  46. package/dist/index29.mjs +369 -8
  47. package/dist/index3.cjs.js +19 -0
  48. package/dist/index3.mjs +18 -539
  49. package/dist/index30.cjs.js +72 -0
  50. package/dist/index30.mjs +71 -7
  51. package/dist/index31.cjs.js +191 -0
  52. package/dist/index31.mjs +190 -42
  53. package/dist/index32.cjs.js +21 -0
  54. package/dist/index32.mjs +20 -29
  55. package/dist/index33.cjs.js +336 -0
  56. package/dist/index33.mjs +335 -5
  57. package/dist/index34.cjs.js +539 -0
  58. package/dist/index34.mjs +537 -8
  59. package/dist/index4.cjs.js +150 -0
  60. package/dist/index4.mjs +149 -5
  61. package/dist/index5.cjs.js +254 -0
  62. package/dist/index5.mjs +252 -4
  63. package/dist/index6.cjs.js +125 -0
  64. package/dist/index6.mjs +124 -29
  65. package/dist/index7.cjs.js +33 -0
  66. package/dist/index7.mjs +32 -12
  67. package/dist/index8.cjs.js +12 -0
  68. package/dist/index8.mjs +10 -4
  69. package/dist/index9.cjs.js +11 -0
  70. package/dist/index9.mjs +10 -133
  71. package/package.json +1 -1
  72. package/dist/index.cjs10.js +0 -18
  73. package/dist/index.cjs11.js +0 -15
  74. package/dist/index.cjs12.js +0 -48
  75. package/dist/index.cjs13.js +0 -365
  76. package/dist/index.cjs14.js +0 -416
  77. package/dist/index.cjs15.js +0 -206
  78. package/dist/index.cjs16.js +0 -388
  79. package/dist/index.cjs17.js +0 -579
  80. package/dist/index.cjs18.js +0 -157
  81. package/dist/index.cjs19.js +0 -26
  82. package/dist/index.cjs2.js +0 -259
  83. package/dist/index.cjs20.js +0 -42
  84. package/dist/index.cjs21.js +0 -16
  85. package/dist/index.cjs22.js +0 -104
  86. package/dist/index.cjs23.js +0 -129
  87. package/dist/index.cjs24.js +0 -7
  88. package/dist/index.cjs25.js +0 -27
  89. package/dist/index.cjs26.js +0 -10
  90. package/dist/index.cjs27.js +0 -29
  91. package/dist/index.cjs28.js +0 -46
  92. package/dist/index.cjs29.js +0 -8
  93. package/dist/index.cjs3.js +0 -540
  94. package/dist/index.cjs30.js +0 -7
  95. package/dist/index.cjs31.js +0 -42
  96. package/dist/index.cjs32.js +0 -29
  97. package/dist/index.cjs33.js +0 -5
  98. package/dist/index.cjs34.js +0 -9
  99. package/dist/index.cjs4.js +0 -5
  100. package/dist/index.cjs5.js +0 -5
  101. package/dist/index.cjs6.js +0 -29
  102. package/dist/index.cjs7.js +0 -12
  103. package/dist/index.cjs8.js +0 -5
  104. package/dist/index.cjs9.js +0 -133
package/dist/index18.mjs CHANGED
@@ -1,158 +1,15 @@
1
- import isEqual from "./index6.mjs";
2
- import uniqueBy from "./index34.mjs";
3
- class Observer {
4
- previousItems = [];
5
- callbacks;
6
- unbindEvents;
7
- /**
8
- * Creates a new instance of the `Observer` class.
9
- * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
10
- * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
11
- */
12
- constructor(bindEvents) {
13
- this.callbacks = {
14
- added: [],
15
- addedBefore: [],
16
- changed: [],
17
- changedField: [],
18
- movedBefore: [],
19
- removed: []
20
- };
21
- this.unbindEvents = bindEvents();
22
- }
23
- call(event, ...args) {
24
- this.callbacks[event].forEach(({ callback, options }) => {
25
- if (!options.skipInitial || !options.isInitial) {
26
- callback(...args);
27
- }
28
- });
29
- }
30
- hasCallbacks(events) {
31
- return events.some((event) => this.callbacks[event].length > 0);
32
- }
33
- /**
34
- * Determines if the observer has no active callbacks registered for any events.
35
- * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
36
- */
37
- isEmpty() {
38
- return !this.hasCallbacks([
39
- "added",
40
- "addedBefore",
41
- "changed",
42
- "changedField",
43
- "movedBefore",
44
- "removed"
45
- ]);
46
- }
47
- /**
48
- * Compares the previous state of items with the new state and triggers the appropriate callbacks
49
- * for events such as added, removed, changed, or moved items.
50
- * @param getItems - A function that returns a promise resolving to the new items or the items themselves.
51
- */
52
- runChecks(getItems) {
53
- const result = getItems();
54
- if (result instanceof Promise) {
55
- result.then((newItems) => this.checkItems(newItems)).catch((error) => {
56
- console.error("Error while asynchronously querying items", error);
57
- });
58
- } else {
59
- this.checkItems(result);
60
- }
61
- }
62
- checkItems(newItems) {
63
- const oldItemsMap = new Map(this.previousItems.map((item, index) => [
64
- item.id,
65
- { item, index, beforeItem: this.previousItems[index + 1] || null }
66
- ]));
67
- const newItemsMap = new Map(newItems.map((item, index) => [
68
- item.id,
69
- { item, index, beforeItem: newItems[index + 1] || null }
70
- ]));
71
- if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
72
- oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
73
- const newItem = newItemsMap.get(oldItem.id);
74
- if (newItem) {
75
- if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
76
- this.call("changed", newItem.item);
77
- if (this.hasCallbacks(["changedField"])) {
78
- const keys = uniqueBy([
79
- ...Object.keys(newItem.item),
80
- ...Object.keys(oldItem)
81
- ], (value) => value);
82
- keys.forEach((key) => {
83
- if (isEqual(newItem.item[key], oldItem[key]))
84
- return;
85
- this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
86
- });
87
- }
88
- }
89
- if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
90
- this.call("movedBefore", newItem.item, newItem.beforeItem);
91
- }
92
- } else {
93
- this.call("removed", oldItem);
94
- }
95
- });
96
- }
97
- if (this.hasCallbacks(["added", "addedBefore"])) {
98
- newItems.forEach((newItem, index) => {
99
- const oldItem = oldItemsMap.get(newItem.id);
100
- if (oldItem)
101
- return;
102
- this.call("added", newItem);
103
- this.call("addedBefore", newItem, newItems[index + 1] || null);
104
- });
105
- }
106
- this.previousItems = newItems;
107
- Object.keys(this.callbacks).forEach((key) => {
108
- const event = key;
109
- const callbacks = this.callbacks[event];
110
- this.callbacks[event] = callbacks.map((callback) => ({
111
- ...callback,
112
- options: {
113
- ...callback.options,
114
- isInitial: false
115
- }
116
- }));
117
- });
118
- }
119
- stopped = false;
120
- /**
121
- * Stops the observer by unbinding all events and cleaning up resources.
122
- * Safe to call multiple times - will only unbind once.
123
- */
124
- stop() {
125
- if (this.stopped)
126
- return;
127
- this.stopped = true;
128
- this.unbindEvents();
129
- }
130
- /**
131
- * Registers callbacks for specific events to observe changes in the collection.
132
- * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
133
- * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
134
- */
135
- addCallbacks(callbacks, skipInitial = false) {
136
- Object.keys(callbacks).forEach((key) => {
137
- const typedKey = key;
138
- this.callbacks[typedKey].push({
139
- callback: callbacks[typedKey],
140
- options: { skipInitial, isInitial: true }
141
- });
142
- });
143
- }
144
- /**
145
- * Removes the specified callbacks for specific events, unregistering them from the observer.
146
- * @param callbacks - An object containing the callbacks to be removed for various events.
147
- */
148
- removeCallbacks(callbacks) {
149
- Object.keys(callbacks).forEach((key) => {
150
- const typedKey = key;
151
- const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
152
- this.callbacks[typedKey].splice(index, 1);
153
- });
154
- }
1
+ import { Query } from "mingo";
2
+ //#region src/utils/match.ts
3
+ /**
4
+ * Tests whether a given item matches a specified selector.
5
+ * Uses the `mingo` library to evaluate the query.
6
+ * @template T - The type of the item being tested.
7
+ * @param item - The item to test against the selector.
8
+ * @param selector - The query selector used to match the item.
9
+ * @returns A boolean indicating whether the item matches the selector.
10
+ */
11
+ function match(item, selector) {
12
+ return new Query(selector).test(item);
155
13
  }
156
- export {
157
- Observer as default
158
- };
14
+ //#endregion
15
+ export { match as default };
@@ -0,0 +1,23 @@
1
+ const require_deepClone = require("./index17.cjs.js");
2
+ let mingo = require("mingo");
3
+ //#region src/utils/modify.ts
4
+ /**
5
+ * Applies a modifier to an object and returns a new modified object.
6
+ * @template T - The type of the object to be modified.
7
+ * @param item - The object to be modified.
8
+ * @param modifier - The modifier to apply. This can be any transformation logic.
9
+ * @returns - Returns a new object with the modifications applied.
10
+ * @example
11
+ * const item = { a: 1, b: 2 }
12
+ * const modifier = { $set: { b: 3, c: 4 } }
13
+ * const result = modify(item, modifier)
14
+ * // result: { a: 1, b: 3, c: 4 }
15
+ */
16
+ function modify(item, modifier) {
17
+ if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
+ const clonedItem = require_deepClone.default(item);
19
+ (0, mingo.update)(clonedItem, modifier);
20
+ return clonedItem;
21
+ }
22
+ //#endregion
23
+ exports.default = modify;
package/dist/index19.mjs CHANGED
@@ -1,27 +1,23 @@
1
- function createSignal(reactivityAdapter, initialValue, isEqual = Object.is) {
2
- let value = initialValue;
3
- const dependency = reactivityAdapter?.create();
4
- const isInReactiveScope = () => {
5
- if (!reactivityAdapter?.isInScope)
6
- return true;
7
- return reactivityAdapter.isInScope();
8
- };
9
- const signal = {
10
- get() {
11
- if (dependency && isInReactiveScope())
12
- dependency.depend();
13
- return value;
14
- },
15
- set(newValue) {
16
- if (isEqual(value, newValue))
17
- return;
18
- value = newValue;
19
- if (dependency)
20
- dependency.notify();
21
- }
22
- };
23
- return signal;
1
+ import deepClone from "./index17.mjs";
2
+ import { update } from "mingo";
3
+ //#region src/utils/modify.ts
4
+ /**
5
+ * Applies a modifier to an object and returns a new modified object.
6
+ * @template T - The type of the object to be modified.
7
+ * @param item - The object to be modified.
8
+ * @param modifier - The modifier to apply. This can be any transformation logic.
9
+ * @returns - Returns a new object with the modifications applied.
10
+ * @example
11
+ * const item = { a: 1, b: 2 }
12
+ * const modifier = { $set: { b: 3, c: 4 } }
13
+ * const result = modify(item, modifier)
14
+ * // result: { a: 1, b: 3, c: 4 }
15
+ */
16
+ function modify(item, modifier) {
17
+ if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
+ const clonedItem = deepClone(item);
19
+ update(clonedItem, modifier);
20
+ return clonedItem;
24
21
  }
25
- export {
26
- createSignal as default
27
- };
22
+ //#endregion
23
+ export { modify as default };
@@ -0,0 +1,33 @@
1
+ //#region src/utils/isEqual.ts
2
+ /**
3
+ * Compares two values for deep equality.
4
+ * @param a - The first value to compare.
5
+ * @param b - The second value to compare.
6
+ * @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
7
+ * @example
8
+ * isEqual({ a: 1 }, { a: 1 }); // true
9
+ * isEqual([1, 2], [1, 2]); // true
10
+ * isEqual(new Date(0), new Date(0)); // true
11
+ * isEqual(/abc/, /abc/); // true
12
+ * isEqual({ a: 1 }, { a: 2 }); // false
13
+ * isEqual(null, null); // true
14
+ */
15
+ function isEqual(a, b) {
16
+ if (Object.is(a, b)) return true;
17
+ if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
18
+ if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
19
+ if (typeof a !== "object") return false;
20
+ if (typeof b !== "object") return false;
21
+ if (a === null) return false;
22
+ if (b === null) return false;
23
+ const aKeys = Object.keys(a);
24
+ const bKeys = Object.keys(b);
25
+ if (aKeys.length !== bKeys.length) return false;
26
+ for (const key of aKeys) {
27
+ if (!bKeys.includes(key)) return false;
28
+ if (!isEqual(a[key], b[key])) return false;
29
+ }
30
+ return true;
31
+ }
32
+ //#endregion
33
+ exports.default = isEqual;
package/dist/index2.mjs CHANGED
@@ -1,259 +1,33 @@
1
- import Observer from "./index18.mjs";
2
- function isInReactiveScope(reactivity) {
3
- if (!reactivity)
4
- return false;
5
- if (!reactivity.isInScope)
6
- return true;
7
- return reactivity.isInScope();
1
+ //#region src/utils/isEqual.ts
2
+ /**
3
+ * Compares two values for deep equality.
4
+ * @param a - The first value to compare.
5
+ * @param b - The second value to compare.
6
+ * @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
7
+ * @example
8
+ * isEqual({ a: 1 }, { a: 1 }); // true
9
+ * isEqual([1, 2], [1, 2]); // true
10
+ * isEqual(new Date(0), new Date(0)); // true
11
+ * isEqual(/abc/, /abc/); // true
12
+ * isEqual({ a: 1 }, { a: 2 }); // false
13
+ * isEqual(null, null); // true
14
+ */
15
+ function isEqual(a, b) {
16
+ if (Object.is(a, b)) return true;
17
+ if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
18
+ if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
19
+ if (typeof a !== "object") return false;
20
+ if (typeof b !== "object") return false;
21
+ if (a === null) return false;
22
+ if (b === null) return false;
23
+ const aKeys = Object.keys(a);
24
+ const bKeys = Object.keys(b);
25
+ if (aKeys.length !== bKeys.length) return false;
26
+ for (const key of aKeys) {
27
+ if (!bKeys.includes(key)) return false;
28
+ if (!isEqual(a[key], b[key])) return false;
29
+ }
30
+ return true;
8
31
  }
9
- class Cursor {
10
- observer;
11
- getItems;
12
- options;
13
- onCleanupCallbacks = [];
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
- * @param options.transformAll - A function that will be able to solve the n+1 problem
30
- */
31
- constructor(getItems, options) {
32
- this.getItems = 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
- depend(changeEvents) {
64
- if (this.options?.async)
65
- return;
66
- if (!isInReactiveScope(this.options.reactive)) {
67
- console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
68
- }
69
- if (!this.options.reactive)
70
- return;
71
- const signal = this.options.reactive.create();
72
- signal.depend();
73
- const notify = () => signal.notify();
74
- function buildNotifier(event) {
75
- const eventHandler = changeEvents[event];
76
- return (...args) => {
77
- if (eventHandler === true) {
78
- notify();
79
- return;
80
- }
81
- if (typeof eventHandler !== "function")
82
- return;
83
- eventHandler(notify)(...args);
84
- };
85
- }
86
- const stop = this.observeRawChanges({
87
- added: buildNotifier("added"),
88
- addedBefore: buildNotifier("addedBefore"),
89
- changed: buildNotifier("changed"),
90
- changedField: buildNotifier("changedField"),
91
- movedBefore: buildNotifier("movedBefore"),
92
- removed: buildNotifier("removed")
93
- }, true);
94
- if (this.options.reactive.onDispose) {
95
- this.options.reactive.onDispose(() => stop(), signal);
96
- }
97
- this.onCleanup(stop);
98
- }
99
- ensureObserver() {
100
- if (!this.observer) {
101
- const observer = new Observer(() => {
102
- const requery = () => {
103
- observer.runChecks(this.getItems);
104
- };
105
- const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
106
- return () => {
107
- if (cleanup)
108
- cleanup();
109
- };
110
- });
111
- this.onCleanup(() => observer.stop());
112
- this.observer = observer;
113
- }
114
- return this.observer;
115
- }
116
- observeRawChanges(callbacks, skipInitial = false) {
117
- const observer = this.ensureObserver();
118
- observer.addCallbacks(callbacks, skipInitial);
119
- observer.runChecks(this.getItems);
120
- return () => {
121
- observer.removeCallbacks(callbacks);
122
- if (!observer.isEmpty())
123
- return;
124
- observer.stop();
125
- this.observer = void 0;
126
- };
127
- }
128
- /**
129
- * Cleans up all resources associated with the cursor, such as reactive bindings
130
- * and event listeners. This method should be called when the cursor is no longer needed
131
- * to prevent memory leaks.
132
- */
133
- cleanup() {
134
- this.onCleanupCallbacks.forEach((callback) => {
135
- callback();
136
- });
137
- this.onCleanupCallbacks = [];
138
- }
139
- /**
140
- * Registers a cleanup callback to be executed when the `cleanup` method is called.
141
- * Useful for managing resources and ensuring proper cleanup of bindings or listeners.
142
- * @param callback - A function to be executed during cleanup.
143
- */
144
- onCleanup(callback) {
145
- this.onCleanupCallbacks.push(callback);
146
- }
147
- /**
148
- * Iterates over each item in the cursor's result set, applying the provided callback
149
- * function to each transformed item.
150
- * ⚡️ this function is reactive!
151
- * @param callback - A function to execute for each item in the result set.
152
- * @param callback.item - The transformed item.
153
- * @returns A promise that resolves when all items have been processed, or void if not in async mode.
154
- */
155
- forEach(callback) {
156
- this.depend({
157
- addedBefore: true,
158
- removed: true,
159
- movedBefore: true,
160
- ...this.options.fieldTracking ? {} : { changed: true }
161
- });
162
- const executeForEach = (items) => {
163
- items.forEach((item) => {
164
- callback(this.transform(item));
165
- });
166
- };
167
- const result = this.getItems();
168
- if (result instanceof Promise) {
169
- return result.then(executeForEach);
170
- } else {
171
- executeForEach(result);
172
- return void 0;
173
- }
174
- }
175
- /**
176
- * Creates a new array populated with the results of applying the provided callback
177
- * function to each transformed item in the cursor's result set.
178
- * ⚡️ this function is reactive!
179
- * @template V - The type of the items in the resulting array.
180
- * @param callback - A function to execute for each item in the result set.
181
- * @param callback.item - The transformed item.
182
- * @returns An array of results after applying the callback to each item.
183
- */
184
- map(callback) {
185
- const results = [];
186
- const maybePromise = this.forEach((item) => {
187
- results.push(callback(item));
188
- });
189
- if (maybePromise instanceof Promise) {
190
- return maybePromise.then(() => results);
191
- }
192
- return results;
193
- }
194
- /**
195
- * Fetches all transformed items from the cursor's result set as an array.
196
- * Automatically applies filtering, sorting, and limiting as per the cursor's options.
197
- * ⚡️ this function is reactive!
198
- * @returns An array of transformed items in the result set.
199
- */
200
- fetch() {
201
- return this.map((item) => item);
202
- }
203
- /**
204
- * Counts the total number of items in the cursor's result set after applying
205
- * filtering and other criteria.
206
- * ⚡️ this function is reactive!
207
- * @returns The total number of items in the result set.
208
- */
209
- count() {
210
- this.depend({
211
- added: true,
212
- removed: true
213
- });
214
- const maybePromise = this.getItems();
215
- return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
216
- }
217
- /**
218
- * Observes changes to the cursor's result set and triggers the specified callbacks
219
- * when items are added, removed, or updated. Supports reactivity and transformation.
220
- * @param callbacks - An object containing the callback functions to handle different change events.
221
- * @param callbacks.added - Triggered when an item is added to the result set.
222
- * @param callbacks.removed - Triggered when an item is removed from the result set.
223
- * @param callbacks.changed - Triggered when an item in the result set is modified.
224
- * @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
225
- * @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
226
- * @param callbacks.changedField - Triggered when a specific field of an item changes.
227
- * @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
228
- * @returns A function to stop observing changes.
229
- */
230
- observeChanges(callbacks, skipInitial = false) {
231
- return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
232
- if (!callback)
233
- return memo;
234
- return {
235
- ...memo,
236
- [callbackName]: (item, before) => {
237
- const transformedValue = this.transform(item);
238
- const hasBeforeParameter = before !== void 0;
239
- const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
240
- return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
241
- }
242
- };
243
- }, {}), skipInitial);
244
- }
245
- /**
246
- * Forces the cursor to re-evaluate its result set by re-fetching items
247
- * from the collection. This is useful when the underlying data or query
248
- * criteria have changed, and you want to ensure the cursor reflects the latest state.
249
- */
250
- requery() {
251
- if (!this.observer)
252
- return;
253
- this.observer.runChecks(this.getItems);
254
- }
255
- }
256
- export {
257
- Cursor as default,
258
- isInReactiveScope
259
- };
32
+ //#endregion
33
+ export { isEqual as default };
@@ -0,0 +1,37 @@
1
+ //#region src/utils/set.ts
2
+ /**
3
+ * Sets a value at a specified path within an object. Creates nested structures
4
+ * (arrays or objects) as needed to set the value at the correct location. Supports
5
+ * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
6
+ * @template T - The type of the object to modify.
7
+ * @template K - The type of the value to set.
8
+ * @param object - The object to modify. The object is mutated directly.
9
+ * @param path - The path (dot or bracket notation) where the value should be set.
10
+ * @param value - The value to set at the specified path.
11
+ * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
12
+ * @returns The modified object.
13
+ */
14
+ function set(object, path, value, deleteIfUndefined = false) {
15
+ if (object == null) return object;
16
+ const segments = path.split(/[.[\]]/g);
17
+ if (segments[0] === "") segments.shift();
18
+ if (segments.at(-1) === "") segments.pop();
19
+ const apply = (node) => {
20
+ if (segments.length > 1) {
21
+ const key = segments.shift();
22
+ const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
23
+ if (node[key] === void 0) node[key] = nextIsNumber ? [] : {};
24
+ apply(node[key]);
25
+ } else {
26
+ if (deleteIfUndefined && value === void 0) {
27
+ delete node[segments[0]];
28
+ return;
29
+ }
30
+ node[segments[0]] = value;
31
+ }
32
+ };
33
+ apply(object);
34
+ return object;
35
+ }
36
+ //#endregion
37
+ exports.default = set;