@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/index27.mjs CHANGED
@@ -1,8 +1,154 @@
1
- function intersection(...arrays) {
2
- if (arrays.length === 0)
3
- return [];
4
- return [...new Set(arrays.reduce((a, b) => a.filter((c) => b.includes(c))))];
5
- }
6
- export {
7
- intersection as default
1
+ import createSignal from "./index16.mjs";
2
+ import ReplicatedCollection from "./index26.mjs";
3
+ //#region src/AutoFetchCollection.ts
4
+ /**
5
+ * A special collection that automatically fetches items when they are needed.
6
+ */
7
+ var AutoFetchCollection = class extends ReplicatedCollection {
8
+ activeObservers = /* @__PURE__ */ new Map();
9
+ observerTimeouts = /* @__PURE__ */ new Map();
10
+ purgeDelay;
11
+ idQueryCache = /* @__PURE__ */ new Map();
12
+ itemsCache = /* @__PURE__ */ new Map();
13
+ fetchQueryItems;
14
+ triggerReload = null;
15
+ reactivityAdapter = null;
16
+ loadingSignals = /* @__PURE__ */ new Map();
17
+ isFetchingSignal;
18
+ mergeItems;
19
+ /**
20
+ * @param options {Object} - Options for the collection.
21
+ * @param options.fetchQueryItems {Function} - A function that fetches items from the server. It takes the selector as an argument and returns a promise that resolves to an object with an `items` property.
22
+ * @param options.purgeDelay {Number} - The delay in milliseconds before purging an item from the cache.
23
+ */
24
+ constructor(options) {
25
+ let triggerRemoteChange;
26
+ super({
27
+ ...options,
28
+ pull: () => Promise.resolve({ items: [...this.itemsCache.values()].reduce((memo, items) => {
29
+ const newItems = [...memo];
30
+ items.forEach((item) => {
31
+ const index = newItems.findIndex((i) => i.id === item.id);
32
+ if (index === -1) {
33
+ newItems.push(item);
34
+ return;
35
+ }
36
+ newItems[index] = this.mergeItems(newItems[index], item);
37
+ });
38
+ return newItems;
39
+ }, []) }),
40
+ registerRemoteChange: async (onChange) => {
41
+ triggerRemoteChange = onChange;
42
+ }
43
+ });
44
+ this.mergeItems = options.mergeItems ?? ((itemA, itemB) => ({
45
+ ...itemA,
46
+ ...itemB
47
+ }));
48
+ this.purgeDelay = options.purgeDelay ?? 1e4;
49
+ this.isFetchingSignal = createSignal(options.reactivity, false);
50
+ if (!triggerRemoteChange) throw new Error("No triggerRemoteChange method found. Looks like your persistence adapter was not registered");
51
+ this.triggerReload = triggerRemoteChange;
52
+ this.reactivityAdapter = options.reactivity ?? null;
53
+ this.fetchQueryItems = options.fetchQueryItems;
54
+ this.on("observer.created", (selector) => this.handleObserverCreation(selector ?? {}));
55
+ this.on("observer.disposed", (selector) => setTimeout(() => this.handleObserverDisposal(selector ?? {}), 100));
56
+ if (options.registerRemoteChange) options.registerRemoteChange(() => this.forceRefetch());
57
+ }
58
+ /**
59
+ * Registers a query manually that items should be fetched for it
60
+ * @param selector {Object} Selector of the query
61
+ */
62
+ registerQuery(selector) {
63
+ this.handleObserverCreation(selector);
64
+ }
65
+ /**
66
+ * Unregisters a query manually that items are not fetched anymore for it
67
+ * @param selector {Object} Selector of the query
68
+ */
69
+ unregisterQuery(selector) {
70
+ this.handleObserverDisposal(selector);
71
+ }
72
+ getKeyForSelector(selector) {
73
+ return JSON.stringify(selector);
74
+ }
75
+ async forceRefetch() {
76
+ return Promise.all([...this.activeObservers.values()].map(({ selector }) => this.fetchSelector(selector))).then(() => {});
77
+ }
78
+ fetchSelector(selector) {
79
+ this.isFetchingSignal.set(true);
80
+ return this.fetchQueryItems(selector).then((response) => {
81
+ if (!response.items) throw new Error("AutoFetchCollection currently only works with a full item response");
82
+ this.itemsCache.set(this.getKeyForSelector(selector), response.items);
83
+ response.items.forEach((item) => {
84
+ const queries = this.idQueryCache.get(item.id) ?? [];
85
+ queries.push(selector);
86
+ this.idQueryCache.set(item.id, queries);
87
+ });
88
+ this.setLoading(selector, true);
89
+ this.once("persistence.received", () => {
90
+ this.setLoading(selector, false);
91
+ });
92
+ if (!this.triggerReload) throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
93
+ this.triggerReload();
94
+ }).catch((error) => {
95
+ this.emit("persistence.error", error);
96
+ }).finally(() => {
97
+ this.isFetchingSignal.set(false);
98
+ });
99
+ }
100
+ handleObserverCreation(selector) {
101
+ const activeObservers = this.activeObservers.get(this.getKeyForSelector(selector))?.count ?? 0;
102
+ this.activeObservers.set(this.getKeyForSelector(selector), {
103
+ selector,
104
+ count: activeObservers + 1
105
+ });
106
+ const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
107
+ if (timeout) clearTimeout(timeout);
108
+ if (activeObservers === 0) this.fetchSelector(selector);
109
+ }
110
+ handleObserverDisposal(selector) {
111
+ const activeObservers = (this.activeObservers.get(this.getKeyForSelector(selector))?.count ?? 0) - 1;
112
+ if (activeObservers > 0) {
113
+ this.activeObservers.set(this.getKeyForSelector(selector), {
114
+ selector,
115
+ count: activeObservers
116
+ });
117
+ return;
118
+ }
119
+ const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
120
+ if (timeout) clearTimeout(timeout);
121
+ const removeObserver = () => {
122
+ this.activeObservers.delete(this.getKeyForSelector(selector));
123
+ this.itemsCache.delete(this.getKeyForSelector(selector));
124
+ if (!this.triggerReload) throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
125
+ this.triggerReload();
126
+ };
127
+ if (this.purgeDelay === 0) {
128
+ removeObserver();
129
+ return;
130
+ }
131
+ this.observerTimeouts.set(this.getKeyForSelector(selector), setTimeout(removeObserver, this.purgeDelay));
132
+ }
133
+ ensureSignal(selector) {
134
+ if (!this.reactivityAdapter) throw new Error("No reactivity adapter found");
135
+ if (!this.loadingSignals.has(this.getKeyForSelector(selector))) this.loadingSignals.set(this.getKeyForSelector(selector), createSignal(this.reactivityAdapter, false));
136
+ return this.loadingSignals.get(this.getKeyForSelector(selector));
137
+ }
138
+ setLoading(selector, value) {
139
+ this.ensureSignal(selector).set(value);
140
+ }
141
+ /**
142
+ * Indicates wether a query is currently been loaded
143
+ * ⚡️ this function is reactive!
144
+ * @param selector {Object} Selector of the query
145
+ * @returns The loading state
146
+ */
147
+ isLoading(selector) {
148
+ const isPushing = this.isPushing();
149
+ if (!selector) return this.isFetchingSignal.get() || isPushing;
150
+ return this.ensureSignal(selector).get() || isPushing;
151
+ }
8
152
  };
153
+ //#endregion
154
+ export { AutoFetchCollection as default };
@@ -0,0 +1,11 @@
1
+ //#region src/createMemoryAdapter.ts
2
+ /**
3
+ * Creates an MemoryAdapter based on the given definition.
4
+ * @param definition - The definition of the MemoryAdapter.
5
+ * @returns The created MemoryAdapter.
6
+ */
7
+ function createMemoryAdapter(definition) {
8
+ return definition;
9
+ }
10
+ //#endregion
11
+ exports.default = createMemoryAdapter;
package/dist/index28.mjs CHANGED
@@ -1,30 +1,11 @@
1
- import isFieldExpression from "./index29.mjs";
2
- import serializeValue from "./index20.mjs";
3
- function getMatchingKeys(field, selector) {
4
- const result = { include: null, exclude: null };
5
- const fieldSelector = selector[field];
6
- if (fieldSelector instanceof RegExp)
7
- return result;
8
- if (fieldSelector == null)
9
- return result;
10
- if (isFieldExpression(fieldSelector)) {
11
- if (fieldSelector.$ne != null) {
12
- result.exclude = [serializeValue(fieldSelector.$ne)];
13
- return result;
14
- }
15
- if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
16
- result.include = fieldSelector.$in.map(serializeValue);
17
- return result;
18
- }
19
- if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
20
- result.exclude = fieldSelector.$nin.map(serializeValue);
21
- return result;
22
- }
23
- return { include: null, exclude: null };
24
- }
25
- result.include = [serializeValue(fieldSelector)];
26
- return result;
1
+ //#region src/createMemoryAdapter.ts
2
+ /**
3
+ * Creates an MemoryAdapter based on the given definition.
4
+ * @param definition - The definition of the MemoryAdapter.
5
+ * @returns The created MemoryAdapter.
6
+ */
7
+ function createMemoryAdapter(definition) {
8
+ return definition;
27
9
  }
