@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
package/dist/index17.mjs CHANGED
@@ -1,143 +1,15 @@
1
- import isEqual from "./index10.mjs";
2
- import uniqueBy from "./index26.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 newItems - The new list of items to compare against the previous state.
51
- */
52
- runChecks(newItems) {
53
- const oldItemsMap = new Map(this.previousItems.map((item, index) => [
54
- item.id,
55
- { item, index, beforeItem: this.previousItems[index + 1] || null }
56
- ]));
57
- const newItemsMap = new Map(newItems.map((item, index) => [
58
- item.id,
59
- { item, index, beforeItem: newItems[index + 1] || null }
60
- ]));
61
- if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
62
- oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
63
- const newItem = newItemsMap.get(oldItem.id);
64
- if (newItem) {
65
- if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
66
- this.call("changed", newItem.item);
67
- if (this.hasCallbacks(["changedField"])) {
68
- const keys = uniqueBy([
69
- ...Object.keys(newItem.item),
70
- ...Object.keys(oldItem)
71
- ], (value) => value);
72
- keys.forEach((key) => {
73
- if (isEqual(newItem.item[key], oldItem[key]))
74
- return;
75
- this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
76
- });
77
- }
78
- }
79
- if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
80
- this.call("movedBefore", newItem.item, newItem.beforeItem);
81
- }
82
- } else {
83
- this.call("removed", oldItem);
84
- }
85
- });
86
- }
87
- if (this.hasCallbacks(["added", "addedBefore"])) {
88
- newItems.forEach((newItem, index) => {
89
- const oldItem = oldItemsMap.get(newItem.id);
90
- if (oldItem)
91
- return;
92
- this.call("added", newItem);
93
- this.call("addedBefore", newItem, newItems[index + 1] || null);
94
- });
95
- }
96
- this.previousItems = newItems;
97
- Object.keys(this.callbacks).forEach((key) => {
98
- const event = key;
99
- const callbacks = this.callbacks[event];
100
- this.callbacks[event] = callbacks.map((callback) => ({
101
- ...callback,
102
- options: {
103
- ...callback.options,
104
- isInitial: false
105
- }
106
- }));
107
- });
108
- }
109
- /**
110
- * Stops the observer by unbinding all events and cleaning up resources.
111
- */
112
- stop() {
113
- this.unbindEvents();
114
- }
115
- /**
116
- * Registers callbacks for specific events to observe changes in the collection.
117
- * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
118
- * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
119
- */
120
- addCallbacks(callbacks, skipInitial = false) {
121
- Object.keys(callbacks).forEach((key) => {
122
- const typedKey = key;
123
- this.callbacks[typedKey].push({
124
- callback: callbacks[typedKey],
125
- options: { skipInitial, isInitial: true }
126
- });
127
- });
128
- }
129
- /**
130
- * Removes the specified callbacks for specific events, unregistering them from the observer.
131
- * @param callbacks - An object containing the callbacks to be removed for various events.
132
- */
133
- removeCallbacks(callbacks) {
134
- Object.keys(callbacks).forEach((key) => {
135
- const typedKey = key;
136
- const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
137
- this.callbacks[typedKey].splice(index, 1);
138
- });
139
- }
1
+ //#region src/utils/intersection.ts
2
+ /**
3
+ * Computes the intersection of multiple arrays, returning an array of unique elements
4
+ * that are present in all the input arrays.
5
+ * @template T - The type of elements in the arrays.
6
+ * @param arrays - A variable number of arrays to compute the intersection of.
7
+ * @returns An array containing the unique elements found in all the input arrays.
8
+ * - If no arrays are provided, returns an empty array.
9
+ */
10
+ function intersection(...arrays) {
11
+ if (arrays.length === 0) return [];
12
+ return [...new Set(arrays.reduce((a, b) => a.filter((c) => b.includes(c))))];
140
13
  }
