@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
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const require_intersection = require("./index16.cjs.js");
|
|
2
|
+
//#region src/getIndexInfo.ts
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves merged index information for a given flat selector by querying multiple
|
|
5
|
+
* index providers. Combines results from all index providers to determine matched ids
|
|
6
|
+
* and an optimized selector.
|
|
7
|
+
* @template T - The type of the items in the collection.
|
|
8
|
+
* @template I - The type of the unique identifier for the items.
|
|
9
|
+
* @param queryFunctions - An array of index providers to query.
|
|
10
|
+
* @param selector - The flat selector used to filter items.
|
|
11
|
+
* @returns An object containing:
|
|
12
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
13
|
+
* - `ids`: An array of matched item ids.
|
|
14
|
+
* - `optimizedSelector`: A flat selector optimized based on the index results.
|
|
15
|
+
*/
|
|
16
|
+
function getMergedIndexInfo(queryFunctions, selector) {
|
|
17
|
+
return queryFunctions.reduce((memoOrPromise, queryFunction) => {
|
|
18
|
+
const resultOrPromise = queryFunction(selector);
|
|
19
|
+
const processResult = (memo, result) => {
|
|
20
|
+
if (!result.matched) return memo;
|
|
21
|
+
const optimizedSelector = result.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
|
|
22
|
+
return {
|
|
23
|
+
matched: true,
|
|
24
|
+
ids: [...new Set(memo.matched ? require_intersection.default(memo.ids, result.ids) : result.ids)],
|
|
25
|
+
optimizedSelector
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
if (resultOrPromise instanceof Promise) return resultOrPromise.then(async (result) => {
|
|
29
|
+
return processResult(memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise, result);
|
|
30
|
+
});
|
|
31
|
+
const memo = memoOrPromise;
|
|
32
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
33
|
+
return processResult(memo, resultOrPromise);
|
|
34
|
+
}, {
|
|
35
|
+
matched: false,
|
|
36
|
+
ids: [],
|
|
37
|
+
optimizedSelector: { ...selector }
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
|
|
42
|
+
* @param queryFunctions - An array of index providers to query.
|
|
43
|
+
* @param logicGate - An array of selectors representing the logic gate.
|
|
44
|
+
* @param idsCallback - A callback function called with matched ids.
|
|
45
|
+
* @returns An array of optimized selectors or a promise that resolves to such an array.
|
|
46
|
+
*/
|
|
47
|
+
function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
|
|
48
|
+
return logicGate.reduce((memoOrPromise, sel) => {
|
|
49
|
+
const getSelector = (indexInfo) => {
|
|
50
|
+
const { matched: selMatched, ids: selIds, optimizedSelector } = indexInfo;
|
|
51
|
+
if (selMatched) {
|
|
52
|
+
idsCallback(true, selIds);
|
|
53
|
+
if (Object.keys(optimizedSelector).length > 0) return optimizedSelector;
|
|
54
|
+
} else {
|
|
55
|
+
idsCallback(false, []);
|
|
56
|
+
return sel;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
|
|
60
|
+
if (indexInfoOrPromise instanceof Promise) return indexInfoOrPromise.then(async (indexInfo) => {
|
|
61
|
+
const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
62
|
+
const optimizedSelector = getSelector(indexInfo);
|
|
63
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
64
|
+
return memo;
|
|
65
|
+
});
|
|
66
|
+
const memo = memoOrPromise;
|
|
67
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
68
|
+
const optimizedSelector = getSelector(indexInfoOrPromise);
|
|
69
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
70
|
+
return memo;
|
|
71
|
+
}, []);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves index information for a given complex selector by querying multiple
|
|
75
|
+
* index providers. Handles nested `$and` and `$or` conditions in the selector and
|
|
76
|
+
* optimizes the selector to minimize processing overhead.
|
|
77
|
+
* @template T - The type of the items in the collection.
|
|
78
|
+
* @template I - The type of the unique identifier for the items.
|
|
79
|
+
* @param queryFunctions - An array of index providers to query.
|
|
80
|
+
* @param selector - The complex selector used to filter items.
|
|
81
|
+
* @returns An object containing:
|
|
82
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
83
|
+
* - `ids`: An array of matched item ids.
|
|
84
|
+
* - `optimizedSelector`: A selector optimized based on the index results, with unused
|
|
85
|
+
* conditions removed.
|
|
86
|
+
*/
|
|
87
|
+
function getIndexInfo(queryFunctions, selector) {
|
|
88
|
+
if (selector == null || Object.keys(selector).length <= 0) return {
|
|
89
|
+
matched: false,
|
|
90
|
+
ids: [],
|
|
91
|
+
optimizedSelector: selector
|
|
92
|
+
};
|
|
93
|
+
const { $and, $or, ...rest } = selector;
|
|
94
|
+
const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
|
|
95
|
+
const processFlatInfo = (flatInfo) => {
|
|
96
|
+
let { matched, ids } = flatInfo;
|
|
97
|
+
const newSelector = flatInfo.optimizedSelector;
|
|
98
|
+
const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
|
|
99
|
+
if (!match) return;
|
|
100
|
+
ids = matched ? require_intersection.default(ids, selIds) : selIds;
|
|
101
|
+
matched = true;
|
|
102
|
+
}) : void 0;
|
|
103
|
+
const process$and = ($andNew) => {
|
|
104
|
+
if ($andNew && $andNew.length > 0) newSelector.$and = $andNew;
|
|
105
|
+
let hasNonIndexField = false;
|
|
106
|
+
const matchedBefore = matched;
|
|
107
|
+
const idsBefore = ids;
|
|
108
|
+
let orIds = [];
|
|
109
|
+
let orMatched = false;
|
|
110
|
+
const process$or = ($orNew) => {
|
|
111
|
+
if (hasNonIndexField || $orNew && $orNew.length > 0) {
|
|
112
|
+
newSelector.$or = $or;
|
|
113
|
+
matched = matchedBefore;
|
|
114
|
+
ids = idsBefore;
|
|
115
|
+
} else if (orMatched) {
|
|
116
|
+
ids = matchedBefore ? require_intersection.default(idsBefore, orIds) : orIds;
|
|
117
|
+
matched = true;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
matched,
|
|
121
|
+
ids: ids || [],
|
|
122
|
+
optimizedSelector: newSelector
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
|
|
126
|
+
if (match) {
|
|
127
|
+
orIds = [...new Set([...orIds, ...selIds])];
|
|
128
|
+
orMatched = true;
|
|
129
|
+
} else hasNonIndexField = true;
|
|
130
|
+
}) : void 0;
|
|
131
|
+
if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
|
|
132
|
+
return process$or($orNewOrPromise);
|
|
133
|
+
};
|
|
134
|
+
if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
|
|
135
|
+
return process$and($andNewOrPromise);
|
|
136
|
+
};
|
|
137
|
+
if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
|
|
138
|
+
return processFlatInfo(flatInfoOrPromise);
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
exports.default = getIndexInfo;
|
package/dist/index17.mjs
CHANGED
|
@@ -1,153 +1,141 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"added",
|
|
40
|
-
"addedBefore",
|
|
41
|
-
"changed",
|
|
42
|
-
"changedField",
|
|
43
|
-
"movedBefore",
|
|
44
|
-
"removed"
|
|
45
|
-
]);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
49
|
-
* for events such as added, removed, changed, or moved items.
|
|
50
|
-
* @param getItems - A function that returns a promise resolving to the new items or the items themselves.
|
|
51
|
-
*/
|
|
52
|
-
runChecks(getItems) {
|
|
53
|
-
const result = getItems();
|
|
54
|
-
if (result instanceof Promise) {
|
|
55
|
-
result.then((newItems) => this.checkItems(newItems)).catch((error) => {
|
|
56
|
-
console.error("Error while asynchronously querying items", error);
|
|
57
|
-
});
|
|
58
|
-
} else {
|
|
59
|
-
this.checkItems(result);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
checkItems(newItems) {
|
|
63
|
-
const oldItemsMap = new Map(this.previousItems.map((item, index) => [
|
|
64
|
-
item.id,
|
|
65
|
-
{ item, index, beforeItem: this.previousItems[index + 1] || null }
|
|
66
|
-
]));
|
|
67
|
-
const newItemsMap = new Map(newItems.map((item, index) => [
|
|
68
|
-
item.id,
|
|
69
|
-
{ item, index, beforeItem: newItems[index + 1] || null }
|
|
70
|
-
]));
|
|
71
|
-
if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
|
|
72
|
-
oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
|
|
73
|
-
const newItem = newItemsMap.get(oldItem.id);
|
|
74
|
-
if (newItem) {
|
|
75
|
-
if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
|
|
76
|
-
this.call("changed", newItem.item);
|
|
77
|
-
if (this.hasCallbacks(["changedField"])) {
|
|
78
|
-
const keys = uniqueBy([
|
|
79
|
-
...Object.keys(newItem.item),
|
|
80
|
-
...Object.keys(oldItem)
|
|
81
|
-
], (value) => value);
|
|
82
|
-
keys.forEach((key) => {
|
|
83
|
-
if (isEqual(newItem.item[key], oldItem[key]))
|
|
84
|
-
return;
|
|
85
|
-
this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
|
|
90
|
-
this.call("movedBefore", newItem.item, newItem.beforeItem);
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
this.call("removed", oldItem);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
if (this.hasCallbacks(["added", "addedBefore"])) {
|
|
98
|
-
newItems.forEach((newItem, index) => {
|
|
99
|
-
const oldItem = oldItemsMap.get(newItem.id);
|
|
100
|
-
if (oldItem)
|
|
101
|
-
return;
|
|
102
|
-
this.call("added", newItem);
|
|
103
|
-
this.call("addedBefore", newItem, newItems[index + 1] || null);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
this.previousItems = newItems;
|
|
107
|
-
Object.keys(this.callbacks).forEach((key) => {
|
|
108
|
-
const event = key;
|
|
109
|
-
const callbacks = this.callbacks[event];
|
|
110
|
-
this.callbacks[event] = callbacks.map((callback) => ({
|
|
111
|
-
...callback,
|
|
112
|
-
options: {
|
|
113
|
-
...callback.options,
|
|
114
|
-
isInitial: false
|
|
115
|
-
}
|
|
116
|
-
}));
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Stops the observer by unbinding all events and cleaning up resources.
|
|
121
|
-
*/
|
|
122
|
-
stop() {
|
|
123
|
-
this.unbindEvents();
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Registers callbacks for specific events to observe changes in the collection.
|
|
127
|
-
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
128
|
-
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
129
|
-
*/
|
|
130
|
-
addCallbacks(callbacks, skipInitial = false) {
|
|
131
|
-
Object.keys(callbacks).forEach((key) => {
|
|
132
|
-
const typedKey = key;
|
|
133
|
-
this.callbacks[typedKey].push({
|
|
134
|
-
callback: callbacks[typedKey],
|
|
135
|
-
options: { skipInitial, isInitial: true }
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
141
|
-
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
142
|
-
*/
|
|
143
|
-
removeCallbacks(callbacks) {
|
|
144
|
-
Object.keys(callbacks).forEach((key) => {
|
|
145
|
-
const typedKey = key;
|
|
146
|
-
const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
|
|
147
|
-
this.callbacks[typedKey].splice(index, 1);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
1
|
+
import intersection from "./index16.mjs";
|
|
2
|
+
//#region src/getIndexInfo.ts
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves merged index information for a given flat selector by querying multiple
|
|
5
|
+
* index providers. Combines results from all index providers to determine matched ids
|
|
6
|
+
* and an optimized selector.
|
|
7
|
+
* @template T - The type of the items in the collection.
|
|
8
|
+
* @template I - The type of the unique identifier for the items.
|
|
9
|
+
* @param queryFunctions - An array of index providers to query.
|
|
10
|
+
* @param selector - The flat selector used to filter items.
|
|
11
|
+
* @returns An object containing:
|
|
12
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
13
|
+
* - `ids`: An array of matched item ids.
|
|
14
|
+
* - `optimizedSelector`: A flat selector optimized based on the index results.
|
|
15
|
+
*/
|
|
16
|
+
function getMergedIndexInfo(queryFunctions, selector) {
|
|
17
|
+
return queryFunctions.reduce((memoOrPromise, queryFunction) => {
|
|
18
|
+
const resultOrPromise = queryFunction(selector);
|
|
19
|
+
const processResult = (memo, result) => {
|
|
20
|
+
if (!result.matched) return memo;
|
|
21
|
+
const optimizedSelector = result.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
|
|
22
|
+
return {
|
|
23
|
+
matched: true,
|
|
24
|
+
ids: [...new Set(memo.matched ? intersection(memo.ids, result.ids) : result.ids)],
|
|
25
|
+
optimizedSelector
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
if (resultOrPromise instanceof Promise) return resultOrPromise.then(async (result) => {
|
|
29
|
+
return processResult(memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise, result);
|
|
30
|
+
});
|
|
31
|
+
const memo = memoOrPromise;
|
|
32
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
33
|
+
return processResult(memo, resultOrPromise);
|
|
34
|
+
}, {
|
|
35
|
+
matched: false,
|
|
36
|
+
ids: [],
|
|
37
|
+
optimizedSelector: { ...selector }
|
|
38
|
+
});
|
|
150
39
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
|
|
42
|
+
* @param queryFunctions - An array of index providers to query.
|
|
43
|
+
* @param logicGate - An array of selectors representing the logic gate.
|
|
44
|
+
* @param idsCallback - A callback function called with matched ids.
|
|
45
|
+
* @returns An array of optimized selectors or a promise that resolves to such an array.
|
|
46
|
+
*/
|
|
47
|
+
function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
|
|
48
|
+
return logicGate.reduce((memoOrPromise, sel) => {
|
|
49
|
+
const getSelector = (indexInfo) => {
|
|
50
|
+
const { matched: selMatched, ids: selIds, optimizedSelector } = indexInfo;
|
|
51
|
+
if (selMatched) {
|
|
52
|
+
idsCallback(true, selIds);
|
|
53
|
+
if (Object.keys(optimizedSelector).length > 0) return optimizedSelector;
|
|
54
|
+
} else {
|
|
55
|
+
idsCallback(false, []);
|
|
56
|
+
return sel;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
|
|
60
|
+
if (indexInfoOrPromise instanceof Promise) return indexInfoOrPromise.then(async (indexInfo) => {
|
|
61
|
+
const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
62
|
+
const optimizedSelector = getSelector(indexInfo);
|
|
63
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
64
|
+
return memo;
|
|
65
|
+
});
|
|
66
|
+
const memo = memoOrPromise;
|
|
67
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
68
|
+
const optimizedSelector = getSelector(indexInfoOrPromise);
|
|
69
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
70
|
+
return memo;
|
|
71
|
+
}, []);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves index information for a given complex selector by querying multiple
|
|
75
|
+
* index providers. Handles nested `$and` and `$or` conditions in the selector and
|
|
76
|
+
* optimizes the selector to minimize processing overhead.
|
|
77
|
+
* @template T - The type of the items in the collection.
|
|
78
|
+
* @template I - The type of the unique identifier for the items.
|
|
79
|
+
* @param queryFunctions - An array of index providers to query.
|
|
80
|
+
* @param selector - The complex selector used to filter items.
|
|
81
|
+
* @returns An object containing:
|
|
82
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
83
|
+
* - `ids`: An array of matched item ids.
|
|
84
|
+
* - `optimizedSelector`: A selector optimized based on the index results, with unused
|
|
85
|
+
* conditions removed.
|
|
86
|
+
*/
|
|
87
|
+
function getIndexInfo(queryFunctions, selector) {
|
|
88
|
+
if (selector == null || Object.keys(selector).length <= 0) return {
|
|
89
|
+
matched: false,
|
|
90
|
+
ids: [],
|
|
91
|
+
optimizedSelector: selector
|
|
92
|
+
};
|
|
93
|
+
const { $and, $or, ...rest } = selector;
|
|
94
|
+
const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
|
|
95
|
+
const processFlatInfo = (flatInfo) => {
|
|
96
|
+
let { matched, ids } = flatInfo;
|
|
97
|
+
const newSelector = flatInfo.optimizedSelector;
|
|
98
|
+
const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
|
|
99
|
+
if (!match) return;
|
|
100
|
+
ids = matched ? intersection(ids, selIds) : selIds;
|
|
101
|
+
matched = true;
|
|
102
|
+
}) : void 0;
|
|
103
|
+
const process$and = ($andNew) => {
|
|
104
|
+
if ($andNew && $andNew.length > 0) newSelector.$and = $andNew;
|
|
105
|
+
let hasNonIndexField = false;
|
|
106
|
+
const matchedBefore = matched;
|
|
107
|
+
const idsBefore = ids;
|
|
108
|
+
let orIds = [];
|
|
109
|
+
let orMatched = false;
|
|
110
|
+
const process$or = ($orNew) => {
|
|
111
|
+
if (hasNonIndexField || $orNew && $orNew.length > 0) {
|
|
112
|
+
newSelector.$or = $or;
|
|
113
|
+
matched = matchedBefore;
|
|
114
|
+
ids = idsBefore;
|
|
115
|
+
} else if (orMatched) {
|
|
116
|
+
ids = matchedBefore ? intersection(idsBefore, orIds) : orIds;
|
|
117
|
+
matched = true;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
matched,
|
|
121
|
+
ids: ids || [],
|
|
122
|
+
optimizedSelector: newSelector
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
|
|
126
|
+
if (match) {
|
|
127
|
+
orIds = [...new Set([...orIds, ...selIds])];
|
|
128
|
+
orMatched = true;
|
|
129
|
+
} else hasNonIndexField = true;
|
|
130
|
+
}) : void 0;
|
|
131
|
+
if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
|
|
132
|
+
return process$or($orNewOrPromise);
|
|
133
|
+
};
|
|
134
|
+
if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
|
|
135
|
+
return process$and($andNewOrPromise);
|
|
136
|
+
};
|
|
137
|
+
if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
|
|
138
|
+
return processFlatInfo(flatInfoOrPromise);
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
export { getIndexInfo as default };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/utils/deepClone.ts
|
|
2
|
+
/**
|
|
3
|
+
* Performs a deep clone of a value, supporting various types including arrays, objects,
|
|
4
|
+
* Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
|
|
5
|
+
* @template T - The type of the value to clone.
|
|
6
|
+
* @param value - The value to deep clone.
|
|
7
|
+
* @returns A deep copy of the provided value.
|
|
8
|
+
* @throws {Error} An error if the value is a function, as cloning functions is not supported.
|
|
9
|
+
*/
|
|
10
|
+
function clone(value) {
|
|
11
|
+
if (typeof value === "function") throw new Error("Cloning functions is not supported");
|
|
12
|
+
if (value === null || typeof value !== "object") return value;
|
|
13
|
+
if (value instanceof Date) return new Date(value);
|
|
14
|
+
if (Array.isArray(value)) return value.map((item) => clone(item));
|
|
15
|
+
if (value instanceof Map) {
|
|
16
|
+
const result = /* @__PURE__ */ new Map();
|
|
17
|
+
value.forEach((currentValue, key) => {
|
|
18
|
+
result.set(key, clone(currentValue));
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
if (value instanceof Set) {
|
|
23
|
+
const result = /* @__PURE__ */ new Set();
|
|
24
|
+
value.forEach((currentValue) => {
|
|
25
|
+
result.add(clone(currentValue));
|
|
26
|
+
});
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (value instanceof RegExp) return new RegExp(value);
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a deep clone of an object. Uses the `structuredClone` function if available,
|
|
36
|
+
* otherwise falls back to a manual deep clone implementation.
|
|
37
|
+
* @template T - The type of the object to clone.
|
|
38
|
+
* @param object - The object to deep clone.
|
|
39
|
+
* @returns A deep copy of the provided object.
|
|
40
|
+
*/
|
|
41
|
+
function deepClone(object) {
|
|
42
|
+
if (typeof structuredClone === "function") return structuredClone(object);
|
|
43
|
+
/* istanbul ignore next -- @preserve */
|
|
44
|
+
return clone(object);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.default = deepClone;
|
package/dist/index18.mjs
CHANGED
|
@@ -1,27 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
//#region src/utils/deepClone.ts
|
|
2
|
+
/**
|
|
3
|
+
* Performs a deep clone of a value, supporting various types including arrays, objects,
|
|
4
|
+
* Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
|
|
5
|
+
* @template T - The type of the value to clone.
|
|
6
|
+
* @param value - The value to deep clone.
|
|
7
|
+
* @returns A deep copy of the provided value.
|
|
8
|
+
* @throws {Error} An error if the value is a function, as cloning functions is not supported.
|
|
9
|
+
*/
|
|
10
|
+
function clone(value) {
|
|
11
|
+
if (typeof value === "function") throw new Error("Cloning functions is not supported");
|
|
12
|
+
if (value === null || typeof value !== "object") return value;
|
|
13
|
+
if (value instanceof Date) return new Date(value);
|
|
14
|
+
if (Array.isArray(value)) return value.map((item) => clone(item));
|
|
15
|
+
if (value instanceof Map) {
|
|
16
|
+
const result = /* @__PURE__ */ new Map();
|
|
17
|
+
value.forEach((currentValue, key) => {
|
|
18
|
+
result.set(key, clone(currentValue));
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
if (value instanceof Set) {
|
|
23
|
+
const result = /* @__PURE__ */ new Set();
|
|
24
|
+
value.forEach((currentValue) => {
|
|
25
|
+
result.add(clone(currentValue));
|
|
26
|
+
});
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (value instanceof RegExp) return new RegExp(value);
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
|
|
32
|
+
return result;
|
|
24
33
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Creates a deep clone of an object. Uses the `structuredClone` function if available,
|
|
36
|
+
* otherwise falls back to a manual deep clone implementation.
|
|
37
|
+
* @template T - The type of the object to clone.
|
|
38
|
+
* @param object - The object to deep clone.
|
|
39
|
+
* @returns A deep copy of the provided object.
|
|
40
|
+
*/
|
|
41
|
+
function deepClone(object) {
|
|
42
|
+
if (typeof structuredClone === "function") return structuredClone(object);
|
|
43
|
+
/* istanbul ignore next -- @preserve */
|
|
44
|
+
return clone(object);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { deepClone as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
let mingo = require("mingo");
|
|
2
|
+
//#region src/utils/match.ts
|
|
3
|
+
/**
|
|
4
|
+
* Tests whether a given item matches a specified selector.
|
|
5
|
+
* Uses the `mingo` library to evaluate the query.
|
|
6
|
+
* @template T - The type of the item being tested.
|
|
7
|
+
* @param item - The item to test against the selector.
|
|
8
|
+
* @param selector - The query selector used to match the item.
|
|
9
|
+
* @returns A boolean indicating whether the item matches the selector.
|
|
10
|
+
*/
|
|
11
|
+
function match(item, selector) {
|
|
12
|
+
return new mingo.Query(selector).test(item);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.default = match;
|
package/dist/index19.mjs
CHANGED
|
@@ -1,42 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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;
|
|
1
|
+
import { Query } from "mingo";
|
|
2
|
+
//#region src/utils/match.ts
|
|
3
|
+
/**
|
|
4
|
+
* Tests whether a given item matches a specified selector.
|
|
5
|
+
* Uses the `mingo` library to evaluate the query.
|
|
6
|
+
* @template T - The type of the item being tested.
|
|
7
|
+
* @param item - The item to test against the selector.
|
|
8
|
+
* @param selector - The query selector used to match the item.
|
|
9
|
+
* @returns A boolean indicating whether the item matches the selector.
|
|
10
|
+
*/
|
|
11
|
+
function match(item, selector) {
|
|
12
|
+
return new Query(selector).test(item);
|
|
33
13
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return structuredClone(object);
|
|
37
|
-
return clone(object);
|
|
38
|
-
}
|
|
39
|
-
export {
|
|
40
|
-
clone,
|
|
41
|
-
deepClone as default
|
|
42
|
-
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { match as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/utils/isEqual.ts
|
|
2
|
+
/**
|
|
3
|
+
* Compares two values for deep equality.
|
|
4
|
+
* @param a - The first value to compare.
|
|
5
|
+
* @param b - The second value to compare.
|
|
6
|
+
* @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
|
|
7
|
+
* @example
|
|
8
|
+
* isEqual({ a: 1 }, { a: 1 }); // true
|
|
9
|
+
* isEqual([1, 2], [1, 2]); // true
|
|
10
|
+
* isEqual(new Date(0), new Date(0)); // true
|
|
11
|
+
* isEqual(/abc/, /abc/); // true
|
|
12
|
+
* isEqual({ a: 1 }, { a: 2 }); // false
|
|
13
|
+
* isEqual(null, null); // true
|
|
14
|
+
*/
|
|
15
|
+
function isEqual(a, b) {
|
|
16
|
+
if (Object.is(a, b)) return true;
|
|
17
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
|
|
18
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
19
|
+
if (typeof a !== "object") return false;
|
|
20
|
+
if (typeof b !== "object") return false;
|
|
21
|
+
if (a === null) return false;
|
|
22
|
+
if (b === null) return false;
|
|
23
|
+
const aKeys = Object.keys(a);
|
|
24
|
+
const bKeys = Object.keys(b);
|
|
25
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
26
|
+
for (const key of aKeys) {
|
|
27
|
+
if (!bKeys.includes(key)) return false;
|
|
28
|
+
if (!isEqual(a[key], b[key])) return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
exports.default = isEqual;
|