28
- export {
29
- getMatchingKeys as default
30
- };
10
+ //#endregion
11
+ export { createMemoryAdapter as default };
@@ -0,0 +1,11 @@
1
+ //#region src/createReactivityAdapter.ts
2
+ /**
3
+ * Creates an ReactivityAdapter based on the given definition.
4
+ * @param definition - The definition of the ReactivityAdapter.
5
+ * @returns The created ReactivityAdapter.
6
+ */
7
+ function createReactivityAdapter(definition) {
8
+ return definition;
9
+ }
10
+ //#endregion
11
+ exports.default = createReactivityAdapter;
package/dist/index29.mjs CHANGED
@@ -1,43 +1,11 @@
1
- const expressionKeys = /* @__PURE__ */ new Set([
2
- "$eq",
3
- "$gt",
4
- "$gte",
5
- "$lt",
6
- "$lte",
7
- "$in",
8
- "$nin",
9
- "$ne",
10
- "$exists",
11
- "$not",
12
- "$expr",
13
- "$jsonSchema",
14
- "$mod",
15
- "$regex",
16
- "$options",
17
- "$text",
18
- "$where",
19
- "$all",
20
- "$elemMatch",
21
- "$size",
22
- "$bitsAllClear",
23
- "$bitsAllSet",
24
- "$bitsAnyClear",
25
- "$bitsAnySet"
26
- ]);
27
- function isFieldExpression(expression) {
28
- if (typeof expression !== "object" || expression == null) {
29
- return false;
30
- }
31
- const keys = Object.keys(expression);
32
- if (keys.length === 0) {
33
- return false;
34
- }
35
- const hasInvalidKeys = keys.some((key) => !expressionKeys.has(key));
36
- if (hasInvalidKeys)
37
- return false;
38
- const hasValidKeys = keys.every((key) => expressionKeys.has(key));
39
- return hasValidKeys;
1
+ //#region src/createReactivityAdapter.ts
2
+ /**
3
+ * Creates an ReactivityAdapter based on the given definition.
4
+ * @param definition - The definition of the ReactivityAdapter.
5
+ * @returns The created ReactivityAdapter.
6
+ */
7
+ function createReactivityAdapter(definition) {
8
+ return definition;
40
9
  }
41
- export {
42
- isFieldExpression as default
43
- };
10
+ //#endregion
11
+ export { createReactivityAdapter as default };
@@ -0,0 +1,19 @@
1
+ const require_get = require("./index2.cjs.js");
2
+ let fast_sort = require("fast-sort");
3
+ //#region src/utils/sortItems.ts
4
+ /**
5
+ * Sorts an array of items based on multiple fields and their specified sort order.
6
+ * Uses the `fast-sort` library for efficient sorting.
7
+ * @template T - The type of the items in the array.
8
+ * @param items - The array of items to be sorted.
9
+ * @param sortFields - An object defining the sort order for each field.
10
+ * - Keys are the field names, and values are either `1` (ascending) or `-1` (descending).
11
+ * @returns A new array of items sorted based on the specified fields and their order.
12
+ */
13
+ function sortItems(items, sortFields) {
14
+ return (0, fast_sort.sort)(items).by(Object.entries(sortFields).map(([key, value]) => {
15
+ return { [value === 1 ? "asc" : "desc"]: (i) => require_get.default(i, key) };
16
+ }));
17
+ }
18
+ //#endregion
19
+ exports.default = sortItems;