@signaldb/core 2.0.0-beta.2 → 2.0.0-beta.21
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 +183 -80
- package/dist/AsyncDataAdapter.d.ts +53 -7
- package/dist/AutoFetchDataAdapter.d.ts +4 -6
- package/dist/Collection/Cursor.d.ts +56 -4
- package/dist/Collection/Observer.d.ts +33 -0
- package/dist/Collection/index.d.ts +66 -19
- package/dist/Collection/types.d.ts +9 -2
- package/dist/DataAdapter.d.ts +24 -6
- package/dist/DefaultDataAdapter.d.ts +5 -5
- package/dist/WorkerDataAdapter.d.ts +57 -5
- package/dist/WorkerDataAdapterHost.d.ts +9 -7
- package/dist/createIndex.d.ts +2 -2
- package/dist/createIndexProvider.d.ts +2 -2
- package/dist/createReactivityAdapter.d.ts +2 -2
- package/dist/createStorageAdapter.d.ts +1 -1
- package/dist/getIndexInfo.d.ts +3 -4
- package/dist/index.cjs.js +33 -31
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +17 -32
- package/dist/index10.cjs.js +11 -0
- package/dist/index10.mjs +10 -18
- package/dist/index11.cjs.js +23 -0
- package/dist/index11.mjs +22 -15
- package/dist/index12.cjs.js +45 -0
- package/dist/index12.mjs +44 -353
- package/dist/index13.cjs.js +22 -0
- package/dist/index13.mjs +21 -419
- package/dist/index14.cjs.js +45 -0
- package/dist/index14.mjs +44 -206
- package/dist/index15.cjs.js +86 -0
- package/dist/index15.mjs +85 -388
- package/dist/index16.cjs.js +15 -0
- package/dist/index16.mjs +14 -579
- package/dist/index17.cjs.js +141 -0
- package/dist/index17.mjs +140 -152
- package/dist/index18.cjs.js +47 -0
- package/dist/index18.mjs +46 -26
- package/dist/index19.cjs.js +15 -0
- package/dist/index19.mjs +14 -41
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +23 -0
- package/dist/index20.mjs +22 -16
- package/dist/index21.cjs.js +37 -0
- package/dist/index21.mjs +36 -104
- package/dist/index22.cjs.js +35 -0
- package/dist/index22.mjs +34 -128
- package/dist/index23.cjs.js +21 -0
- package/dist/index23.mjs +20 -7
- package/dist/index24.cjs.js +19 -0
- package/dist/index24.mjs +18 -27
- package/dist/index25.cjs.js +157 -0
- package/dist/index25.mjs +156 -11
- package/dist/index26.cjs.js +38 -0
- package/dist/index26.mjs +37 -29
- package/dist/index27.cjs.js +42 -0
- package/dist/index27.mjs +41 -46
- package/dist/index28.cjs.js +347 -0
- package/dist/index28.mjs +345 -7
- package/dist/index29.cjs.js +602 -0
- package/dist/index29.mjs +601 -7
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -556
- package/dist/index30.cjs.js +11 -0
- package/dist/index30.mjs +10 -42
- package/dist/index31.cjs.js +11 -0
- package/dist/index31.mjs +10 -29
- package/dist/index32.cjs.js +90 -0
- package/dist/index32.mjs +89 -10
- package/dist/index33.cjs.js +53 -0
- package/dist/index33.mjs +52 -5
- package/dist/index34.cjs.js +453 -0
- package/dist/index34.mjs +453 -0
- package/dist/index35.cjs.js +72 -0
- package/dist/index35.mjs +72 -0
- package/dist/index36.cjs.js +580 -0
- package/dist/index36.mjs +580 -0
- package/dist/index37.cjs.js +21 -0
- package/dist/index37.mjs +21 -0
- package/dist/index38.cjs.js +363 -0
- package/dist/index38.mjs +363 -0
- package/dist/index39.cjs.js +513 -0
- package/dist/index39.mjs +513 -0
- package/dist/index4.cjs.js +209 -0
- package/dist/index4.mjs +204 -5
- package/dist/index5.cjs.js +187 -0
- package/dist/index5.mjs +186 -5
- package/dist/index6.cjs.js +303 -0
- package/dist/index6.mjs +301 -28
- package/dist/index7.cjs.js +125 -0
- package/dist/index7.mjs +124 -12
- package/dist/index8.cjs.js +33 -0
- package/dist/index8.mjs +32 -5
- package/dist/index9.cjs.js +12 -0
- package/dist/index9.mjs +11 -133
- package/dist/types/IndexProvider.d.ts +2 -2
- package/dist/types/Modifier.d.ts +1 -1
- package/dist/types/ReactivityAdapter.d.ts +1 -1
- package/dist/types/StorageAdapter.d.ts +9 -1
- package/dist/utils/applyQueryOptions.d.ts +15 -0
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/idIndexQuery.d.ts +20 -0
- package/dist/utils/incrementalQueryUpdate.d.ts +62 -0
- package/dist/utils/isFieldExpression.d.ts +1 -1
- package/dist/utils/match.d.ts +1 -1
- package/dist/utils/modify.d.ts +1 -1
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/dist/utils/queryId.d.ts +2 -2
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/dist/utils/storageIndexQuery.d.ts +20 -0
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -353
- package/dist/index.cjs13.js +0 -419
- package/dist/index.cjs14.js +0 -206
- package/dist/index.cjs15.js +0 -388
- package/dist/index.cjs16.js +0 -579
- package/dist/index.cjs17.js +0 -152
- package/dist/index.cjs18.js +0 -26
- package/dist/index.cjs19.js +0 -42
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -16
- package/dist/index.cjs21.js +0 -104
- package/dist/index.cjs22.js +0 -129
- package/dist/index.cjs23.js +0 -7
- package/dist/index.cjs24.js +0 -27
- package/dist/index.cjs25.js +0 -10
- package/dist/index.cjs26.js +0 -29
- package/dist/index.cjs27.js +0 -46
- package/dist/index.cjs28.js +0 -8
- package/dist/index.cjs29.js +0 -7
- package/dist/index.cjs3.js +0 -557
- package/dist/index.cjs30.js +0 -42
- package/dist/index.cjs31.js +0 -29
- package/dist/index.cjs32.js +0 -9
- package/dist/index.cjs33.js +0 -5
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
package/dist/index.cjs17.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const isEqual = require("./index.cjs6.js");
|
|
3
|
-
const uniqueBy = require("./index.cjs32.js");
|
|
4
|
-
class Observer {
|
|
5
|
-
previousItems = [];
|
|
6
|
-
callbacks;
|
|
7
|
-
unbindEvents;
|
|
8
|
-
/**
|
|
9
|
-
* Creates a new instance of the `Observer` class.
|
|
10
|
-
* Sets up event bindings and initializes the callbacks for tracking changes in a collection.
|
|
11
|
-
* @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
|
|
12
|
-
*/
|
|
13
|
-
constructor(bindEvents) {
|
|
14
|
-
this.callbacks = {
|
|
15
|
-
added: [],
|
|
16
|
-
addedBefore: [],
|
|
17
|
-
changed: [],
|
|
18
|
-
changedField: [],
|
|
19
|
-
movedBefore: [],
|
|
20
|
-
removed: []
|
|
21
|
-
};
|
|
22
|
-
this.unbindEvents = bindEvents();
|
|
23
|
-
}
|
|
24
|
-
call(event, ...args) {
|
|
25
|
-
this.callbacks[event].forEach(({ callback, options }) => {
|
|
26
|
-
if (!options.skipInitial || !options.isInitial) {
|
|
27
|
-
callback(...args);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
hasCallbacks(events) {
|
|
32
|
-
return events.some((event) => this.callbacks[event].length > 0);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Determines if the observer has no active callbacks registered for any events.
|
|
36
|
-
* @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
|
|
37
|
-
*/
|
|
38
|
-
isEmpty() {
|
|
39
|
-
return !this.hasCallbacks([
|
|
40
|
-
"added",
|
|
41
|
-
"addedBefore",
|
|
42
|
-
"changed",
|
|
43
|
-
"changedField",
|
|
44
|
-
"movedBefore",
|
|
45
|
-
"removed"
|
|
46
|
-
]);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
50
|
-
* for events such as added, removed, changed, or moved items.
|
|
51
|
-
* @param getItems - A function that returns a promise resolving to the new items or the items themselves.
|
|
52
|
-
*/
|
|
53
|
-
runChecks(getItems) {
|
|
54
|
-
const result = getItems();
|
|
55
|
-
if (result instanceof Promise) {
|
|
56
|
-
result.then((newItems) => this.checkItems(newItems)).catch((error) => {
|
|
57
|
-
console.error("Error while asynchronously querying items", error);
|
|
58
|
-
});
|
|
59
|
-
} else {
|
|
60
|
-
this.checkItems(result);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
checkItems(newItems) {
|
|
64
|
-
const oldItemsMap = new Map(this.previousItems.map((item, index) => [
|
|
65
|
-
item.id,
|
|
66
|
-
{ item, index, beforeItem: this.previousItems[index + 1] || null }
|
|
67
|
-
]));
|
|
68
|
-
const newItemsMap = new Map(newItems.map((item, index) => [
|
|
69
|
-
item.id,
|
|
70
|
-
{ item, index, beforeItem: newItems[index + 1] || null }
|
|
71
|
-
]));
|
|
72
|
-
if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
|
|
73
|
-
oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
|
|
74
|
-
const newItem = newItemsMap.get(oldItem.id);
|
|
75
|
-
if (newItem) {
|
|
76
|
-
if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
|
|
77
|
-
this.call("changed", newItem.item);
|
|
78
|
-
if (this.hasCallbacks(["changedField"])) {
|
|
79
|
-
const keys = uniqueBy([
|
|
80
|
-
...Object.keys(newItem.item),
|
|
81
|
-
...Object.keys(oldItem)
|
|
82
|
-
], (value) => value);
|
|
83
|
-
keys.forEach((key) => {
|
|
84
|
-
if (isEqual(newItem.item[key], oldItem[key]))
|
|
85
|
-
return;
|
|
86
|
-
this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
|
|
91
|
-
this.call("movedBefore", newItem.item, newItem.beforeItem);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
this.call("removed", oldItem);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
if (this.hasCallbacks(["added", "addedBefore"])) {
|
|
99
|
-
newItems.forEach((newItem, index) => {
|
|
100
|
-
const oldItem = oldItemsMap.get(newItem.id);
|
|
101
|
-
if (oldItem)
|
|
102
|
-
return;
|
|
103
|
-
this.call("added", newItem);
|
|
104
|
-
this.call("addedBefore", newItem, newItems[index + 1] || null);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
this.previousItems = newItems;
|
|
108
|
-
Object.keys(this.callbacks).forEach((key) => {
|
|
109
|
-
const event = key;
|
|
110
|
-
const callbacks = this.callbacks[event];
|
|
111
|
-
this.callbacks[event] = callbacks.map((callback) => ({
|
|
112
|
-
...callback,
|
|
113
|
-
options: {
|
|
114
|
-
...callback.options,
|
|
115
|
-
isInitial: false
|
|
116
|
-
}
|
|
117
|
-
}));
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Stops the observer by unbinding all events and cleaning up resources.
|
|
122
|
-
*/
|
|
123
|
-
stop() {
|
|
124
|
-
this.unbindEvents();
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Registers callbacks for specific events to observe changes in the collection.
|
|
128
|
-
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
129
|
-
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
130
|
-
*/
|
|
131
|
-
addCallbacks(callbacks, skipInitial = false) {
|
|
132
|
-
Object.keys(callbacks).forEach((key) => {
|
|
133
|
-
const typedKey = key;
|
|
134
|
-
this.callbacks[typedKey].push({
|
|
135
|
-
callback: callbacks[typedKey],
|
|
136
|
-
options: { skipInitial, isInitial: true }
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
142
|
-
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
143
|
-
*/
|
|
144
|
-
removeCallbacks(callbacks) {
|
|
145
|
-
Object.keys(callbacks).forEach((key) => {
|
|
146
|
-
const typedKey = key;
|
|
147
|
-
const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
|
|
148
|
-
this.callbacks[typedKey].splice(index, 1);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
module.exports = Observer;
|
package/dist/index.cjs18.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function createSignal(reactivityAdapter, initialValue, isEqual = Object.is) {
|
|
3
|
-
let value = initialValue;
|
|
4
|
-
const dependency = reactivityAdapter?.create();
|
|
5
|
-
const isInReactiveScope = () => {
|
|
6
|
-
if (!reactivityAdapter?.isInScope)
|
|
7
|
-
return true;
|
|
8
|
-
return reactivityAdapter.isInScope();
|
|
9
|
-
};
|
|
10
|
-
const signal = {
|
|
11
|
-
get() {
|
|
12
|
-
if (dependency && isInReactiveScope())
|
|
13
|
-
dependency.depend();
|
|
14
|
-
return value;
|
|
15
|
-
},
|
|
16
|
-
set(newValue) {
|
|
17
|
-
if (isEqual(value, newValue))
|
|
18
|
-
return;
|
|
19
|
-
value = newValue;
|
|
20
|
-
if (dependency)
|
|
21
|
-
dependency.notify();
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
return signal;
|
|
25
|
-
}
|
|
26
|
-
module.exports = createSignal;
|
package/dist/index.cjs19.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
function clone(value) {
|
|
4
|
-
if (typeof value === "function")
|
|
5
|
-
throw new Error("Cloning functions is not supported");
|
|
6
|
-
if (value === null || typeof value !== "object")
|
|
7
|
-
return value;
|
|
8
|
-
if (value instanceof Date)
|
|
9
|
-
return new Date(value);
|
|
10
|
-
if (Array.isArray(value))
|
|
11
|
-
return value.map((item) => clone(item));
|
|
12
|
-
if (value instanceof Map) {
|
|
13
|
-
const result2 = /* @__PURE__ */ new Map();
|
|
14
|
-
value.forEach((currentValue, key) => {
|
|
15
|
-
result2.set(key, clone(currentValue));
|
|
16
|
-
});
|
|
17
|
-
return result2;
|
|
18
|
-
}
|
|
19
|
-
if (value instanceof Set) {
|
|
20
|
-
const result2 = /* @__PURE__ */ new Set();
|
|
21
|
-
value.forEach((currentValue) => {
|
|
22
|
-
result2.add(clone(currentValue));
|
|
23
|
-
});
|
|
24
|
-
return result2;
|
|
25
|
-
}
|
|
26
|
-
if (value instanceof RegExp)
|
|
27
|
-
return new RegExp(value);
|
|
28
|
-
const result = {};
|
|
29
|
-
for (const key in value) {
|
|
30
|
-
if (Object.hasOwnProperty.call(value, key)) {
|
|
31
|
-
result[key] = clone(value[key]);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
function deepClone(object) {
|
|
37
|
-
if (typeof structuredClone === "function")
|
|
38
|
-
return structuredClone(object);
|
|
39
|
-
return clone(object);
|
|
40
|
-
}
|
|
41
|
-
exports.clone = clone;
|
|
42
|
-
exports.default = deepClone;
|
package/dist/index.cjs2.js
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const Observer = require("./index.cjs17.js");
|
|
4
|
-
function isInReactiveScope(reactivity) {
|
|
5
|
-
if (!reactivity)
|
|
6
|
-
return false;
|
|
7
|
-
if (!reactivity.isInScope)
|
|
8
|
-
return true;
|
|
9
|
-
return reactivity.isInScope();
|
|
10
|
-
}
|
|
11
|
-
class Cursor {
|
|
12
|
-
observer;
|
|
13
|
-
getItems;
|
|
14
|
-
options;
|
|
15
|
-
onCleanupCallbacks = [];
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new instance of the `Cursor` class.
|
|
18
|
-
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
19
|
-
* @template T - The type of the items in the collection.
|
|
20
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
21
|
-
* @param getItems - A function that retrieves the filtered list of items.
|
|
22
|
-
* @param options - Optional configuration for the cursor.
|
|
23
|
-
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
24
|
-
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
25
|
-
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
26
|
-
* @param options.sort - A sort specifier to determine the order of the items.
|
|
27
|
-
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
28
|
-
* @param options.limit - The maximum number of items to return in the result set.
|
|
29
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
30
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
31
|
-
* @param options.transformAll - A function that will be able to solve the n+1 problem
|
|
32
|
-
*/
|
|
33
|
-
constructor(getItems, options) {
|
|
34
|
-
this.getItems = getItems;
|
|
35
|
-
this.options = options || {};
|
|
36
|
-
}
|
|
37
|
-
addGetters(item) {
|
|
38
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
39
|
-
return item;
|
|
40
|
-
const depend = this.depend.bind(this);
|
|
41
|
-
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
42
|
-
Object.defineProperty(memo, key, {
|
|
43
|
-
get() {
|
|
44
|
-
depend({
|
|
45
|
-
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
46
|
-
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
47
|
-
return;
|
|
48
|
-
notify();
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
return value;
|
|
52
|
-
},
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true
|
|
55
|
-
});
|
|
56
|
-
return memo;
|
|
57
|
-
}, {});
|
|
58
|
-
}
|
|
59
|
-
transform(rawItem) {
|
|
60
|
-
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
61
|
-
if (!this.options.transform)
|
|
62
|
-
return item;
|
|
63
|
-
return this.options.transform(item);
|
|
64
|
-
}
|
|
65
|
-
depend(changeEvents) {
|
|
66
|
-
if (this.options?.async)
|
|
67
|
-
return;
|
|
68
|
-
if (!isInReactiveScope(this.options.reactive)) {
|
|
69
|
-
console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
|
|
70
|
-
}
|
|
71
|
-
if (!this.options.reactive)
|
|
72
|
-
return;
|
|
73
|
-
const signal = this.options.reactive.create();
|
|
74
|
-
signal.depend();
|
|
75
|
-
const notify = () => signal.notify();
|
|
76
|
-
function buildNotifier(event) {
|
|
77
|
-
const eventHandler = changeEvents[event];
|
|
78
|
-
return (...args) => {
|
|
79
|
-
if (eventHandler === true) {
|
|
80
|
-
notify();
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (typeof eventHandler !== "function")
|
|
84
|
-
return;
|
|
85
|
-
eventHandler(notify)(...args);
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
const stop = this.observeRawChanges({
|
|
89
|
-
added: buildNotifier("added"),
|
|
90
|
-
addedBefore: buildNotifier("addedBefore"),
|
|
91
|
-
changed: buildNotifier("changed"),
|
|
92
|
-
changedField: buildNotifier("changedField"),
|
|
93
|
-
movedBefore: buildNotifier("movedBefore"),
|
|
94
|
-
removed: buildNotifier("removed")
|
|
95
|
-
}, true);
|
|
96
|
-
if (this.options.reactive.onDispose) {
|
|
97
|
-
this.options.reactive.onDispose(() => stop(), signal);
|
|
98
|
-
}
|
|
99
|
-
this.onCleanup(stop);
|
|
100
|
-
}
|
|
101
|
-
ensureObserver() {
|
|
102
|
-
if (!this.observer) {
|
|
103
|
-
const observer = new Observer(() => {
|
|
104
|
-
const requery = () => {
|
|
105
|
-
observer.runChecks(this.getItems);
|
|
106
|
-
};
|
|
107
|
-
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
108
|
-
return () => {
|
|
109
|
-
if (cleanup)
|
|
110
|
-
cleanup();
|
|
111
|
-
};
|
|
112
|
-
});
|
|
113
|
-
this.onCleanup(() => observer.stop());
|
|
114
|
-
this.observer = observer;
|
|
115
|
-
}
|
|
116
|
-
return this.observer;
|
|
117
|
-
}
|
|
118
|
-
observeRawChanges(callbacks, skipInitial = false) {
|
|
119
|
-
const observer = this.ensureObserver();
|
|
120
|
-
observer.addCallbacks(callbacks, skipInitial);
|
|
121
|
-
observer.runChecks(this.getItems);
|
|
122
|
-
return () => {
|
|
123
|
-
observer.removeCallbacks(callbacks);
|
|
124
|
-
if (!observer.isEmpty())
|
|
125
|
-
return;
|
|
126
|
-
observer.stop();
|
|
127
|
-
this.observer = void 0;
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
132
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
133
|
-
* to prevent memory leaks.
|
|
134
|
-
*/
|
|
135
|
-
cleanup() {
|
|
136
|
-
this.onCleanupCallbacks.forEach((callback) => {
|
|
137
|
-
callback();
|
|
138
|
-
});
|
|
139
|
-
this.onCleanupCallbacks = [];
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
143
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
144
|
-
* @param callback - A function to be executed during cleanup.
|
|
145
|
-
*/
|
|
146
|
-
onCleanup(callback) {
|
|
147
|
-
this.onCleanupCallbacks.push(callback);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
151
|
-
* function to each transformed item.
|
|
152
|
-
* ⚡️ this function is reactive!
|
|
153
|
-
* @param callback - A function to execute for each item in the result set.
|
|
154
|
-
* @param callback.item - The transformed item.
|
|
155
|
-
* @returns A promise that resolves when all items have been processed, or void if not in async mode.
|
|
156
|
-
*/
|
|
157
|
-
forEach(callback) {
|
|
158
|
-
this.depend({
|
|
159
|
-
addedBefore: true,
|
|
160
|
-
removed: true,
|
|
161
|
-
movedBefore: true,
|
|
162
|
-
...this.options.fieldTracking ? {} : { changed: true }
|
|
163
|
-
});
|
|
164
|
-
const executeForEach = (items) => {
|
|
165
|
-
items.forEach((item) => {
|
|
166
|
-
callback(this.transform(item));
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
const result = this.getItems();
|
|
170
|
-
if (result instanceof Promise) {
|
|
171
|
-
return result.then(executeForEach);
|
|
172
|
-
} else {
|
|
173
|
-
executeForEach(result);
|
|
174
|
-
return void 0;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Creates a new array populated with the results of applying the provided callback
|
|
179
|
-
* function to each transformed item in the cursor's result set.
|
|
180
|
-
* ⚡️ this function is reactive!
|
|
181
|
-
* @template V - The type of the items in the resulting array.
|
|
182
|
-
* @param callback - A function to execute for each item in the result set.
|
|
183
|
-
* @param callback.item - The transformed item.
|
|
184
|
-
* @returns An array of results after applying the callback to each item.
|
|
185
|
-
*/
|
|
186
|
-
map(callback) {
|
|
187
|
-
const results = [];
|
|
188
|
-
const maybePromise = this.forEach((item) => {
|
|
189
|
-
results.push(callback(item));
|
|
190
|
-
});
|
|
191
|
-
if (maybePromise instanceof Promise) {
|
|
192
|
-
return maybePromise.then(() => results);
|
|
193
|
-
}
|
|
194
|
-
return results;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
198
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
199
|
-
* ⚡️ this function is reactive!
|
|
200
|
-
* @returns An array of transformed items in the result set.
|
|
201
|
-
*/
|
|
202
|
-
fetch() {
|
|
203
|
-
return this.map((item) => item);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
207
|
-
* filtering and other criteria.
|
|
208
|
-
* ⚡️ this function is reactive!
|
|
209
|
-
* @returns The total number of items in the result set.
|
|
210
|
-
*/
|
|
211
|
-
count() {
|
|
212
|
-
this.depend({
|
|
213
|
-
added: true,
|
|
214
|
-
removed: true
|
|
215
|
-
});
|
|
216
|
-
const maybePromise = this.getItems();
|
|
217
|
-
return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
221
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
222
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
223
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
224
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
225
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
226
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
227
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
228
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
229
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
230
|
-
* @returns A function to stop observing changes.
|
|
231
|
-
*/
|
|
232
|
-
observeChanges(callbacks, skipInitial = false) {
|
|
233
|
-
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
234
|
-
if (!callback)
|
|
235
|
-
return memo;
|
|
236
|
-
return {
|
|
237
|
-
...memo,
|
|
238
|
-
[callbackName]: (item, before) => {
|
|
239
|
-
const transformedValue = this.transform(item);
|
|
240
|
-
const hasBeforeParameter = before !== void 0;
|
|
241
|
-
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
242
|
-
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
}, {}), skipInitial);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
249
|
-
* from the collection. This is useful when the underlying data or query
|
|
250
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
251
|
-
*/
|
|
252
|
-
requery() {
|
|
253
|
-
if (!this.observer)
|
|
254
|
-
return;
|
|
255
|
-
this.observer.runChecks(this.getItems);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
exports.default = Cursor;
|
|
259
|
-
exports.isInReactiveScope = isInReactiveScope;
|
package/dist/index.cjs20.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function isEmptyOptions(options) {
|
|
3
|
-
if (options == null)
|
|
4
|
-
return true;
|
|
5
|
-
if (typeof options !== "object")
|
|
6
|
-
return false;
|
|
7
|
-
if (Array.isArray(options))
|
|
8
|
-
return false;
|
|
9
|
-
return Object.keys(options).length === 0;
|
|
10
|
-
}
|
|
11
|
-
function queryId(selector, options) {
|
|
12
|
-
const selectorId = JSON.stringify(selector);
|
|
13
|
-
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
14
|
-
return `${selectorId}:${optionsId}`;
|
|
15
|
-
}
|
|
16
|
-
module.exports = queryId;
|
package/dist/index.cjs21.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const createIndexProvider = require("./index.cjs33.js");
|
|
3
|
-
const get = require("./index.cjs10.js");
|
|
4
|
-
const getMatchingKeys = require("./index.cjs26.js");
|
|
5
|
-
const serializeValue = require("./index.cjs11.js");
|
|
6
|
-
function createIndex(field) {
|
|
7
|
-
const index = /* @__PURE__ */ new Map();
|
|
8
|
-
const ensureSet = (key) => {
|
|
9
|
-
let set = index.get(key);
|
|
10
|
-
if (!set) {
|
|
11
|
-
set = /* @__PURE__ */ new Set();
|
|
12
|
-
index.set(key, set);
|
|
13
|
-
}
|
|
14
|
-
return set;
|
|
15
|
-
};
|
|
16
|
-
return createIndexProvider({
|
|
17
|
-
query(selector) {
|
|
18
|
-
if (!Object.hasOwnProperty.call(selector, field)) {
|
|
19
|
-
return { matched: false };
|
|
20
|
-
}
|
|
21
|
-
const fieldSelector = selector[field];
|
|
22
|
-
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
23
|
-
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
|
|
24
|
-
if (keys.include == null && keys.exclude == null)
|
|
25
|
-
return { matched: false };
|
|
26
|
-
let includedIds = [];
|
|
27
|
-
if (keys.include == null) {
|
|
28
|
-
for (const set of index.values()) {
|
|
29
|
-
for (const pos of set) {
|
|
30
|
-
includedIds.push(pos);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
for (const key of keys.include) {
|
|
35
|
-
const posSet = index.get(key);
|
|
36
|
-
if (posSet) {
|
|
37
|
-
for (const pos of posSet) {
|
|
38
|
-
includedIds.push(pos);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (keys.exclude != null) {
|
|
44
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
45
|
-
for (const key of keys.exclude) {
|
|
46
|
-
const posSet = index.get(key);
|
|
47
|
-
if (posSet) {
|
|
48
|
-
for (const pos of posSet) {
|
|
49
|
-
excludeIds.add(pos);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
matched: true,
|
|
57
|
-
ids: includedIds,
|
|
58
|
-
fields: [field],
|
|
59
|
-
keepSelector: filteresForNull
|
|
60
|
-
};
|
|
61
|
-
},
|
|
62
|
-
rebuild(items) {
|
|
63
|
-
index.clear();
|
|
64
|
-
items.forEach((item) => {
|
|
65
|
-
const value = serializeValue(get(item, field));
|
|
66
|
-
ensureSet(value).add(item.id);
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
// NEW: delta methods
|
|
70
|
-
insert(items) {
|
|
71
|
-
for (const item of items) {
|
|
72
|
-
const value = serializeValue(get(item, field));
|
|
73
|
-
ensureSet(value).add(item.id);
|
|
74
|
-
}
|
|
75
|
-
},
|
|
76
|
-
remove(items) {
|
|
77
|
-
for (const item of items) {
|
|
78
|
-
const value = serializeValue(get(item, field));
|
|
79
|
-
const set = index.get(value);
|
|
80
|
-
if (!set)
|
|
81
|
-
continue;
|
|
82
|
-
set.delete(item.id);
|
|
83
|
-
if (set.size === 0)
|
|
84
|
-
index.delete(value);
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
update(pairs) {
|
|
88
|
-
for (const { oldItem, newItem } of pairs) {
|
|
89
|
-
const oldValue = serializeValue(get(oldItem, field));
|
|
90
|
-
const newValue = serializeValue(get(newItem, field));
|
|
91
|
-
if (oldValue === newValue)
|
|
92
|
-
continue;
|
|
93
|
-
const oldSet = index.get(oldValue);
|
|
94
|
-
if (oldSet) {
|
|
95
|
-
oldSet.delete(oldItem.id);
|
|
96
|
-
if (oldSet.size === 0)
|
|
97
|
-
index.delete(oldValue);
|
|
98
|
-
}
|
|
99
|
-
ensureSet(newValue).add(newItem.id);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
module.exports = createIndex;
|