@signaldb/core 2.0.0-beta.13 → 2.0.0-beta.15
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 +72 -46
- package/dist/AsyncDataAdapter.d.ts +11 -2
- package/dist/Collection/Cursor.d.ts +11 -1
- package/dist/Collection/Observer.d.ts +31 -0
- package/dist/Collection/index.d.ts +40 -0
- package/dist/DataAdapter.d.ts +15 -2
- package/dist/WorkerDataAdapter.d.ts +25 -2
- package/dist/WorkerDataAdapterHost.d.ts +2 -0
- package/dist/index.cjs.js +15 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +15 -15
- package/dist/index10.cjs.js +7 -19
- package/dist/index10.mjs +7 -19
- package/dist/index11.cjs.js +19 -41
- package/dist/index11.mjs +19 -41
- package/dist/index12.cjs.js +41 -18
- package/dist/index12.mjs +41 -18
- package/dist/index13.cjs.js +18 -41
- package/dist/index13.mjs +18 -41
- package/dist/index14.cjs.js +39 -80
- package/dist/index14.mjs +39 -80
- package/dist/index15.cjs.js +82 -11
- package/dist/index15.mjs +82 -11
- package/dist/index16.cjs.js +11 -132
- package/dist/index16.mjs +11 -132
- package/dist/index17.cjs.js +127 -38
- package/dist/index17.mjs +127 -38
- package/dist/index18.cjs.js +43 -11
- package/dist/index18.mjs +43 -11
- package/dist/index19.cjs.js +10 -18
- package/dist/index19.mjs +11 -19
- package/dist/index20.cjs.js +19 -33
- package/dist/index20.mjs +19 -33
- package/dist/index21.cjs.js +33 -31
- package/dist/index21.mjs +33 -31
- package/dist/index22.cjs.js +31 -21
- package/dist/index22.mjs +31 -21
- package/dist/index23.cjs.js +16 -14
- package/dist/index23.mjs +16 -14
- package/dist/index24.cjs.js +15 -338
- package/dist/index24.mjs +15 -338
- package/dist/index25.cjs.js +122 -554
- package/dist/index25.mjs +121 -554
- package/dist/index26.cjs.js +34 -7
- package/dist/index26.mjs +34 -7
- package/dist/index27.cjs.js +339 -7
- package/dist/index27.mjs +339 -7
- package/dist/index28.cjs.js +595 -83
- package/dist/index28.mjs +595 -82
- package/dist/index29.cjs.js +8 -422
- package/dist/index29.mjs +8 -422
- package/dist/index30.cjs.js +7 -68
- package/dist/index30.mjs +7 -68
- package/dist/index31.cjs.js +86 -28
- package/dist/index31.mjs +85 -28
- package/dist/index32.cjs.js +446 -278
- package/dist/index32.mjs +446 -278
- package/dist/index33.cjs.js +68 -17
- package/dist/index33.mjs +68 -17
- package/dist/index34.cjs.js +460 -304
- package/dist/index34.mjs +460 -304
- package/dist/index35.cjs.js +14 -532
- package/dist/index35.mjs +14 -532
- package/dist/index36.cjs.js +389 -0
- package/dist/index36.mjs +389 -0
- package/dist/index37.cjs.js +539 -0
- package/dist/index37.mjs +539 -0
- package/dist/index4.cjs.js +199 -140
- package/dist/index4.mjs +195 -140
- package/dist/index5.cjs.js +152 -253
- package/dist/index5.mjs +152 -253
- package/dist/index6.cjs.js +267 -89
- package/dist/index6.mjs +267 -89
- package/dist/index7.cjs.js +121 -29
- package/dist/index7.mjs +121 -29
- package/dist/index8.cjs.js +29 -8
- package/dist/index8.mjs +29 -8
- package/dist/index9.cjs.js +8 -7
- package/dist/index9.mjs +8 -7
- package/dist/utils/incrementalQueryUpdate.d.ts +60 -0
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/package.json +1 -1
package/dist/index6.cjs.js
CHANGED
|
@@ -1,125 +1,303 @@
|
|
|
1
|
-
|
|
1
|
+
const require_Observer = require("./index5.cjs.js");
|
|
2
|
+
//#region src/Collection/Cursor.ts
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* Checks if the current scope is reactive, considering the provided reactivity adapter.
|
|
5
|
+
* @param reactivity - The reactivity adapter or a boolean indicating whether reactivity is enabled.
|
|
6
|
+
* @returns A boolean indicating if the current scope is reactive.
|
|
4
7
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
function isInReactiveScope(reactivity) {
|
|
9
|
+
if (!reactivity) return false;
|
|
10
|
+
if (!reactivity.isInScope) return true;
|
|
11
|
+
return reactivity.isInScope();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Represents a cursor for querying and observing a filtered, sorted, and transformed
|
|
15
|
+
* subset of items from a collection. Supports reactivity and field tracking.
|
|
16
|
+
* @template T - The type of the items in the collection.
|
|
17
|
+
* @template U - The transformed item type after applying transform (default is T).
|
|
18
|
+
*/
|
|
19
|
+
var Cursor = class {
|
|
20
|
+
observer;
|
|
21
|
+
getItems;
|
|
22
|
+
options;
|
|
23
|
+
onCleanupCallbacks = [];
|
|
7
24
|
/**
|
|
8
|
-
*
|
|
25
|
+
* Creates a new instance of the `Cursor` class.
|
|
26
|
+
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
27
|
+
* @template T - The type of the items in the collection.
|
|
28
|
+
* @template U - The transformed item type after applying transformations (default is T).
|
|
29
|
+
* @param getItems - A function that retrieves the filtered list of items.
|
|
30
|
+
* @param options - Optional configuration for the cursor.
|
|
31
|
+
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
32
|
+
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
33
|
+
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
34
|
+
* @param options.sort - A sort specifier to determine the order of the items.
|
|
35
|
+
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
36
|
+
* @param options.limit - The maximum number of items to return in the result set.
|
|
37
|
+
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
38
|
+
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
39
|
+
* @param options.transformAll - A function that will be able to solve the n+1 problem
|
|
9
40
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
|
|
41
|
+
constructor(getItems, options) {
|
|
42
|
+
this.getItems = getItems;
|
|
43
|
+
this.options = options || {};
|
|
44
|
+
}
|
|
45
|
+
addGetters(item) {
|
|
46
|
+
if (!isInReactiveScope(this.options.reactive)) return item;
|
|
47
|
+
const depend = this.depend.bind(this);
|
|
48
|
+
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
49
|
+
Object.defineProperty(memo, key, {
|
|
50
|
+
get() {
|
|
51
|
+
depend({ changedField: (notify) => (changedItem, changedFieldName) => {
|
|
52
|
+
if (changedFieldName !== key || changedItem.id !== item.id) return;
|
|
53
|
+
notify();
|
|
54
|
+
} });
|
|
55
|
+
return value;
|
|
56
|
+
},
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true
|
|
59
|
+
});
|
|
60
|
+
return memo;
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
transform(rawItem) {
|
|
64
|
+
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
65
|
+
if (!this.options.transform) return item;
|
|
66
|
+
return this.options.transform(item);
|
|
67
|
+
}
|
|
68
|
+
depend(changeEvents, bindExtraNotifier) {
|
|
69
|
+
if (this.options?.async) return;
|
|
70
|
+
if (!isInReactiveScope(this.options.reactive)) console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
|
|
71
|
+
if (!this.options.reactive) return;
|
|
72
|
+
const signal = this.options.reactive.create();
|
|
73
|
+
signal.depend();
|
|
74
|
+
const notify = () => signal.notify();
|
|
75
|
+
/**
|
|
76
|
+
* Builds a notifier function for the specified event.
|
|
77
|
+
* @template Event - The type of the event.
|
|
78
|
+
* @param event - The event for which to build the notifier.
|
|
79
|
+
* @returns A function that handles the event and triggers the appropriate notifications.
|
|
80
|
+
*/
|
|
81
|
+
function buildNotifier(event) {
|
|
82
|
+
const eventHandler = changeEvents[event];
|
|
83
|
+
return (...args) => {
|
|
84
|
+
if (eventHandler === true) {
|
|
85
|
+
notify();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (typeof eventHandler !== "function") return;
|
|
89
|
+
eventHandler(notify)(...args);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const stop = this.observeRawChanges({
|
|
93
|
+
added: buildNotifier("added"),
|
|
94
|
+
addedBefore: buildNotifier("addedBefore"),
|
|
95
|
+
changed: buildNotifier("changed"),
|
|
96
|
+
changedField: buildNotifier("changedField"),
|
|
97
|
+
movedBefore: buildNotifier("movedBefore"),
|
|
98
|
+
removed: buildNotifier("removed")
|
|
99
|
+
}, true);
|
|
100
|
+
if (this.options.reactive.onDispose) this.options.reactive.onDispose(() => stop(), signal);
|
|
101
|
+
this.onCleanup(stop);
|
|
102
|
+
if (!bindExtraNotifier) return;
|
|
103
|
+
const stopExtraNotifier = bindExtraNotifier(notify);
|
|
104
|
+
if (this.options.reactive.onDispose) this.options.reactive.onDispose(() => stopExtraNotifier(), signal);
|
|
105
|
+
this.onCleanup(stopExtraNotifier);
|
|
106
|
+
}
|
|
107
|
+
ensureObserver() {
|
|
108
|
+
if (!this.observer) {
|
|
109
|
+
const observer = new require_Observer.default(() => {
|
|
110
|
+
const requery = () => {
|
|
111
|
+
observer.runChecks(this.getItems);
|
|
112
|
+
};
|
|
113
|
+
const applyDelta = (delta) => {
|
|
114
|
+
observer.applyDelta(delta, this.getItems);
|
|
115
|
+
};
|
|
116
|
+
const cleanup = this.options.bindEvents && this.options.bindEvents(requery, applyDelta);
|
|
117
|
+
return () => {
|
|
118
|
+
if (cleanup) cleanup();
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
this.onCleanup(() => observer.stop());
|
|
122
|
+
this.observer = observer;
|
|
123
|
+
}
|
|
124
|
+
return this.observer;
|
|
125
|
+
}
|
|
126
|
+
observeRawChanges(callbacks, skipInitial = false) {
|
|
127
|
+
const observer = this.ensureObserver();
|
|
128
|
+
observer.addCallbacks(callbacks, skipInitial);
|
|
129
|
+
observer.runChecks(this.getItems);
|
|
130
|
+
return () => {
|
|
131
|
+
observer.removeCallbacks(callbacks);
|
|
132
|
+
if (!observer.isEmpty()) return;
|
|
133
|
+
observer.stop();
|
|
134
|
+
this.observer = void 0;
|
|
135
|
+
};
|
|
14
136
|
}
|
|
15
137
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @returns The emitter instance (for chaining).
|
|
138
|
+
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
139
|
+
* and event listeners. This method should be called when the cursor is no longer needed
|
|
140
|
+
* to prevent memory leaks.
|
|
20
141
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
listenersSet.add(listener);
|
|
28
|
-
if (listenersSet.size > this._maxListeners) console.warn(`Possible EventEmitter memory leak detected. ${listenersSet.size} ${String(eventName)} listeners added. Use emitter.setMaxListeners() to increase limit.`);
|
|
29
|
-
return this;
|
|
142
|
+
cleanup() {
|
|
143
|
+
this.onCleanupCallbacks.forEach((callback) => {
|
|
144
|
+
callback();
|
|
145
|
+
});
|
|
146
|
+
this.onCleanupCallbacks = [];
|
|
30
147
|
}
|
|
31
148
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @param
|
|
35
|
-
* @returns The emitter instance (for chaining).
|
|
149
|
+
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
150
|
+
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
151
|
+
* @param callback - A function to be executed during cleanup.
|
|
36
152
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
153
|
+
onCleanup(callback) {
|
|
154
|
+
this.onCleanupCallbacks.push(callback);
|
|
39
155
|
}
|
|
40
156
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @param
|
|
45
|
-
* @
|
|
157
|
+
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
158
|
+
* function to each transformed item.
|
|
159
|
+
* ⚡️ this function is reactive!
|
|
160
|
+
* @param callback - A function to execute for each item in the result set.
|
|
161
|
+
* @param callback.item - The transformed item.
|
|
162
|
+
* @returns A promise that resolves when all items have been processed, or void if not in async mode.
|
|
46
163
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
164
|
+
forEach(callback) {
|
|
165
|
+
this.depend({
|
|
166
|
+
addedBefore: true,
|
|
167
|
+
removed: true,
|
|
168
|
+
movedBefore: true,
|
|
169
|
+
...this.options.fieldTracking ? {} : { changed: true }
|
|
51
170
|
});
|
|
52
|
-
|
|
171
|
+
const executeForEach = (items) => {
|
|
172
|
+
items.forEach((item) => {
|
|
173
|
+
callback(this.transform(item));
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
const result = this.getItems();
|
|
177
|
+
if (result instanceof Promise) return result.then(executeForEach);
|
|
178
|
+
else {
|
|
179
|
+
executeForEach(result);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
53
182
|
}
|
|
54
183
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* @
|
|
184
|
+
* Creates a new array populated with the results of applying the provided callback
|
|
185
|
+
* function to each transformed item in the cursor's result set.
|
|
186
|
+
* ⚡️ this function is reactive!
|
|
187
|
+
* @template V - The type of the items in the resulting array.
|
|
188
|
+
* @param callback - A function to execute for each item in the result set.
|
|
189
|
+
* @param callback.item - The transformed item.
|
|
190
|
+
* @returns An array of results after applying the callback to each item.
|
|
59
191
|
*/
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return
|
|
192
|
+
map(callback) {
|
|
193
|
+
const results = [];
|
|
194
|
+
const maybePromise = this.forEach((item) => {
|
|
195
|
+
results.push(callback(item));
|
|
196
|
+
});
|
|
197
|
+
if (maybePromise instanceof Promise) return maybePromise.then(() => results);
|
|
198
|
+
return results;
|
|
66
199
|
}
|
|
67
200
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @returns
|
|
201
|
+
* Fetches all transformed items from the cursor's result set as an array.
|
|
202
|
+
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
203
|
+
* ⚡️ this function is reactive!
|
|
204
|
+
* @returns An array of transformed items in the result set.
|
|
72
205
|
*/
|
|
73
|
-
|
|
74
|
-
return this.
|
|
206
|
+
fetch() {
|
|
207
|
+
return this.map((item) => item);
|
|
75
208
|
}
|
|
76
209
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
210
|
+
* Counts the total number of items in the cursor's result set after applying
|
|
211
|
+
* filtering and other criteria.
|
|
212
|
+
* ⚡️ this function is reactive!
|
|
213
|
+
* @returns The total number of items in the result set.
|
|
80
214
|
*/
|
|
81
|
-
|
|
82
|
-
this.
|
|
83
|
-
|
|
215
|
+
count() {
|
|
216
|
+
this.depend({
|
|
217
|
+
added: true,
|
|
218
|
+
removed: true
|
|
84
219
|
});
|
|
220
|
+
const maybePromise = this.getItems();
|
|
221
|
+
return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
|
|
85
222
|
}
|
|
86
223
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
224
|
+
* Whether this cursor's query has yet to deliver a first result.
|
|
225
|
+
* ⚡️ this function is reactive!
|
|
226
|
+
*
|
|
227
|
+
* An asynchronous data adapter answers a newly registered query only after a
|
|
228
|
+
* round trip, and serves a neutral empty result until it does — which a
|
|
229
|
+
* consumer cannot otherwise tell apart from "there is nothing to show". This
|
|
230
|
+
* is that distinction, per query rather than per collection, so one screen
|
|
231
|
+
* waiting on its own data says nothing about any other query.
|
|
232
|
+
*
|
|
233
|
+
* Follows the usual `isLoading`/`isFetching` split: it reports "no result
|
|
234
|
+
* yet", not "an execution is in flight". A write that re-runs an
|
|
235
|
+
* already-settled query drives it through `'active'` again while this stays
|
|
236
|
+
* `false`, so a list does not fall back to a loading state every time one of
|
|
237
|
+
* its rows changes.
|
|
238
|
+
*
|
|
239
|
+
* A query that fails counts as settled — the `query.error` event on the
|
|
240
|
+
* collection is what surfaces the failure, and a loading state that never
|
|
241
|
+
* ends is the worse answer. Reading this registers the query if nothing else
|
|
242
|
+
* has, so it cannot wait on something nobody asked for. It is always `false`
|
|
243
|
+
* for an `{ async: true }` cursor, whose `fetch()` awaits the real result
|
|
244
|
+
* anyway, and for a data adapter that answers synchronously.
|
|
245
|
+
* @returns A boolean indicating whether the first result is still pending.
|
|
90
246
|
*/
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
if (!
|
|
94
|
-
|
|
247
|
+
isLoading() {
|
|
248
|
+
const queryState = this.options.queryState;
|
|
249
|
+
if (!queryState || this.options.async) return false;
|
|
250
|
+
this.depend({}, (notify) => queryState.onSettled(notify));
|
|
251
|
+
return !queryState.hasSettled();
|
|
95
252
|
}
|
|
96
253
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* @
|
|
254
|
+
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
255
|
+
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
256
|
+
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
257
|
+
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
258
|
+
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
259
|
+
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
260
|
+
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
261
|
+
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
262
|
+
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
263
|
+
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
264
|
+
* @returns A function to stop observing changes.
|
|
100
265
|
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
266
|
+
observeChanges(callbacks, skipInitial = false) {
|
|
267
|
+
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
268
|
+
if (!callback) return memo;
|
|
269
|
+
return {
|
|
270
|
+
...memo,
|
|
271
|
+
[callbackName]: (item, before) => {
|
|
272
|
+
const transformedValue = this.transform(item);
|
|
273
|
+
const hasBeforeParameter = before !== void 0;
|
|
274
|
+
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
275
|
+
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}, {}), skipInitial);
|
|
104
279
|
}
|
|
105
280
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
281
|
+
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
282
|
+
* from the collection. This is useful when the underlying data or query
|
|
283
|
+
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
109
284
|
*/
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
285
|
+
requery() {
|
|
286
|
+
if (!this.observer) return;
|
|
287
|
+
this.observer.runChecks(this.getItems);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Brings the cursor up to date from a description of what changed, rather than by re-running the
|
|
291
|
+
* query and comparing the result with the previous one.
|
|
292
|
+
*
|
|
293
|
+
* Falls back to `requery` when the delta does not fit the result the cursor currently holds, so
|
|
294
|
+
* a caller never has to decide which of the two is safe.
|
|
295
|
+
* @param delta - The change to apply.
|
|
296
|
+
*/
|
|
297
|
+
applyDelta(delta) {
|
|
298
|
+
if (!this.observer) return;
|
|
299
|
+
this.observer.applyDelta(delta, this.getItems);
|
|
122
300
|
}
|
|
123
301
|
};
|
|
124
302
|
//#endregion
|
|
125
|
-
exports.default =
|
|
303
|
+
exports.default = Cursor;
|