@signaldb/core 1.5.4 → 1.7.0
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 +29 -28
- package/dist/Collection/createIndex.d.ts +1 -1
- package/dist/Collection/index.d.ts +1 -0
- package/dist/index.cjs.js +14 -12
- package/dist/index.cjs10.js +27 -10
- package/dist/index.cjs11.js +10 -3
- package/dist/index.cjs12.js +3 -136
- package/dist/index.cjs13.js +131 -61
- package/dist/index.cjs14.js +72 -5
- package/dist/index.cjs15.js +8 -41
- package/dist/index.cjs16.js +25 -11
- package/dist/index.cjs17.js +143 -23
- package/dist/index.cjs18.js +5 -262
- package/dist/index.cjs19.js +36 -63
- package/dist/index.cjs2.js +210 -732
- package/dist/index.cjs20.js +13 -85
- package/dist/index.cjs21.js +24 -8
- package/dist/index.cjs22.js +67 -24
- package/dist/index.cjs23.js +72 -131
- package/dist/index.cjs24.js +16 -5
- package/dist/index.cjs25.js +22 -11
- package/dist/index.cjs26.js +7 -27
- package/dist/index.cjs27.js +5 -40
- package/dist/index.cjs28.js +26 -26
- package/dist/index.cjs29.js +40 -7
- package/dist/index.cjs3.js +757 -136
- package/dist/index.cjs4.js +165 -62
- package/dist/index.cjs5.js +67 -3
- package/dist/index.cjs6.js +2 -2
- package/dist/index.cjs7.js +2 -2
- package/dist/index.cjs8.js +2 -2
- package/dist/index.cjs9.js +3 -27
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +14 -12
- package/dist/index10.mjs +27 -10
- package/dist/index11.mjs +10 -3
- package/dist/index12.mjs +3 -136
- package/dist/index13.mjs +131 -60
- package/dist/index14.mjs +71 -5
- package/dist/index15.mjs +8 -40
- package/dist/index16.mjs +25 -11
- package/dist/index17.mjs +143 -23
- package/dist/index18.mjs +5 -261
- package/dist/index19.mjs +36 -63
- package/dist/index2.mjs +210 -733
- package/dist/index20.mjs +13 -84
- package/dist/index21.mjs +24 -8
- package/dist/index22.mjs +66 -24
- package/dist/index23.mjs +71 -131
- package/dist/index24.mjs +16 -5
- package/dist/index25.mjs +22 -11
- package/dist/index26.mjs +7 -27
- package/dist/index27.mjs +5 -40
- package/dist/index28.mjs +26 -26
- package/dist/index29.mjs +40 -7
- package/dist/index3.mjs +757 -136
- package/dist/index4.mjs +165 -61
- package/dist/index5.mjs +66 -3
- package/dist/index6.mjs +2 -2
- package/dist/index7.mjs +2 -2
- package/dist/index8.mjs +2 -2
- package/dist/index9.mjs +3 -27
- package/dist/types/IndexProvider.d.ts +2 -0
- package/dist/types/Modifier.d.ts +17 -10
- package/dist/types/Selector.d.ts +1 -7
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/serializeValue.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.cjs14.js
CHANGED
|
@@ -1,7 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const createIndexProvider = require("./index.cjs6.js");
|
|
4
|
+
const get = require("./index.cjs24.js");
|
|
5
|
+
const getMatchingKeys = require("./index.cjs28.js");
|
|
6
|
+
const serializeValue = require("./index.cjs20.js");
|
|
7
|
+
function createExternalIndex(field, index) {
|
|
8
|
+
return createIndexProvider({
|
|
9
|
+
query(selector) {
|
|
10
|
+
if (!Object.hasOwnProperty.call(selector, field)) {
|
|
11
|
+
return { matched: false };
|
|
12
|
+
}
|
|
13
|
+
const fieldSelector = selector[field];
|
|
14
|
+
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
15
|
+
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
|
|
16
|
+
if (keys.include == null && keys.exclude == null)
|
|
17
|
+
return { matched: false };
|
|
18
|
+
let includedPositions = [];
|
|
19
|
+
if (keys.include == null) {
|
|
20
|
+
for (const set of index.values()) {
|
|
21
|
+
for (const pos of set) {
|
|
22
|
+
includedPositions.push(pos);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
for (const key of keys.include) {
|
|
27
|
+
const posSet = index.get(key);
|
|
28
|
+
if (posSet) {
|
|
29
|
+
for (const pos of posSet) {
|
|
30
|
+
includedPositions.push(pos);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (keys.exclude != null) {
|
|
36
|
+
const excludeSet = /* @__PURE__ */ new Set();
|
|
37
|
+
for (const key of keys.exclude) {
|
|
38
|
+
const posSet = index.get(key);
|
|
39
|
+
if (posSet) {
|
|
40
|
+
for (const pos of posSet) {
|
|
41
|
+
excludeSet.add(pos);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
matched: true,
|
|
49
|
+
positions: includedPositions,
|
|
50
|
+
fields: [field],
|
|
51
|
+
keepSelector: filteresForNull
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
rebuild() {
|
|
55
|
+
}
|
|
56
|
+
});
|
|
6
57
|
}
|
|
7
|
-
|
|
58
|
+
function createIndex(field) {
|
|
59
|
+
const index = /* @__PURE__ */ new Map();
|
|
60
|
+
return {
|
|
61
|
+
...createExternalIndex(field, index),
|
|
62
|
+
rebuild(items) {
|
|
63
|
+
index.clear();
|
|
64
|
+
items.forEach((item, i) => {
|
|
65
|
+
const value = serializeValue(get(item, field));
|
|
66
|
+
const current = index.get(value) || /* @__PURE__ */ new Set();
|
|
67
|
+
current.add(i);
|
|
68
|
+
index.set(value, current);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
exports.createExternalIndex = createExternalIndex;
|
|
74
|
+
exports.default = createIndex;
|
package/dist/index.cjs15.js
CHANGED
|
@@ -1,43 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return
|
|
8
|
-
|
|
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;
|
|
2
|
+
const fastSort = require("fast-sort");
|
|
3
|
+
const get = require("./index.cjs24.js");
|
|
4
|
+
function sortItems(items, sortFields) {
|
|
5
|
+
return fastSort.sort(items).by(Object.entries(sortFields).map(([key, value]) => {
|
|
6
|
+
const order = value === 1 ? "asc" : "desc";
|
|
7
|
+
return { [order]: (i) => get(i, key) };
|
|
8
|
+
}));
|
|
35
9
|
}
|
|
36
|
-
|
|
37
|
-
if (typeof structuredClone === "function")
|
|
38
|
-
return structuredClone(object);
|
|
39
|
-
/* istanbul ignore next -- @preserve */
|
|
40
|
-
return clone(object);
|
|
41
|
-
}
|
|
42
|
-
exports.clone = clone;
|
|
43
|
-
exports.default = deepClone;
|
|
10
|
+
module.exports = sortItems;
|
package/dist/index.cjs16.js
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
const get = require("./index.cjs24.js");
|
|
3
|
+
const set = require("./index.cjs25.js");
|
|
4
|
+
function project(item, fields) {
|
|
5
|
+
const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
|
|
6
|
+
if (allFieldsDeactivated) {
|
|
7
|
+
const result2 = { ...item };
|
|
8
|
+
Object.keys(fields).forEach((key) => {
|
|
9
|
+
const fieldValue = get(item, key);
|
|
10
|
+
if (fieldValue === void 0)
|
|
11
|
+
return;
|
|
12
|
+
set(result2, key, void 0, true);
|
|
13
|
+
});
|
|
14
|
+
return result2;
|
|
15
|
+
}
|
|
16
|
+
const result = {};
|
|
17
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
18
|
+
const fieldValue = get(item, key);
|
|
19
|
+
if (fieldValue === void 0)
|
|
20
|
+
return;
|
|
21
|
+
if (fieldValue == null && value !== 1)
|
|
22
|
+
return;
|
|
23
|
+
set(result, key, value === 1 ? fieldValue : void 0);
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
12
26
|
}
|
|
13
|
-
module.exports =
|
|
27
|
+
module.exports = project;
|
package/dist/index.cjs17.js
CHANGED
|
@@ -1,26 +1,146 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
const isEqual = require("./index.cjs10.js");
|
|
6
|
+
const uniqueBy = require("./index.cjs26.js");
|
|
7
|
+
class Observer {
|
|
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
|
+
__publicField(this, "previousItems", []);
|
|
15
|
+
__publicField(this, "callbacks");
|
|
16
|
+
__publicField(this, "unbindEvents");
|
|
17
|
+
this.callbacks = {
|
|
18
|
+
added: [],
|
|
19
|
+
addedBefore: [],
|
|
20
|
+
changed: [],
|
|
21
|
+
changedField: [],
|
|
22
|
+
movedBefore: [],
|
|
23
|
+
removed: []
|
|
24
|
+
};
|
|
25
|
+
this.unbindEvents = bindEvents();
|
|
26
|
+
}
|
|
27
|
+
call(event, ...args) {
|
|
28
|
+
this.callbacks[event].forEach(({ callback, options }) => {
|
|
29
|
+
if (!options.skipInitial || !options.isInitial) {
|
|
30
|
+
callback(...args);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
hasCallbacks(events) {
|
|
35
|
+
return events.some((event) => this.callbacks[event].length > 0);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Determines if the observer has no active callbacks registered for any events.
|
|
39
|
+
* @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
|
|
40
|
+
*/
|
|
41
|
+
isEmpty() {
|
|
42
|
+
return !this.hasCallbacks([
|
|
43
|
+
"added",
|
|
44
|
+
"addedBefore",
|
|
45
|
+
"changed",
|
|
46
|
+
"changedField",
|
|
47
|
+
"movedBefore",
|
|
48
|
+
"removed"
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
53
|
+
* for events such as added, removed, changed, or moved items.
|
|
54
|
+
* @param newItems - The new list of items to compare against the previous state.
|
|
55
|
+
*/
|
|
56
|
+
runChecks(newItems) {
|
|
57
|
+
const oldItemsMap = new Map(this.previousItems.map((item, index) => [
|
|
58
|
+
item.id,
|
|
59
|
+
{ item, index, beforeItem: this.previousItems[index + 1] || null }
|
|
60
|
+
]));
|
|
61
|
+
const newItemsMap = new Map(newItems.map((item, index) => [
|
|
62
|
+
item.id,
|
|
63
|
+
{ item, index, beforeItem: newItems[index + 1] || null }
|
|
64
|
+
]));
|
|
65
|
+
if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
|
|
66
|
+
oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
|
|
67
|
+
var _a;
|
|
68
|
+
const newItem = newItemsMap.get(oldItem.id);
|
|
69
|
+
if (newItem) {
|
|
70
|
+
if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
|
|
71
|
+
this.call("changed", newItem.item);
|
|
72
|
+
if (this.hasCallbacks(["changedField"])) {
|
|
73
|
+
const keys = uniqueBy([
|
|
74
|
+
...Object.keys(newItem.item),
|
|
75
|
+
...Object.keys(oldItem)
|
|
76
|
+
], (value) => value);
|
|
77
|
+
keys.forEach((key) => {
|
|
78
|
+
if (isEqual(newItem.item[key], oldItem[key]))
|
|
79
|
+
return;
|
|
80
|
+
this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (newItem.index !== index && ((_a = newItem.beforeItem) == null ? void 0 : _a.id) !== (oldBeforeItem == null ? void 0 : oldBeforeItem.id)) {
|
|
85
|
+
this.call("movedBefore", newItem.item, newItem.beforeItem);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
this.call("removed", oldItem);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
22
91
|
}
|
|
23
|
-
|
|
24
|
-
|
|
92
|
+
if (this.hasCallbacks(["added", "addedBefore"])) {
|
|
93
|
+
newItems.forEach((newItem, index) => {
|
|
94
|
+
const oldItem = oldItemsMap.get(newItem.id);
|
|
95
|
+
if (oldItem)
|
|
96
|
+
return;
|
|
97
|
+
this.call("added", newItem);
|
|
98
|
+
this.call("addedBefore", newItem, newItems[index + 1] || null);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
this.previousItems = newItems;
|
|
102
|
+
Object.keys(this.callbacks).forEach((key) => {
|
|
103
|
+
const event = key;
|
|
104
|
+
const callbacks = this.callbacks[event];
|
|
105
|
+
this.callbacks[event] = callbacks.map((callback) => ({
|
|
106
|
+
...callback,
|
|
107
|
+
options: {
|
|
108
|
+
...callback.options,
|
|
109
|
+
isInitial: false
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Stops the observer by unbinding all events and cleaning up resources.
|
|
116
|
+
*/
|
|
117
|
+
stop() {
|
|
118
|
+
this.unbindEvents();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Registers callbacks for specific events to observe changes in the collection.
|
|
122
|
+
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
123
|
+
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
124
|
+
*/
|
|
125
|
+
addCallbacks(callbacks, skipInitial = false) {
|
|
126
|
+
Object.keys(callbacks).forEach((key) => {
|
|
127
|
+
const typedKey = key;
|
|
128
|
+
this.callbacks[typedKey].push({
|
|
129
|
+
callback: callbacks[typedKey],
|
|
130
|
+
options: { skipInitial, isInitial: true }
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
136
|
+
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
137
|
+
*/
|
|
138
|
+
removeCallbacks(callbacks) {
|
|
139
|
+
Object.keys(callbacks).forEach((key) => {
|
|
140
|
+
const typedKey = key;
|
|
141
|
+
const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
|
|
142
|
+
this.callbacks[typedKey].splice(index, 1);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
25
145
|
}
|
|
26
|
-
module.exports =
|
|
146
|
+
module.exports = Observer;
|
package/dist/index.cjs18.js
CHANGED
|
@@ -1,264 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const sortItems = require("./index.cjs21.js");
|
|
7
|
-
const project = require("./index.cjs22.js");
|
|
8
|
-
const Observer = require("./index.cjs23.js");
|
|
9
|
-
function isInReactiveScope(reactivity) {
|
|
10
|
-
if (!reactivity)
|
|
11
|
-
return false;
|
|
12
|
-
if (!reactivity.isInScope)
|
|
13
|
-
return true;
|
|
14
|
-
return reactivity.isInScope();
|
|
2
|
+
const mingo = require("mingo");
|
|
3
|
+
function match(item, selector) {
|
|
4
|
+
const query = new mingo.Query(selector);
|
|
5
|
+
return query.test(item);
|
|
15
6
|
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Creates a new instance of the `Cursor` class.
|
|
19
|
-
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
20
|
-
* @template T - The type of the items in the collection.
|
|
21
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
22
|
-
* @param getItems - A function that retrieves the filtered list of items.
|
|
23
|
-
* @param options - Optional configuration for the cursor.
|
|
24
|
-
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
25
|
-
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
26
|
-
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
27
|
-
* @param options.sort - A sort specifier to determine the order of the items.
|
|
28
|
-
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
29
|
-
* @param options.limit - The maximum number of items to return in the result set.
|
|
30
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
31
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
32
|
-
*/
|
|
33
|
-
constructor(getItems, options) {
|
|
34
|
-
__publicField(this, "observer");
|
|
35
|
-
__publicField(this, "getFilteredItems");
|
|
36
|
-
__publicField(this, "options");
|
|
37
|
-
__publicField(this, "onCleanupCallbacks", []);
|
|
38
|
-
this.getFilteredItems = getItems;
|
|
39
|
-
this.options = options || {};
|
|
40
|
-
}
|
|
41
|
-
addGetters(item) {
|
|
42
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
43
|
-
return item;
|
|
44
|
-
const depend = this.depend.bind(this);
|
|
45
|
-
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
46
|
-
Object.defineProperty(memo, key, {
|
|
47
|
-
get() {
|
|
48
|
-
depend({
|
|
49
|
-
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
50
|
-
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
51
|
-
return;
|
|
52
|
-
notify();
|
|
53
|
-
}
|
|
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)
|
|
66
|
-
return item;
|
|
67
|
-
return this.options.transform(item);
|
|
68
|
-
}
|
|
69
|
-
getItems() {
|
|
70
|
-
const items = this.getFilteredItems();
|
|
71
|
-
const { sort, skip, limit } = this.options;
|
|
72
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
73
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
74
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
75
|
-
const idExcluded = this.options.fields && this.options.fields.id === 0;
|
|
76
|
-
return limited.map((item) => {
|
|
77
|
-
if (!this.options.fields)
|
|
78
|
-
return item;
|
|
79
|
-
return {
|
|
80
|
-
...idExcluded ? {} : { id: item.id },
|
|
81
|
-
...project(item, this.options.fields)
|
|
82
|
-
};
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
depend(changeEvents) {
|
|
86
|
-
if (!this.options.reactive)
|
|
87
|
-
return;
|
|
88
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
89
|
-
return;
|
|
90
|
-
const signal = this.options.reactive.create();
|
|
91
|
-
signal.depend();
|
|
92
|
-
const notify = () => signal.notify();
|
|
93
|
-
function buildNotifier(event) {
|
|
94
|
-
const eventHandler = changeEvents[event];
|
|
95
|
-
return (...args) => {
|
|
96
|
-
if (eventHandler === true) {
|
|
97
|
-
notify();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (typeof eventHandler !== "function")
|
|
101
|
-
return;
|
|
102
|
-
eventHandler(notify)(...args);
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
const stop = this.observeRawChanges({
|
|
106
|
-
added: buildNotifier("added"),
|
|
107
|
-
addedBefore: buildNotifier("addedBefore"),
|
|
108
|
-
changed: buildNotifier("changed"),
|
|
109
|
-
changedField: buildNotifier("changedField"),
|
|
110
|
-
movedBefore: buildNotifier("movedBefore"),
|
|
111
|
-
removed: buildNotifier("removed")
|
|
112
|
-
}, true);
|
|
113
|
-
if (this.options.reactive.onDispose) {
|
|
114
|
-
this.options.reactive.onDispose(() => stop(), signal);
|
|
115
|
-
}
|
|
116
|
-
this.onCleanup(stop);
|
|
117
|
-
}
|
|
118
|
-
ensureObserver() {
|
|
119
|
-
if (!this.observer) {
|
|
120
|
-
const observer = new Observer(() => {
|
|
121
|
-
const requery = () => {
|
|
122
|
-
observer.runChecks(this.getItems());
|
|
123
|
-
};
|
|
124
|
-
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
125
|
-
return () => {
|
|
126
|
-
if (cleanup)
|
|
127
|
-
cleanup();
|
|
128
|
-
};
|
|
129
|
-
});
|
|
130
|
-
this.onCleanup(() => observer.stop());
|
|
131
|
-
this.observer = observer;
|
|
132
|
-
}
|
|
133
|
-
return this.observer;
|
|
134
|
-
}
|
|
135
|
-
observeRawChanges(callbacks, skipInitial = false) {
|
|
136
|
-
const observer = this.ensureObserver();
|
|
137
|
-
observer.addCallbacks(callbacks, skipInitial);
|
|
138
|
-
observer.runChecks(this.getItems());
|
|
139
|
-
return () => {
|
|
140
|
-
observer.removeCallbacks(callbacks);
|
|
141
|
-
if (!observer.isEmpty())
|
|
142
|
-
return;
|
|
143
|
-
observer.stop();
|
|
144
|
-
this.observer = void 0;
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
149
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
150
|
-
* to prevent memory leaks.
|
|
151
|
-
*/
|
|
152
|
-
cleanup() {
|
|
153
|
-
this.onCleanupCallbacks.forEach((callback) => {
|
|
154
|
-
callback();
|
|
155
|
-
});
|
|
156
|
-
this.onCleanupCallbacks = [];
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
160
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
161
|
-
* @param callback - A function to be executed during cleanup.
|
|
162
|
-
*/
|
|
163
|
-
onCleanup(callback) {
|
|
164
|
-
this.onCleanupCallbacks.push(callback);
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
168
|
-
* function to each transformed item.
|
|
169
|
-
* ⚡️ this function is reactive!
|
|
170
|
-
* @param callback - A function to execute for each item in the result set.
|
|
171
|
-
* @param callback.item - The transformed item.
|
|
172
|
-
*/
|
|
173
|
-
forEach(callback) {
|
|
174
|
-
const items = this.getItems();
|
|
175
|
-
this.depend({
|
|
176
|
-
addedBefore: true,
|
|
177
|
-
removed: true,
|
|
178
|
-
movedBefore: true,
|
|
179
|
-
...this.options.fieldTracking ? {} : { changed: true }
|
|
180
|
-
});
|
|
181
|
-
items.forEach((item) => {
|
|
182
|
-
callback(this.transform(item));
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Creates a new array populated with the results of applying the provided callback
|
|
187
|
-
* function to each transformed item in the cursor's result set.
|
|
188
|
-
* ⚡️ this function is reactive!
|
|
189
|
-
* @template V - The type of the items in the resulting array.
|
|
190
|
-
* @param callback - A function to execute for each item in the result set.
|
|
191
|
-
* @param callback.item - The transformed item.
|
|
192
|
-
* @returns An array of results after applying the callback to each item.
|
|
193
|
-
*/
|
|
194
|
-
map(callback) {
|
|
195
|
-
const results = [];
|
|
196
|
-
this.forEach((item) => {
|
|
197
|
-
results.push(callback(item));
|
|
198
|
-
});
|
|
199
|
-
return results;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
203
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
204
|
-
* ⚡️ this function is reactive!
|
|
205
|
-
* @returns An array of transformed items in the result set.
|
|
206
|
-
*/
|
|
207
|
-
fetch() {
|
|
208
|
-
return this.map((item) => item);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
212
|
-
* filtering and other criteria.
|
|
213
|
-
* ⚡️ this function is reactive!
|
|
214
|
-
* @returns The total number of items in the result set.
|
|
215
|
-
*/
|
|
216
|
-
count() {
|
|
217
|
-
const items = this.getItems();
|
|
218
|
-
this.depend({
|
|
219
|
-
added: true,
|
|
220
|
-
removed: true
|
|
221
|
-
});
|
|
222
|
-
return items.length;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
226
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
227
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
228
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
229
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
230
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
231
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
232
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
233
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
234
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
235
|
-
* @returns A function to stop observing changes.
|
|
236
|
-
*/
|
|
237
|
-
observeChanges(callbacks, skipInitial = false) {
|
|
238
|
-
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
239
|
-
if (!callback)
|
|
240
|
-
return memo;
|
|
241
|
-
return {
|
|
242
|
-
...memo,
|
|
243
|
-
[callbackName]: (item, before) => {
|
|
244
|
-
const transformedValue = this.transform(item);
|
|
245
|
-
const hasBeforeParameter = before !== void 0;
|
|
246
|
-
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
247
|
-
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}, {}), skipInitial);
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
254
|
-
* from the collection. This is useful when the underlying data or query
|
|
255
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
256
|
-
*/
|
|
257
|
-
requery() {
|
|
258
|
-
if (!this.observer)
|
|
259
|
-
return;
|
|
260
|
-
this.observer.runChecks(this.getItems());
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
exports.default = Cursor;
|
|
264
|
-
exports.isInReactiveScope = isInReactiveScope;
|
|
7
|
+
module.exports = match;
|