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