@signaldb/core 1.2.4 → 1.3.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 +28 -47
- package/dist/index.cjs.js +12 -18
- package/dist/index.cjs10.js +6 -27
- package/dist/index.cjs11.js +3 -6
- package/dist/index.cjs12.js +136 -3
- package/dist/index.cjs13.js +40 -134
- package/dist/index.cjs14.js +5 -38
- package/dist/index.cjs15.js +42 -2
- package/dist/index.cjs16.js +11 -5
- package/dist/index.cjs17.js +17 -40
- package/dist/index.cjs18.js +262 -11
- package/dist/index.cjs19.js +66 -16
- package/dist/index.cjs2.js +688 -96
- package/dist/index.cjs20.js +71 -247
- package/dist/index.cjs21.js +8 -68
- package/dist/index.cjs22.js +22 -83
- package/dist/index.cjs23.js +144 -8
- package/dist/index.cjs24.js +5 -25
- package/dist/index.cjs25.js +16 -144
- package/dist/index.cjs26.js +27 -5
- package/dist/index.cjs27.js +39 -15
- package/dist/index.cjs28.js +25 -24
- package/dist/index.cjs29.js +7 -40
- package/dist/index.cjs3.js +139 -664
- package/dist/index.cjs4.js +62 -166
- package/dist/index.cjs5.js +3 -67
- 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 +27 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.mjs +12 -18
- package/dist/index10.mjs +6 -27
- package/dist/index11.mjs +3 -6
- package/dist/index12.mjs +136 -3
- package/dist/index13.mjs +39 -134
- package/dist/index14.mjs +5 -37
- package/dist/index15.mjs +41 -2
- package/dist/index16.mjs +11 -5
- package/dist/index17.mjs +17 -39
- package/dist/index18.mjs +261 -11
- package/dist/index19.mjs +65 -16
- package/dist/index2.mjs +688 -94
- package/dist/index20.mjs +71 -247
- package/dist/index21.mjs +8 -67
- package/dist/index22.mjs +22 -82
- package/dist/index23.mjs +144 -8
- package/dist/index24.mjs +5 -25
- package/dist/index25.mjs +16 -144
- package/dist/index26.mjs +27 -5
- package/dist/index27.mjs +39 -15
- package/dist/index28.mjs +25 -24
- package/dist/index29.mjs +7 -40
- package/dist/index3.mjs +139 -664
- package/dist/index4.mjs +61 -166
- package/dist/index5.mjs +3 -66
- package/dist/index6.mjs +2 -2
- package/dist/index7.mjs +2 -2
- package/dist/index8.mjs +2 -2
- package/dist/index9.mjs +27 -3
- package/dist/utils/getMatchingKeys.d.ts +11 -6
- package/package.json +1 -1
- package/dist/devtools.d.ts +0 -4
- package/dist/index.cjs30.js +0 -29
- package/dist/index.cjs31.js +0 -9
- package/dist/index30.mjs +0 -30
- package/dist/index31.mjs +0 -10
package/dist/index13.mjs
CHANGED
|
@@ -1,139 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let listenersSet = this._listenerStore.get(eventName);
|
|
24
|
-
if (!listenersSet) {
|
|
25
|
-
listenersSet = /* @__PURE__ */ new Set();
|
|
26
|
-
this._listenerStore.set(eventName, listenersSet);
|
|
1
|
+
import createIndexProvider from "./index5.mjs";
|
|
2
|
+
import get from "./index25.mjs";
|
|
3
|
+
import getMatchingKeys from "./index26.mjs";
|
|
4
|
+
import serializeValue from "./index16.mjs";
|
|
5
|
+
function createExternalIndex(field, index) {
|
|
6
|
+
return createIndexProvider({
|
|
7
|
+
query(selector) {
|
|
8
|
+
const keys = getMatchingKeys(field, selector);
|
|
9
|
+
if (keys.include == null && keys.exclude == null)
|
|
10
|
+
return { matched: false };
|
|
11
|
+
const includedKeys = keys.include == null ? [...index.values()].reduce((memo, set) => [...memo, ...set], []) : keys.include.reduce((memo, key) => [...memo, ...index.get(key) || []], []);
|
|
12
|
+
const itemPositions = keys.exclude == null ? includedKeys : keys.exclude.reduce((memo, key) => memo.filter((i) => {
|
|
13
|
+
var _a;
|
|
14
|
+
return !((_a = index.get(key)) == null ? void 0 : _a.has(i));
|
|
15
|
+
}), includedKeys);
|
|
16
|
+
return {
|
|
17
|
+
matched: true,
|
|
18
|
+
positions: itemPositions,
|
|
19
|
+
fields: [field]
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
rebuild() {
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Subscribe to an event, handling it only once. Automatically removes
|
|
45
|
-
* the listener after it fires the first time.
|
|
46
|
-
* @param eventName The event name (key of E).
|
|
47
|
-
* @param listener A function that receives the emitted arguments.
|
|
48
|
-
* @returns The emitter instance (for chaining).
|
|
49
|
-
*/
|
|
50
|
-
once(eventName, listener) {
|
|
51
|
-
const onceWrapper = (...args) => {
|
|
52
|
-
listener(...args);
|
|
53
|
-
this.off(eventName, onceWrapper);
|
|
54
|
-
};
|
|
55
|
-
return this.on(eventName, onceWrapper);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Unsubscribe a previously subscribed listener.
|
|
59
|
-
* @param eventName The event name (key of E).
|
|
60
|
-
* @param listener The original function passed to `on` or `once`.
|
|
61
|
-
* @returns The emitter instance (for chaining).
|
|
62
|
-
*/
|
|
63
|
-
off(eventName, listener) {
|
|
64
|
-
const listenersSet = this._listenerStore.get(eventName);
|
|
65
|
-
if (!listenersSet)
|
|
66
|
-
return this;
|
|
67
|
-
listenersSet.delete(listener);
|
|
68
|
-
if (listenersSet.size === 0) {
|
|
69
|
-
this._listenerStore.delete(eventName);
|
|
70
|
-
}
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Unsubscribe a previously subscribed listener.
|
|
75
|
-
* @param eventName The event name (key of E).
|
|
76
|
-
* @param listener The original function passed to `on` or `once`.
|
|
77
|
-
* @returns The emitter instance (for chaining).
|
|
78
|
-
*/
|
|
79
|
-
removeListener(eventName, listener) {
|
|
80
|
-
return this.off(eventName, listener);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Emit (dispatch) an event with a variable number of arguments.
|
|
84
|
-
* @param eventName The event name (key of E).
|
|
85
|
-
* @param args The arguments to pass to subscribed listeners.
|
|
86
|
-
*/
|
|
87
|
-
emit(eventName, ...args) {
|
|
88
|
-
this.listeners(eventName).forEach((listener) => {
|
|
89
|
-
listener(...args);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Returns the array of listener functions currently registered for a given event.
|
|
94
|
-
* @param eventName The event name (key of E).
|
|
95
|
-
* @returns An array of listener functions.
|
|
96
|
-
*/
|
|
97
|
-
listeners(eventName) {
|
|
98
|
-
const listenersSet = this._listenerStore.get(eventName);
|
|
99
|
-
if (!listenersSet)
|
|
100
|
-
return [];
|
|
101
|
-
return [...listenersSet.values()];
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Returns the number of listeners for a given event.
|
|
105
|
-
* @param eventName The event name (key of E).
|
|
106
|
-
* @returns The number of listeners.
|
|
107
|
-
*/
|
|
108
|
-
listenerCount(eventName) {
|
|
109
|
-
const listenersSet = this._listenerStore.get(eventName);
|
|
110
|
-
return listenersSet ? listenersSet.size : 0;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Removes all listeners for a given event, or all events if none is specified.
|
|
114
|
-
* @param eventName Optional. If omitted, clears all events’ listeners.
|
|
115
|
-
* @returns The emitter instance (for chaining).
|
|
116
|
-
*/
|
|
117
|
-
removeAllListeners(eventName) {
|
|
118
|
-
if (eventName === void 0) {
|
|
119
|
-
for (const [eventName_, listenersSet] of this._listenerStore.entries()) {
|
|
120
|
-
for (const listener of listenersSet.values()) {
|
|
121
|
-
this.off(eventName_, listener);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
this._listenerStore.clear();
|
|
125
|
-
} else {
|
|
126
|
-
const listenersSet = this._listenerStore.get(eventName);
|
|
127
|
-
if (listenersSet) {
|
|
128
|
-
for (const listener of listenersSet.values()) {
|
|
129
|
-
this.off(eventName, listener);
|
|
130
|
-
}
|
|
131
|
-
this._listenerStore.delete(eventName);
|
|
132
|
-
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function createIndex(field) {
|
|
27
|
+
const index = /* @__PURE__ */ new Map();
|
|
28
|
+
return {
|
|
29
|
+
...createExternalIndex(field, index),
|
|
30
|
+
rebuild(items) {
|
|
31
|
+
index.clear();
|
|
32
|
+
items.forEach((item, i) => {
|
|
33
|
+
const value = serializeValue(get(item, field));
|
|
34
|
+
const current = index.get(value) || /* @__PURE__ */ new Set();
|
|
35
|
+
current.add(i);
|
|
36
|
+
index.set(value, current);
|
|
37
|
+
});
|
|
133
38
|
}
|
|
134
|
-
|
|
135
|
-
}
|
|
39
|
+
};
|
|
136
40
|
}
|
|
137
41
|
export {
|
|
138
|
-
|
|
42
|
+
createExternalIndex,
|
|
43
|
+
createIndex as default
|
|
139
44
|
};
|
package/dist/index14.mjs
CHANGED
|
@@ -1,40 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function createExternalIndex(field, index) {
|
|
6
|
-
return createIndexProvider({
|
|
7
|
-
query(selector) {
|
|
8
|
-
const keys = getMatchingKeys(field, selector);
|
|
9
|
-
if (keys == null)
|
|
10
|
-
return { matched: false };
|
|
11
|
-
const itemPositions = keys.reduce((memo, key) => [...memo, ...index.get(key) || []], []);
|
|
12
|
-
return {
|
|
13
|
-
matched: true,
|
|
14
|
-
positions: itemPositions,
|
|
15
|
-
fields: [field]
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
rebuild() {
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
function createIndex(field) {
|
|
23
|
-
const index = /* @__PURE__ */ new Map();
|
|
24
|
-
return {
|
|
25
|
-
...createExternalIndex(field, index),
|
|
26
|
-
rebuild(items) {
|
|
27
|
-
index.clear();
|
|
28
|
-
items.forEach((item, i) => {
|
|
29
|
-
const value = serializeValue(get(item, field));
|
|
30
|
-
const current = index.get(value) || /* @__PURE__ */ new Set();
|
|
31
|
-
current.add(i);
|
|
32
|
-
index.set(value, current);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
};
|
|
1
|
+
import { Query } from "mingo";
|
|
2
|
+
function match(item, selector) {
|
|
3
|
+
const query = new Query(selector);
|
|
4
|
+
return query.test(item);
|
|
36
5
|
}
|
|
37
6
|
export {
|
|
38
|
-
|
|
39
|
-
createIndex as default
|
|
7
|
+
match as default
|
|
40
8
|
};
|
package/dist/index15.mjs
CHANGED
|
@@ -1,4 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
function clone(value) {
|
|
2
|
+
if (typeof value === "function")
|
|
3
|
+
throw new Error("Cloning functions is not supported");
|
|
4
|
+
if (value === null || typeof value !== "object")
|
|
5
|
+
return value;
|
|
6
|
+
if (value instanceof Date)
|
|
7
|
+
return new Date(value);
|
|
8
|
+
if (Array.isArray(value))
|
|
9
|
+
return value.map((item) => clone(item));
|
|
10
|
+
if (value instanceof Map) {
|
|
11
|
+
const result2 = /* @__PURE__ */ new Map();
|
|
12
|
+
value.forEach((currentValue, key) => {
|
|
13
|
+
result2.set(key, clone(currentValue));
|
|
14
|
+
});
|
|
15
|
+
return result2;
|
|
16
|
+
}
|
|
17
|
+
if (value instanceof Set) {
|
|
18
|
+
const result2 = /* @__PURE__ */ new Set();
|
|
19
|
+
value.forEach((currentValue) => {
|
|
20
|
+
result2.add(clone(currentValue));
|
|
21
|
+
});
|
|
22
|
+
return result2;
|
|
23
|
+
}
|
|
24
|
+
if (value instanceof RegExp)
|
|
25
|
+
return new RegExp(value);
|
|
26
|
+
const result = {};
|
|
27
|
+
for (const key in value) {
|
|
28
|
+
if (Object.hasOwnProperty.call(value, key)) {
|
|
29
|
+
result[key] = clone(value[key]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
function deepClone(object) {
|
|
35
|
+
if (typeof structuredClone === "function")
|
|
36
|
+
return structuredClone(object);
|
|
37
|
+
/* istanbul ignore next -- @preserve */
|
|
38
|
+
return clone(object);
|
|
39
|
+
}
|
|
2
40
|
export {
|
|
3
|
-
|
|
41
|
+
clone,
|
|
42
|
+
deepClone as default
|
|
4
43
|
};
|
package/dist/index16.mjs
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
function serializeValue(value) {
|
|
2
|
+
if (typeof value === "string")
|
|
3
|
+
return value;
|
|
4
|
+
if (typeof value === "number")
|
|
5
|
+
return value.toString();
|
|
6
|
+
if (typeof value === "boolean")
|
|
7
|
+
return value.toString();
|
|
8
|
+
if (value instanceof Date)
|
|
9
|
+
return value.toISOString();
|
|
10
|
+
return JSON.stringify(value);
|
|
5
11
|
}
|
|
6
12
|
export {
|
|
7
|
-
|
|
13
|
+
serializeValue as default
|
|
8
14
|
};
|
package/dist/index17.mjs
CHANGED
|
@@ -1,43 +1,21 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return result2;
|
|
16
|
-
}
|
|
17
|
-
if (value instanceof Set) {
|
|
18
|
-
const result2 = /* @__PURE__ */ new Set();
|
|
19
|
-
value.forEach((currentValue) => {
|
|
20
|
-
result2.add(clone(currentValue));
|
|
21
|
-
});
|
|
22
|
-
return result2;
|
|
23
|
-
}
|
|
24
|
-
if (value instanceof RegExp)
|
|
25
|
-
return new RegExp(value);
|
|
26
|
-
const result = {};
|
|
27
|
-
for (const key in value) {
|
|
28
|
-
if (Object.hasOwnProperty.call(value, key)) {
|
|
29
|
-
result[key] = clone(value[key]);
|
|
1
|
+
function createSignal(dependency, initialValue, isEqual = Object.is) {
|
|
2
|
+
let value = initialValue;
|
|
3
|
+
const signal = {
|
|
4
|
+
get() {
|
|
5
|
+
if (dependency)
|
|
6
|
+
dependency.depend();
|
|
7
|
+
return value;
|
|
8
|
+
},
|
|
9
|
+
set(newValue) {
|
|
10
|
+
if (isEqual(value, newValue))
|
|
11
|
+
return;
|
|
12
|
+
value = newValue;
|
|
13
|
+
if (dependency)
|
|
14
|
+
dependency.notify();
|
|
30
15
|
}
|
|
31
|
-
}
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
function deepClone(object) {
|
|
35
|
-
if (typeof structuredClone === "function")
|
|
36
|
-
return structuredClone(object);
|
|
37
|
-
/* istanbul ignore next -- @preserve */
|
|
38
|
-
return clone(object);
|
|
16
|
+
};
|
|
17
|
+
return signal;
|
|
39
18
|
}
|
|
40
19
|
export {
|
|
41
|
-
|
|
42
|
-
deepClone as default
|
|
20
|
+
createSignal as default
|
|
43
21
|
};
|
package/dist/index18.mjs
CHANGED
|
@@ -1,14 +1,264 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
10
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import sortItems from "./index21.mjs";
|
|
5
|
+
import project from "./index22.mjs";
|
|
6
|
+
import Observer from "./index23.mjs";
|
|
7
|
+
function isInReactiveScope(reactivity) {
|
|
8
|
+
if (!reactivity)
|
|
9
|
+
return false;
|
|
10
|
+
if (!reactivity.isInScope)
|
|
11
|
+
return true;
|
|
12
|
+
return reactivity.isInScope();
|
|
13
|
+
}
|
|
14
|
+
class Cursor {
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new instance of the `Cursor` class.
|
|
17
|
+
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
18
|
+
* @template T - The type of the items in the collection.
|
|
19
|
+
* @template U - The transformed item type after applying transformations (default is T).
|
|
20
|
+
* @param getItems - A function that retrieves the filtered list of items.
|
|
21
|
+
* @param options - Optional configuration for the cursor.
|
|
22
|
+
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
23
|
+
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
24
|
+
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
25
|
+
* @param options.sort - A sort specifier to determine the order of the items.
|
|
26
|
+
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
27
|
+
* @param options.limit - The maximum number of items to return in the result set.
|
|
28
|
+
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
29
|
+
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
30
|
+
*/
|
|
31
|
+
constructor(getItems, options) {
|
|
32
|
+
__publicField(this, "observer");
|
|
33
|
+
__publicField(this, "getFilteredItems");
|
|
34
|
+
__publicField(this, "options");
|
|
35
|
+
__publicField(this, "onCleanupCallbacks", []);
|
|
36
|
+
this.getFilteredItems = getItems;
|
|
37
|
+
this.options = options || {};
|
|
38
|
+
}
|
|
39
|
+
addGetters(item) {
|
|
40
|
+
if (!isInReactiveScope(this.options.reactive))
|
|
41
|
+
return item;
|
|
42
|
+
const depend = this.depend.bind(this);
|
|
43
|
+
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
44
|
+
Object.defineProperty(memo, key, {
|
|
45
|
+
get() {
|
|
46
|
+
depend({
|
|
47
|
+
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
48
|
+
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
49
|
+
return;
|
|
50
|
+
notify();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return value;
|
|
54
|
+
},
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true
|
|
57
|
+
});
|
|
58
|
+
return memo;
|
|
59
|
+
}, {});
|
|
60
|
+
}
|
|
61
|
+
transform(rawItem) {
|
|
62
|
+
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
63
|
+
if (!this.options.transform)
|
|
64
|
+
return item;
|
|
65
|
+
return this.options.transform(item);
|
|
66
|
+
}
|
|
67
|
+
getItems() {
|
|
68
|
+
const items = this.getFilteredItems();
|
|
69
|
+
const { sort, skip, limit } = this.options;
|
|
70
|
+
const sorted = sort ? sortItems(items, sort) : items;
|
|
71
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
72
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
73
|
+
const idExcluded = this.options.fields && this.options.fields.id === 0;
|
|
74
|
+
return limited.map((item) => {
|
|
75
|
+
if (!this.options.fields)
|
|
76
|
+
return item;
|
|
77
|
+
return {
|
|
78
|
+
...idExcluded ? {} : { id: item.id },
|
|
79
|
+
...project(item, this.options.fields)
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
depend(changeEvents) {
|
|
84
|
+
if (!this.options.reactive)
|
|
85
|
+
return;
|
|
86
|
+
if (!isInReactiveScope(this.options.reactive))
|
|
87
|
+
return;
|
|
88
|
+
const signal = this.options.reactive.create();
|
|
89
|
+
signal.depend();
|
|
90
|
+
const notify = () => signal.notify();
|
|
91
|
+
function buildNotifier(event) {
|
|
92
|
+
const eventHandler = changeEvents[event];
|
|
93
|
+
return (...args) => {
|
|
94
|
+
if (eventHandler === true) {
|
|
95
|
+
notify();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (typeof eventHandler !== "function")
|
|
99
|
+
return;
|
|
100
|
+
eventHandler(notify)(...args);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const stop = this.observeRawChanges({
|
|
104
|
+
added: buildNotifier("added"),
|
|
105
|
+
addedBefore: buildNotifier("addedBefore"),
|
|
106
|
+
changed: buildNotifier("changed"),
|
|
107
|
+
changedField: buildNotifier("changedField"),
|
|
108
|
+
movedBefore: buildNotifier("movedBefore"),
|
|
109
|
+
removed: buildNotifier("removed")
|
|
110
|
+
}, true);
|
|
111
|
+
if (this.options.reactive.onDispose) {
|
|
112
|
+
this.options.reactive.onDispose(() => stop(), signal);
|
|
113
|
+
}
|
|
114
|
+
this.onCleanup(stop);
|
|
115
|
+
}
|
|
116
|
+
ensureObserver() {
|
|
117
|
+
if (!this.observer) {
|
|
118
|
+
const observer = new Observer(() => {
|
|
119
|
+
const requery = () => {
|
|
120
|
+
observer.runChecks(this.getItems());
|
|
121
|
+
};
|
|
122
|
+
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
123
|
+
return () => {
|
|
124
|
+
if (cleanup)
|
|
125
|
+
cleanup();
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
this.onCleanup(() => observer.stop());
|
|
129
|
+
this.observer = observer;
|
|
130
|
+
}
|
|
131
|
+
return this.observer;
|
|
132
|
+
}
|
|
133
|
+
observeRawChanges(callbacks, skipInitial = false) {
|
|
134
|
+
const observer = this.ensureObserver();
|
|
135
|
+
observer.addCallbacks(callbacks, skipInitial);
|
|
136
|
+
observer.runChecks(this.getItems());
|
|
137
|
+
return () => {
|
|
138
|
+
observer.removeCallbacks(callbacks);
|
|
139
|
+
if (!observer.isEmpty())
|
|
140
|
+
return;
|
|
141
|
+
observer.stop();
|
|
142
|
+
this.observer = void 0;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
147
|
+
* and event listeners. This method should be called when the cursor is no longer needed
|
|
148
|
+
* to prevent memory leaks.
|
|
149
|
+
*/
|
|
150
|
+
cleanup() {
|
|
151
|
+
this.onCleanupCallbacks.forEach((callback) => {
|
|
152
|
+
callback();
|
|
153
|
+
});
|
|
154
|
+
this.onCleanupCallbacks = [];
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
158
|
+
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
159
|
+
* @param callback - A function to be executed during cleanup.
|
|
160
|
+
*/
|
|
161
|
+
onCleanup(callback) {
|
|
162
|
+
this.onCleanupCallbacks.push(callback);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
166
|
+
* function to each transformed item.
|
|
167
|
+
* ⚡️ this function is reactive!
|
|
168
|
+
* @param callback - A function to execute for each item in the result set.
|
|
169
|
+
* @param callback.item - The transformed item.
|
|
170
|
+
*/
|
|
171
|
+
forEach(callback) {
|
|
172
|
+
const items = this.getItems();
|
|
173
|
+
this.depend({
|
|
174
|
+
addedBefore: true,
|
|
175
|
+
removed: true,
|
|
176
|
+
movedBefore: true,
|
|
177
|
+
...this.options.fieldTracking ? {} : { changed: true }
|
|
178
|
+
});
|
|
179
|
+
items.forEach((item) => {
|
|
180
|
+
callback(this.transform(item));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
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.
|
|
191
|
+
*/
|
|
192
|
+
map(callback) {
|
|
193
|
+
const results = [];
|
|
194
|
+
this.forEach((item) => {
|
|
195
|
+
results.push(callback(item));
|
|
196
|
+
});
|
|
197
|
+
return results;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Fetches all transformed items from the cursor's result set as an array.
|
|
201
|
+
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
202
|
+
* ⚡️ this function is reactive!
|
|
203
|
+
* @returns An array of transformed items in the result set.
|
|
204
|
+
*/
|
|
205
|
+
fetch() {
|
|
206
|
+
return this.map((item) => item);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Counts the total number of items in the cursor's result set after applying
|
|
210
|
+
* filtering and other criteria.
|
|
211
|
+
* ⚡️ this function is reactive!
|
|
212
|
+
* @returns The total number of items in the result set.
|
|
213
|
+
*/
|
|
214
|
+
count() {
|
|
215
|
+
const items = this.getItems();
|
|
216
|
+
this.depend({
|
|
217
|
+
added: true,
|
|
218
|
+
removed: true
|
|
219
|
+
});
|
|
220
|
+
return items.length;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
224
|
+
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
225
|
+
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
226
|
+
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
227
|
+
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
228
|
+
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
229
|
+
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
230
|
+
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
231
|
+
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
232
|
+
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
233
|
+
* @returns A function to stop observing changes.
|
|
234
|
+
*/
|
|
235
|
+
observeChanges(callbacks, skipInitial = false) {
|
|
236
|
+
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
237
|
+
if (!callback)
|
|
238
|
+
return memo;
|
|
239
|
+
return {
|
|
240
|
+
...memo,
|
|
241
|
+
[callbackName]: (item, before) => {
|
|
242
|
+
const transformedValue = this.transform(item);
|
|
243
|
+
const hasBeforeParameter = before !== void 0;
|
|
244
|
+
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
245
|
+
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}, {}), skipInitial);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
252
|
+
* from the collection. This is useful when the underlying data or query
|
|
253
|
+
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
254
|
+
*/
|
|
255
|
+
requery() {
|
|
256
|
+
if (!this.observer)
|
|
257
|
+
return;
|
|
258
|
+
this.observer.runChecks(this.getItems());
|
|
259
|
+
}
|
|
11
260
|
}
|
|
12
261
|
export {
|
|
13
|
-
|
|
262
|
+
Cursor as default,
|
|
263
|
+
isInReactiveScope
|
|
14
264
|
};
|