@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/index.cjs4.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const ReplicatedCollection = require("./index.cjs23.js");
|
|
3
|
-
const createSignal = require("./index.cjs21.js");
|
|
4
|
-
class AutoFetchCollection extends ReplicatedCollection.default {
|
|
5
|
-
activeObservers = /* @__PURE__ */ new Map();
|
|
6
|
-
observerTimeouts = /* @__PURE__ */ new Map();
|
|
7
|
-
purgeDelay;
|
|
8
|
-
idQueryCache = /* @__PURE__ */ new Map();
|
|
9
|
-
itemsCache = /* @__PURE__ */ new Map();
|
|
10
|
-
fetchQueryItems;
|
|
11
|
-
triggerReload = null;
|
|
12
|
-
reactivityAdapter = null;
|
|
13
|
-
loadingSignals = /* @__PURE__ */ new Map();
|
|
14
|
-
isFetchingSignal;
|
|
15
|
-
mergeItems;
|
|
16
|
-
/**
|
|
17
|
-
* @param options {Object} - Options for the collection.
|
|
18
|
-
* @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.
|
|
19
|
-
* @param options.purgeDelay {Number} - The delay in milliseconds before purging an item from the cache.
|
|
20
|
-
*/
|
|
21
|
-
constructor(options) {
|
|
22
|
-
let triggerRemoteChange;
|
|
23
|
-
super({
|
|
24
|
-
...options,
|
|
25
|
-
pull: () => Promise.resolve({
|
|
26
|
-
items: [...this.itemsCache.values()].reduce((memo, items) => {
|
|
27
|
-
const newItems = [...memo];
|
|
28
|
-
items.forEach((item) => {
|
|
29
|
-
const index = newItems.findIndex((i) => i.id === item.id);
|
|
30
|
-
if (index === -1) {
|
|
31
|
-
newItems.push(item);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
newItems[index] = this.mergeItems(newItems[index], item);
|
|
35
|
-
});
|
|
36
|
-
return newItems;
|
|
37
|
-
}, [])
|
|
38
|
-
}),
|
|
39
|
-
registerRemoteChange: async (onChange) => {
|
|
40
|
-
triggerRemoteChange = onChange;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
this.mergeItems = options.mergeItems ?? ((itemA, itemB) => ({ ...itemA, ...itemB }));
|
|
44
|
-
this.purgeDelay = options.purgeDelay ?? 1e4;
|
|
45
|
-
this.isFetchingSignal = createSignal(options.reactivity, false);
|
|
46
|
-
if (!triggerRemoteChange)
|
|
47
|
-
throw new Error("No triggerRemoteChange method found. Looks like your persistence adapter was not registered");
|
|
48
|
-
this.triggerReload = triggerRemoteChange;
|
|
49
|
-
this.reactivityAdapter = options.reactivity ?? null;
|
|
50
|
-
this.fetchQueryItems = options.fetchQueryItems;
|
|
51
|
-
this.on("observer.created", (selector) => this.handleObserverCreation(selector ?? {}));
|
|
52
|
-
this.on("observer.disposed", (selector) => setTimeout(() => this.handleObserverDisposal(selector ?? {}), 100));
|
|
53
|
-
if (options.registerRemoteChange) {
|
|
54
|
-
void options.registerRemoteChange(() => this.forceRefetch());
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Registers a query manually that items should be fetched for it
|
|
59
|
-
* @param selector {Object} Selector of the query
|
|
60
|
-
*/
|
|
61
|
-
registerQuery(selector) {
|
|
62
|
-
this.handleObserverCreation(selector);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Unregisters a query manually that items are not fetched anymore for it
|
|
66
|
-
* @param selector {Object} Selector of the query
|
|
67
|
-
*/
|
|
68
|
-
unregisterQuery(selector) {
|
|
69
|
-
this.handleObserverDisposal(selector);
|
|
70
|
-
}
|
|
71
|
-
getKeyForSelector(selector) {
|
|
72
|
-
return JSON.stringify(selector);
|
|
73
|
-
}
|
|
74
|
-
async forceRefetch() {
|
|
75
|
-
return Promise.all([...this.activeObservers.values()].map(({ selector }) => this.fetchSelector(selector))).then(() => {
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
fetchSelector(selector) {
|
|
79
|
-
this.isFetchingSignal.set(true);
|
|
80
|
-
return this.fetchQueryItems(selector).then((response) => {
|
|
81
|
-
if (!response.items)
|
|
82
|
-
throw new Error("AutoFetchCollection currently only works with a full item response");
|
|
83
|
-
this.itemsCache.set(this.getKeyForSelector(selector), response.items);
|
|
84
|
-
response.items.forEach((item) => {
|
|
85
|
-
const queries = this.idQueryCache.get(item.id) ?? [];
|
|
86
|
-
queries.push(selector);
|
|
87
|
-
this.idQueryCache.set(item.id, queries);
|
|
88
|
-
});
|
|
89
|
-
this.setLoading(selector, true);
|
|
90
|
-
this.once("persistence.received", () => {
|
|
91
|
-
this.setLoading(selector, false);
|
|
92
|
-
});
|
|
93
|
-
if (!this.triggerReload)
|
|
94
|
-
throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
|
|
95
|
-
void this.triggerReload();
|
|
96
|
-
}).catch((error) => {
|
|
97
|
-
this.emit("persistence.error", error);
|
|
98
|
-
}).finally(() => {
|
|
99
|
-
this.isFetchingSignal.set(false);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
handleObserverCreation(selector) {
|
|
103
|
-
const activeObservers = this.activeObservers.get(this.getKeyForSelector(selector))?.count ?? 0;
|
|
104
|
-
this.activeObservers.set(this.getKeyForSelector(selector), {
|
|
105
|
-
selector,
|
|
106
|
-
count: activeObservers + 1
|
|
107
|
-
});
|
|
108
|
-
const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
|
|
109
|
-
if (timeout)
|
|
110
|
-
clearTimeout(timeout);
|
|
111
|
-
if (activeObservers === 0)
|
|
112
|
-
void this.fetchSelector(selector);
|
|
113
|
-
}
|
|
114
|
-
handleObserverDisposal(selector) {
|
|
115
|
-
const currentObservers = this.activeObservers.get(this.getKeyForSelector(selector))?.count ?? 0;
|
|
116
|
-
const activeObservers = currentObservers - 1;
|
|
117
|
-
if (activeObservers > 0) {
|
|
118
|
-
this.activeObservers.set(this.getKeyForSelector(selector), {
|
|
119
|
-
selector,
|
|
120
|
-
count: activeObservers
|
|
121
|
-
});
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
const timeout = this.observerTimeouts.get(this.getKeyForSelector(selector));
|
|
125
|
-
if (timeout)
|
|
126
|
-
clearTimeout(timeout);
|
|
127
|
-
const removeObserver = () => {
|
|
128
|
-
this.activeObservers.delete(this.getKeyForSelector(selector));
|
|
129
|
-
this.itemsCache.delete(this.getKeyForSelector(selector));
|
|
130
|
-
if (!this.triggerReload)
|
|
131
|
-
throw new Error("No triggerReload method found. Looks like your persistence adapter was not registered");
|
|
132
|
-
void this.triggerReload();
|
|
133
|
-
};
|
|
134
|
-
if (this.purgeDelay === 0) {
|
|
135
|
-
removeObserver();
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
this.observerTimeouts.set(this.getKeyForSelector(selector), setTimeout(removeObserver, this.purgeDelay));
|
|
139
|
-
}
|
|
140
|
-
ensureSignal(selector) {
|
|
141
|
-
if (!this.reactivityAdapter)
|
|
142
|
-
throw new Error("No reactivity adapter found");
|
|
143
|
-
if (!this.loadingSignals.has(this.getKeyForSelector(selector))) {
|
|
144
|
-
this.loadingSignals.set(this.getKeyForSelector(selector), createSignal(this.reactivityAdapter, false));
|
|
145
|
-
}
|
|
146
|
-
return this.loadingSignals.get(this.getKeyForSelector(selector));
|
|
147
|
-
}
|
|
148
|
-
setLoading(selector, value) {
|
|
149
|
-
const signal = this.ensureSignal(selector);
|
|
150
|
-
signal.set(value);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Indicates wether a query is currently been loaded
|
|
154
|
-
* ⚡️ this function is reactive!
|
|
155
|
-
* @param selector {Object} Selector of the query
|
|
156
|
-
* @returns The loading state
|
|
157
|
-
*/
|
|
158
|
-
isLoading(selector) {
|
|
159
|
-
const isPushing = this.isPushing();
|
|
160
|
-
if (!selector) {
|
|
161
|
-
return this.isFetchingSignal.get() || isPushing;
|
|
162
|
-
}
|
|
163
|
-
const signal = this.ensureSignal(selector);
|
|
164
|
-
return signal.get() || isPushing;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
module.exports = AutoFetchCollection;
|
package/dist/index.cjs5.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const createPersistenceAdapter = require("./index.cjs8.js");
|
|
4
|
-
function createTemporaryFallbackExecutor(firstResolvingPromiseFunction, secondResolvingPromiseFunction, options) {
|
|
5
|
-
const cacheTimeout = options?.cacheTimeout;
|
|
6
|
-
let isResolved = false;
|
|
7
|
-
let resolvedValue = null;
|
|
8
|
-
let timeout = null;
|
|
9
|
-
let secondaryPromise = null;
|
|
10
|
-
return (...args) => {
|
|
11
|
-
if (secondaryPromise == null) {
|
|
12
|
-
if (timeout) {
|
|
13
|
-
clearTimeout(timeout);
|
|
14
|
-
timeout = null;
|
|
15
|
-
}
|
|
16
|
-
secondaryPromise = secondResolvingPromiseFunction(...args).then((result) => {
|
|
17
|
-
{
|
|
18
|
-
timeout = setTimeout(() => {
|
|
19
|
-
isResolved = false;
|
|
20
|
-
resolvedValue = null;
|
|
21
|
-
secondaryPromise = null;
|
|
22
|
-
}, cacheTimeout);
|
|
23
|
-
}
|
|
24
|
-
isResolved = true;
|
|
25
|
-
resolvedValue = result;
|
|
26
|
-
if (options?.onResolve)
|
|
27
|
-
options.onResolve(resolvedValue);
|
|
28
|
-
return result;
|
|
29
|
-
});
|
|
30
|
-
} else if (isResolved) {
|
|
31
|
-
return secondaryPromise;
|
|
32
|
-
}
|
|
33
|
-
return firstResolvingPromiseFunction(...args);
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function combinePersistenceAdapters(slowAdapter, fastAdapter) {
|
|
37
|
-
let handleChange = null;
|
|
38
|
-
const readExecutor = createTemporaryFallbackExecutor(() => fastAdapter.load(), () => slowAdapter.load(), {
|
|
39
|
-
cacheTimeout: 100,
|
|
40
|
-
onResolve: (result) => {
|
|
41
|
-
if (handleChange)
|
|
42
|
-
void handleChange();
|
|
43
|
-
void fastAdapter.save(result.items || [], {
|
|
44
|
-
added: result.changes?.added || [],
|
|
45
|
-
modified: result.changes?.modified || [],
|
|
46
|
-
removed: result.changes?.removed || []
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
return createPersistenceAdapter({
|
|
51
|
-
async register(onChange) {
|
|
52
|
-
handleChange = onChange;
|
|
53
|
-
await Promise.all([slowAdapter.register(onChange), fastAdapter.register(onChange)]);
|
|
54
|
-
},
|
|
55
|
-
async load() {
|
|
56
|
-
const promise = readExecutor();
|
|
57
|
-
return promise;
|
|
58
|
-
},
|
|
59
|
-
async save(items, changes) {
|
|
60
|
-
await Promise.all([
|
|
61
|
-
fastAdapter.save(items, changes),
|
|
62
|
-
slowAdapter.save(items, changes)
|
|
63
|
-
]);
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
exports.createTemporaryFallbackExecutor = createTemporaryFallbackExecutor;
|
|
68
|
-
exports.default = combinePersistenceAdapters;
|
package/dist/index.cjs6.js
DELETED
package/dist/index.cjs7.js
DELETED
package/dist/index.cjs8.js
DELETED