141
- export {
142
- Observer as default
143
- };
14
+ //#endregion
15
+ export { intersection as default };
@@ -0,0 +1,98 @@
1
+ const require_intersection = require("./index17.cjs.js");
2
+ //#region src/Collection/getIndexInfo.ts
3
+ /**
4
+ * Retrieves merged index information for a given flat selector by querying multiple
5
+ * index providers. Combines results from all index providers to determine matched positions
6
+ * and an optimized selector.
7
+ * @template T - The type of the items in the collection.
8
+ * @template I - The type of the unique identifier for the items.
9
+ * @param indexProviders - An array of index providers to query.
10
+ * @param selector - The flat selector used to filter items.
11
+ * @returns An object containing:
12
+ * - `matched`: A boolean indicating if the selector matched any items.
13
+ * - `positions`: An array of matched item positions.
14
+ * - `optimizedSelector`: A flat selector optimized based on the index results.
15
+ */
16
+ function getMergedIndexInfo(indexProviders, selector) {
17
+ return indexProviders.reduce((memo, indexProvider) => {
18
+ const info = indexProvider.query(selector);
19
+ if (!info.matched) return memo;
20
+ const optimizedSelector = info.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
21
+ return {
22
+ matched: true,
23
+ positions: [...new Set(memo.matched ? require_intersection.default(memo.positions, info.positions) : info.positions)],
24
+ optimizedSelector
25
+ };
26
+ }, {
27
+ matched: false,
28
+ positions: [],
29
+ optimizedSelector: { ...selector }
30
+ });
31
+ }
32
+ /**
33
+ * Retrieves index information for a given complex selector by querying multiple
34
+ * index providers. Handles nested `$and` and `$or` conditions in the selector and
35
+ * optimizes the selector to minimize processing overhead.
36
+ * @template T - The type of the items in the collection.
37
+ * @template I - The type of the unique identifier for the items.
38
+ * @param indexProviders - An array of index providers to query.
39
+ * @param selector - The complex selector used to filter items.
40
+ * @returns An object containing:
41
+ * - `matched`: A boolean indicating if the selector matched any items.
42
+ * - `positions`: An array of matched item positions.
43
+ * - `optimizedSelector`: A selector optimized based on the index results, with unused
44
+ * conditions removed.
45
+ */
46
+ function getIndexInfo(indexProviders, selector) {
47
+ if (selector == null || Object.keys(selector).length <= 0) return {
48
+ matched: false,
49
+ positions: [],
50
+ optimizedSelector: selector
51
+ };
52
+ const { $and, $or, ...rest } = selector;
53
+ const flatInfo = getMergedIndexInfo(indexProviders, rest);
54
+ let { matched, positions } = flatInfo;
55
+ const newSelector = flatInfo.optimizedSelector;
56
+ if (Array.isArray($and)) {
57
+ const $andNew = [];
58
+ for (const sel of $and) {
59
+ const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
60
+ if (selMatched) {
61
+ positions = matched ? require_intersection.default(positions, selPositions) : selPositions;
62
+ matched = true;
63
+ if (Object.keys(optimizedSelector).length > 0) $andNew.push(optimizedSelector);
64
+ } else $andNew.push(sel);
65
+ }
66
+ if ($andNew.length > 0) newSelector.$and = $andNew;
67
+ }
68
+ if (Array.isArray($or)) {
69
+ const $orNew = [];
70
+ const matchedBefore = matched;
71
+ const positionsBefore = positions;
72
+ let hasNonIndexField = false;
73
+ for (const sel of $or) {
74
+ const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
75
+ if (selMatched) {
76
+ positions = [...new Set([...positions, ...selPositions])];
77
+ matched = true;
78
+ if (Object.keys(optimizedSelector).length > 0) $orNew.push(optimizedSelector);
79
+ } else {
80
+ $orNew.push(sel);
81
+ hasNonIndexField = true;
82
+ }
83
+ }
84
+ if ($orNew.length > 0) newSelector.$or = $orNew;
85
+ if (hasNonIndexField) {
86
+ newSelector.$or = $or;
87
+ matched = matchedBefore;
88
+ positions = positionsBefore;
89
+ }
90
+ }
91
+ return {
92
+ matched,
93
+ positions: positions || [],
94
+ optimizedSelector: newSelector
95
+ };
96
+ }
97
+ //#endregion
98
+ exports.default = getIndexInfo;
package/dist/index18.mjs CHANGED
@@ -1,8 +1,98 @@
1
- import { Query } from "mingo";
2
- function match(item, selector) {
3
- const query = new Query(selector);
4
- return query.test(item);
1
+ import intersection from "./index17.mjs";
2
+ //#region src/Collection/getIndexInfo.ts
3
+ /**
4
+ * Retrieves merged index information for a given flat selector by querying multiple
5
+ * index providers. Combines results from all index providers to determine matched positions
6
+ * and an optimized selector.
7
+ * @template T - The type of the items in the collection.
8
+ * @template I - The type of the unique identifier for the items.
9
+ * @param indexProviders - An array of index providers to query.
10
+ * @param selector - The flat selector used to filter items.
11
+ * @returns An object containing:
12
+ * - `matched`: A boolean indicating if the selector matched any items.
13
+ * - `positions`: An array of matched item positions.
14
+ * - `optimizedSelector`: A flat selector optimized based on the index results.
15
+ */
16
+ function getMergedIndexInfo(indexProviders, selector) {
17
+ return indexProviders.reduce((memo, indexProvider) => {
18
+ const info = indexProvider.query(selector);
19
+ if (!info.matched) return memo;
20
+ const optimizedSelector = info.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
21
+ return {
22
+ matched: true,
23
+ positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
24
+ optimizedSelector
25
+ };
26
+ }, {
27
+ matched: false,
28
+ positions: [],
29
+ optimizedSelector: { ...selector }
30
+ });
5
31
  }
6
- export {
7
- match as default
8
- };
32
+ /**
33
+ * Retrieves index information for a given complex selector by querying multiple
34
+ * index providers. Handles nested `$and` and `$or` conditions in the selector and
35
+ * optimizes the selector to minimize processing overhead.
36
+ * @template T - The type of the items in the collection.
37
+ * @template I - The type of the unique identifier for the items.
38
+ * @param indexProviders - An array of index providers to query.
39
+ * @param selector - The complex selector used to filter items.
40
+ * @returns An object containing:
41
+ * - `matched`: A boolean indicating if the selector matched any items.
42
+ * - `positions`: An array of matched item positions.
43
+ * - `optimizedSelector`: A selector optimized based on the index results, with unused
44
+ * conditions removed.
45
+ */
46
+ function getIndexInfo(indexProviders, selector) {
47
+ if (selector == null || Object.keys(selector).length <= 0) return {
48
+ matched: false,
49
+ positions: [],
50
+ optimizedSelector: selector
51
+ };
52
+ const { $and, $or, ...rest } = selector;
53
+ const flatInfo = getMergedIndexInfo(indexProviders, rest);
54
+ let { matched, positions } = flatInfo;
55
+ const newSelector = flatInfo.optimizedSelector;
56
+ if (Array.isArray($and)) {
57
+ const $andNew = [];
58
+ for (const sel of $and) {
59
+ const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
60
+ if (selMatched) {
61
+ positions = matched ? intersection(positions, selPositions) : selPositions;
62
+ matched = true;
63
+ if (Object.keys(optimizedSelector).length > 0) $andNew.push(optimizedSelector);
64
+ } else $andNew.push(sel);
65
+ }
66
+ if ($andNew.length > 0) newSelector.$and = $andNew;
67
+ }
68
+ if (Array.isArray($or)) {
69
+ const $orNew = [];
70
+ const matchedBefore = matched;
71
+ const positionsBefore = positions;
72
+ let hasNonIndexField = false;
73
+ for (const sel of $or) {
74
+ const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
75
+ if (selMatched) {
76
+ positions = [...new Set([...positions, ...selPositions])];
77
+ matched = true;
78
+ if (Object.keys(optimizedSelector).length > 0) $orNew.push(optimizedSelector);
79
+ } else {
80
+ $orNew.push(sel);
81
+ hasNonIndexField = true;
82
+ }
83
+ }
84
+ if ($orNew.length > 0) newSelector.$or = $orNew;
85
+ if (hasNonIndexField) {
86
+ newSelector.$or = $or;
87
+ matched = matchedBefore;
88
+ positions = positionsBefore;
89
+ }
90
+ }
91
+ return {
92
+ matched,
93
+ positions: positions || [],
94
+ optimizedSelector: newSelector
95
+ };
96
+ }
97
+ //#endregion
98
+ export { getIndexInfo as default };
@@ -0,0 +1,11 @@
1
+ //#region src/createIndexProvider.ts
2
+ /**
3
+ * Creates an IndexProvider based on the given definition.
4
+ * @param definition - The definition of the IndexProvider.
5
+ * @returns The created IndexProvider.
6
+ */
7
+ function createIndexProvider(definition) {
8
+ return definition;
9
+ }
10
+ //#endregion
11
+ exports.default = createIndexProvider;
package/dist/index19.mjs CHANGED
@@ -1,42 +1,11 @@
1
- function clone(value) {
2
- if (typeof value === "function")
3
- throw new Error("Cloning functions is not supported");
4
- if (value === null || typeof value !== "object")
5
- return value;
6
- if (value instanceof Date)
7
- return new Date(value);
8
- if (Array.isArray(value))
9
- return value.map((item) => clone(item));
10
- if (value instanceof Map) {
11
- const result2 = /* @__PURE__ */ new Map();
12
- value.forEach((currentValue, key) => {
13
- result2.set(key, clone(currentValue));
14
- });
15
- return result2;
16
- }
17
- if (value instanceof Set) {
18
- const result2 = /* @__PURE__ */ new Set();
19
- value.forEach((currentValue) => {
20
- result2.add(clone(currentValue));
21
- });
22
- return result2;
23
- }
24
- if (value instanceof RegExp)
25
- return new RegExp(value);
26
- const result = {};
27
- for (const key in value) {
28
- if (Object.hasOwnProperty.call(value, key)) {
29
- result[key] = clone(value[key]);
30
- }
31
- }
32
- return result;
1
+ //#region src/createIndexProvider.ts
2
+ /**
3
+ * Creates an IndexProvider based on the given definition.
4
+ * @param definition - The definition of the IndexProvider.
5
+ * @returns The created IndexProvider.
6
+ */
7
+ function createIndexProvider(definition) {
8
+ return definition;
33
9
  }
34
- function deepClone(object) {
35
- if (typeof structuredClone === "function")
36
- return structuredClone(object);
37
- return clone(object);
38
- }
39
- export {
40
- clone,
41
- deepClone as default
42
- };
10
+ //#endregion
11
+ export { createIndexProvider as default };
@@ -0,0 +1,23 @@
1
+ //#region src/utils/get.ts
2
+ /**
3
+ * Retrieves the value at a specified path within an object.
4
+ * Supports dot and bracket notation for navigating nested properties.
5
+ * @template T - The type of the object to retrieve the value from.
6
+ * @param value - The object to navigate.
7
+ * @param path - The path (dot or bracket notation) to the desired value.
8
+ * @returns The value at the specified path, or `undefined` if the path does not exist.
9
+ */
10
+ function get(value, path) {
11
+ const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
12
+ if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) return;
13
+ const segments = normalized.split(".");
14
+ let current = value;
15
+ for (const key of segments) {
16
+ if (current == null) return;
17
+ current = current[key];
18
+ }
19
+ if (current === void 0) return;
20
+ return current;
21
+ }
22
+ //#endregion
23
+ exports.default = get;