@signaldb/core 1.0.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.
Files changed (102) hide show
  1. package/README.md +68 -0
  2. package/dist/.vite/manifest.json +222 -0
  3. package/dist/AutoFetchCollection.d.ts +60 -0
  4. package/dist/Collection/Cursor.d.ts +112 -0
  5. package/dist/Collection/Observer.d.ts +61 -0
  6. package/dist/Collection/createIndex.d.ts +15 -0
  7. package/dist/Collection/getIndexInfo.d.ts +41 -0
  8. package/dist/Collection/index.d.ts +235 -0
  9. package/dist/Collection/types.d.ts +25 -0
  10. package/dist/ReplicatedCollection.d.ts +60 -0
  11. package/dist/createIndexProvider.d.ts +8 -0
  12. package/dist/createMemoryAdapter.d.ts +7 -0
  13. package/dist/createReactivityAdapter.d.ts +8 -0
  14. package/dist/index.cjs.js +24 -0
  15. package/dist/index.cjs10.js +8 -0
  16. package/dist/index.cjs11.js +5 -0
  17. package/dist/index.cjs12.js +37 -0
  18. package/dist/index.cjs13.js +26 -0
  19. package/dist/index.cjs14.js +7 -0
  20. package/dist/index.cjs15.js +43 -0
  21. package/dist/index.cjs16.js +13 -0
  22. package/dist/index.cjs17.js +20 -0
  23. package/dist/index.cjs18.js +247 -0
  24. package/dist/index.cjs19.js +71 -0
  25. package/dist/index.cjs2.js +631 -0
  26. package/dist/index.cjs20.js +80 -0
  27. package/dist/index.cjs21.js +10 -0
  28. package/dist/index.cjs22.js +27 -0
  29. package/dist/index.cjs23.js +137 -0
  30. package/dist/index.cjs24.js +18 -0
  31. package/dist/index.cjs25.js +7 -0
  32. package/dist/index.cjs26.js +19 -0
  33. package/dist/index.cjs27.js +24 -0
  34. package/dist/index.cjs28.js +42 -0
  35. package/dist/index.cjs29.js +29 -0
  36. package/dist/index.cjs3.js +165 -0
  37. package/dist/index.cjs30.js +9 -0
  38. package/dist/index.cjs4.js +70 -0
  39. package/dist/index.cjs5.js +5 -0
  40. package/dist/index.cjs6.js +5 -0
  41. package/dist/index.cjs7.js +5 -0
  42. package/dist/index.cjs8.js +5 -0
  43. package/dist/index.cjs9.js +30 -0
  44. package/dist/index.d.ts +16 -0
  45. package/dist/index.mjs +24 -0
  46. package/dist/index10.mjs +9 -0
  47. package/dist/index11.mjs +6 -0
  48. package/dist/index12.mjs +37 -0
  49. package/dist/index13.mjs +27 -0
  50. package/dist/index14.mjs +8 -0
  51. package/dist/index15.mjs +43 -0
  52. package/dist/index16.mjs +14 -0
  53. package/dist/index17.mjs +21 -0
  54. package/dist/index18.mjs +247 -0
  55. package/dist/index19.mjs +71 -0
  56. package/dist/index2.mjs +632 -0
  57. package/dist/index20.mjs +80 -0
  58. package/dist/index21.mjs +11 -0
  59. package/dist/index22.mjs +28 -0
  60. package/dist/index23.mjs +138 -0
  61. package/dist/index24.mjs +18 -0
  62. package/dist/index25.mjs +8 -0
  63. package/dist/index26.mjs +20 -0
  64. package/dist/index27.mjs +25 -0
  65. package/dist/index28.mjs +43 -0
  66. package/dist/index29.mjs +30 -0
  67. package/dist/index3.mjs +166 -0
  68. package/dist/index30.mjs +10 -0
  69. package/dist/index4.mjs +70 -0
  70. package/dist/index5.mjs +6 -0
  71. package/dist/index6.mjs +6 -0
  72. package/dist/index7.mjs +6 -0
  73. package/dist/index8.mjs +6 -0
  74. package/dist/index9.mjs +31 -0
  75. package/dist/persistence/combinePersistenceAdapters.d.ts +32 -0
  76. package/dist/persistence/createPersistenceAdapter.d.ts +9 -0
  77. package/dist/types/Dependency.d.ts +4 -0
  78. package/dist/types/EventEmitter.d.ts +25 -0
  79. package/dist/types/IndexProvider.d.ts +19 -0
  80. package/dist/types/MemoryAdapter.d.ts +15 -0
  81. package/dist/types/Modifier.d.ts +39 -0
  82. package/dist/types/PersistenceAdapter.d.ts +20 -0
  83. package/dist/types/ReactivityAdapter.d.ts +6 -0
  84. package/dist/types/Selector.d.ts +47 -0
  85. package/dist/types/Signal.d.ts +4 -0
  86. package/dist/utils/compact.d.ts +9 -0
  87. package/dist/utils/createSignal.d.ts +14 -0
  88. package/dist/utils/deepClone.d.ts +17 -0
  89. package/dist/utils/get.d.ts +9 -0
  90. package/dist/utils/getMatchingKeys.d.ts +14 -0
  91. package/dist/utils/intersection.d.ts +9 -0
  92. package/dist/utils/isEqual.d.ts +14 -0
  93. package/dist/utils/isFieldExpression.d.ts +11 -0
  94. package/dist/utils/match.d.ts +12 -0
  95. package/dist/utils/modify.d.ts +14 -0
  96. package/dist/utils/project.d.ts +15 -0
  97. package/dist/utils/randomId.d.ts +7 -0
  98. package/dist/utils/serializeValue.d.ts +12 -0
  99. package/dist/utils/set.d.ts +13 -0
  100. package/dist/utils/sortItems.d.ts +12 -0
  101. package/dist/utils/uniqueBy.d.ts +10 -0
  102. package/package.json +58 -0
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ function isEqual(a, b) {
3
+ if (Object.is(a, b))
4
+ return true;
5
+ if (a instanceof RegExp && b instanceof RegExp)
6
+ return a.toString() === b.toString();
7
+ if (a instanceof Date && b instanceof Date)
8
+ return a.getTime() === b.getTime();
9
+ if (typeof a !== "object")
10
+ return false;
11
+ if (typeof b !== "object")
12
+ return false;
13
+ if (a === null)
14
+ return false;
15
+ if (b === null)
16
+ return false;
17
+ const aKeys = Object.keys(a);
18
+ const bKeys = Object.keys(b);
19
+ if (aKeys.length !== bKeys.length)
20
+ return false;
21
+ for (let i = 0; i < aKeys.length; i += 1) {
22
+ const key = aKeys[i];
23
+ if (!bKeys.includes(key))
24
+ return false;
25
+ if (!isEqual(a[key], b[key]))
26
+ return false;
27
+ }
28
+ return true;
29
+ }
30
+ module.exports = isEqual;
@@ -0,0 +1,16 @@
1
+ export type { default as ReactivityAdapter } from './types/ReactivityAdapter';
2
+ export type { default as MemoryAdapter } from './types/MemoryAdapter';
3
+ export type { default as PersistenceAdapter, Changeset, LoadResponse, } from './types/PersistenceAdapter';
4
+ export type { default as Selector } from './types/Selector';
5
+ export type { default as Modifier } from './types/Modifier';
6
+ export type { BaseItem, ObserveCallbacks, CursorOptions, Transform, SortSpecifier, FieldSpecifier, FindOptions, } from './Collection';
7
+ export { default as Collection, createIndex } from './Collection';
8
+ export { default as AutoFetchCollection } from './AutoFetchCollection';
9
+ export { default as combinePersistenceAdapters } from './persistence/combinePersistenceAdapters';
10
+ export { default as createIndexProvider } from './createIndexProvider';
11
+ export { default as createMemoryAdapter } from './createMemoryAdapter';
12
+ export { default as createPersistenceAdapter } from './persistence/createPersistenceAdapter';
13
+ export { default as createReactivityAdapter } from './createReactivityAdapter';
14
+ export { default as isEqual } from './utils/isEqual';
15
+ export { default as modify } from './utils/modify';
16
+ export { default as randomId } from './utils/randomId';
package/dist/index.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import { default as default2 } from "./index2.mjs";
2
+ import { default as default3 } from "./index3.mjs";
3
+ import { default as default4 } from "./index4.mjs";
4
+ import { default as default5 } from "./index5.mjs";
5
+ import { default as default6 } from "./index6.mjs";
6
+ import { default as default7 } from "./index7.mjs";
7
+ import { default as default8 } from "./index8.mjs";
8
+ import { default as default9 } from "./index9.mjs";
9
+ import { default as default10 } from "./index10.mjs";
10
+ import { default as default11 } from "./index11.mjs";
11
+ import { default as default12 } from "./index12.mjs";
12
+ export {
13
+ default3 as AutoFetchCollection,
14
+ default2 as Collection,
15
+ default4 as combinePersistenceAdapters,
16
+ default12 as createIndex,
17
+ default5 as createIndexProvider,
18
+ default6 as createMemoryAdapter,
19
+ default7 as createPersistenceAdapter,
20
+ default8 as createReactivityAdapter,
21
+ default9 as isEqual,
22
+ default10 as modify,
23
+ default11 as randomId
24
+ };
@@ -0,0 +1,9 @@
1
+ import { updateObject } from "mingo/updater";
2
+ function modify(item, modifier) {
3
+ const clonedItem = Object.assign({}, item);
4
+ updateObject(clonedItem, modifier);
5
+ return clonedItem;
6
+ }
7
+ export {
8
+ modify as default
9
+ };
@@ -0,0 +1,6 @@
1
+ function randomId() {
2
+ return Math.floor(Math.random() * 1e17).toString(16);
3
+ }
4
+ export {
5
+ randomId as default
6
+ };
@@ -0,0 +1,37 @@
1
+ import createIndexProvider from "./index5.mjs";
2
+ import get from "./index26.mjs";
3
+ import getMatchingKeys from "./index27.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 == 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 Object.assign(Object.assign({}, createExternalIndex(field, index)), { rebuild(items) {
25
+ index.clear();
26
+ items.forEach((item, i) => {
27
+ const value = serializeValue(get(item, field));
28
+ const current = index.get(value) || /* @__PURE__ */ new Set();
29
+ current.add(i);
30
+ index.set(value, current);
31
+ });
32
+ } });
33
+ }
34
+ export {
35
+ createExternalIndex,
36
+ createIndex as default
37
+ };
@@ -0,0 +1,27 @@
1
+ import { EventEmitter as EventEmitter$1 } from "events";
2
+ class EventEmitter extends EventEmitter$1 {
3
+ /**
4
+ * Registers a listener for the specified event.
5
+ * @template K - The key of the event in the `Events` record.
6
+ * @param event - The name of the event to listen for.
7
+ * @param listener - The listener function to execute when the event is emitted.
8
+ * @returns The `EventEmitter` instance, allowing for method chaining.
9
+ */
10
+ on(event, listener) {
11
+ super.on(event, listener);
12
+ return this;
13
+ }
14
+ /**
15
+ * Emits the specified event, triggering all registered listeners for that event.
16
+ * @template K - The key of the event in the `Events` record.
17
+ * @param event - The name of the event to emit.
18
+ * @param args - The arguments to pass to the event listeners.
19
+ * @returns A boolean indicating whether any listeners were triggered.
20
+ */
21
+ emit(event, ...args) {
22
+ return super.emit(event, ...args);
23
+ }
24
+ }
25
+ export {
26
+ EventEmitter as default
27
+ };
@@ -0,0 +1,8 @@
1
+ import { Query } from "mingo";
2
+ function match(item, selector) {
3
+ const query = new Query(selector);
4
+ return query.test(item);
5
+ }
6
+ export {
7
+ match as default
8
+ };
@@ -0,0 +1,43 @@
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((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
+ }
40
+ export {
41
+ clone,
42
+ deepClone as default
43
+ };
@@ -0,0 +1,14 @@
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);
11
+ }
12
+ export {
13
+ serializeValue as default
14
+ };
@@ -0,0 +1,21 @@
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();
15
+ }
16
+ };
17
+ return signal;
18
+ }
19
+ export {
20
+ createSignal as default
21
+ };
@@ -0,0 +1,247 @@
1
+ import sortItems from "./index21.mjs";
2
+ import project from "./index22.mjs";
3
+ import Observer from "./index23.mjs";
4
+ function isInReactiveScope(reactivity) {
5
+ if (!reactivity)
6
+ return false;
7
+ if (!reactivity.isInScope)
8
+ return true;
9
+ return reactivity.isInScope();
10
+ }
11
+ class Cursor {
12
+ /**
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
+ }
243
+ }
244
+ export {
245
+ Cursor as default,
246
+ isInReactiveScope
247
+ };
@@ -0,0 +1,71 @@
1
+ import { __rest } from "./index24.mjs";
2
+ import intersection from "./index25.mjs";
3
+ function getMergedIndexInfo(indexProviders, selector) {
4
+ return indexProviders.reduce((memo, indexProvider) => {
5
+ const info = indexProvider.query(selector);
6
+ if (!info.matched)
7
+ return memo;
8
+ const optimizedSelector = Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
9
+ return {
10
+ matched: true,
11
+ positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
12
+ optimizedSelector
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
+ }
58
+ }
59
+ if ($orNew.length > 0)
60
+ newSelector.$or = $orNew;
61
+ }
62
+ return {
63
+ matched,
64
+ positions: positions || [],
65
+ optimizedSelector: newSelector
66
+ };
67
+ }
68
+ export {
69
+ getIndexInfo as default,
70
+ getMergedIndexInfo
71
+ };