@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.
- package/dist/.vite/manifest.json +88 -59
- package/dist/AutoFetchCollection.d.ts +3 -3
- package/dist/Collection/Cursor.d.ts +10 -7
- package/dist/Collection/index.d.ts +27 -11
- package/dist/Collection/types.d.ts +1 -0
- package/dist/ReplicatedCollection.d.ts +4 -4
- package/dist/index.cjs.js +26 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +14 -28
- package/dist/index10.cjs.js +265 -0
- package/dist/index10.mjs +263 -28
- package/dist/index11.cjs.js +125 -0
- package/dist/index11.mjs +124 -12
- package/dist/index12.cjs.js +15 -0
- package/dist/index12.mjs +14 -5
- package/dist/index13.cjs.js +23 -0
- package/dist/index13.mjs +22 -133
- package/dist/index14.cjs.js +12 -0
- package/dist/index14.mjs +11 -73
- package/dist/index15.cjs.js +22 -0
- package/dist/index15.mjs +21 -10
- package/dist/index16.cjs.js +33 -0
- package/dist/index16.mjs +32 -27
- package/dist/index17.cjs.js +15 -0
- package/dist/index17.mjs +14 -142
- package/dist/index18.cjs.js +98 -0
- package/dist/index18.mjs +97 -7
- package/dist/index19.cjs.js +11 -0
- package/dist/index19.mjs +10 -41
- package/dist/index2.cjs.js +23 -0
- package/dist/index2.mjs +22 -260
- package/dist/index20.cjs.js +45 -0
- package/dist/index20.mjs +44 -15
- package/dist/index21.cjs.js +45 -0
- package/dist/index21.mjs +44 -26
- package/dist/index22.cjs.js +69 -0
- package/dist/index22.mjs +66 -77
- package/dist/index23.cjs.js +778 -0
- package/dist/index23.mjs +775 -81
- package/dist/index24.cjs.js +11 -0
- package/dist/index24.mjs +10 -18
- package/dist/index25.cjs.js +81 -0
- package/dist/index25.mjs +80 -29
- package/dist/index26.cjs.js +98 -0
- package/dist/index26.mjs +96 -8
- package/dist/index27.cjs.js +154 -0
- package/dist/index27.mjs +153 -7
- package/dist/index28.cjs.js +11 -0
- package/dist/index28.mjs +10 -29
- package/dist/index29.cjs.js +11 -0
- package/dist/index29.mjs +10 -42
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -788
- package/dist/index4.cjs.js +37 -0
- package/dist/index4.mjs +36 -167
- package/dist/index5.cjs.js +35 -0
- package/dist/index5.mjs +34 -67
- package/dist/index6.cjs.js +47 -0
- package/dist/index6.mjs +46 -5
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -5
- package/dist/index8.cjs.js +19 -0
- package/dist/index8.mjs +18 -5
- package/dist/index9.cjs.js +139 -0
- package/dist/index9.mjs +138 -5
- package/dist/utils/deepClone.d.ts +1 -1
- package/package.json +3 -3
- package/dist/index.cjs10.js +0 -29
- package/dist/index.cjs11.js +0 -12
- package/dist/index.cjs12.js +0 -5
- package/dist/index.cjs13.js +0 -133
- package/dist/index.cjs14.js +0 -74
- package/dist/index.cjs15.js +0 -10
- package/dist/index.cjs16.js +0 -27
- package/dist/index.cjs17.js +0 -142
- package/dist/index.cjs18.js +0 -7
- package/dist/index.cjs19.js +0 -42
- package/dist/index.cjs2.js +0 -261
- package/dist/index.cjs20.js +0 -15
- package/dist/index.cjs21.js +0 -26
- package/dist/index.cjs22.js +0 -79
- package/dist/index.cjs23.js +0 -84
- package/dist/index.cjs24.js +0 -18
- package/dist/index.cjs25.js +0 -29
- package/dist/index.cjs26.js +0 -9
- package/dist/index.cjs27.js +0 -7
- package/dist/index.cjs28.js +0 -29
- package/dist/index.cjs29.js +0 -42
- package/dist/index.cjs3.js +0 -788
- package/dist/index.cjs4.js +0 -167
- package/dist/index.cjs5.js +0 -68
- package/dist/index.cjs6.js +0 -5
- package/dist/index.cjs7.js +0 -5
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -5
package/dist/index27.mjs
CHANGED
|
@@ -1,8 +1,154 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
42
|
-
|
|
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;
|