@signaldb/core 2.0.0-beta.6 → 2.0.0-beta.7

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 (104) hide show
  1. package/dist/.vite/manifest.json +118 -84
  2. package/dist/AsyncDataAdapter.d.ts +1 -1
  3. package/dist/index.cjs.js +33 -34
  4. package/dist/index.mjs +17 -35
  5. package/dist/index10.cjs.js +23 -0
  6. package/dist/index10.mjs +21 -17
  7. package/dist/index11.cjs.js +45 -0
  8. package/dist/index11.mjs +44 -15
  9. package/dist/index12.cjs.js +22 -0
  10. package/dist/index12.mjs +21 -47
  11. package/dist/index13.cjs.js +45 -0
  12. package/dist/index13.mjs +44 -365
  13. package/dist/index14.cjs.js +86 -0
  14. package/dist/index14.mjs +85 -416
  15. package/dist/index15.cjs.js +15 -0
  16. package/dist/index15.mjs +14 -206
  17. package/dist/index16.cjs.js +136 -0
  18. package/dist/index16.mjs +135 -388
  19. package/dist/index17.cjs.js +47 -0
  20. package/dist/index17.mjs +45 -578
  21. package/dist/index18.cjs.js +15 -0
  22. package/dist/index18.mjs +14 -157
  23. package/dist/index19.cjs.js +23 -0
  24. package/dist/index19.mjs +22 -26
  25. package/dist/index2.cjs.js +33 -0
  26. package/dist/index2.mjs +32 -258
  27. package/dist/index20.cjs.js +37 -0
  28. package/dist/index20.mjs +36 -41
  29. package/dist/index21.cjs.js +35 -0
  30. package/dist/index21.mjs +34 -16
  31. package/dist/index22.cjs.js +25 -0
  32. package/dist/index22.mjs +24 -104
  33. package/dist/index23.cjs.js +19 -0
  34. package/dist/index23.mjs +18 -128
  35. package/dist/index24.cjs.js +342 -0
  36. package/dist/index24.mjs +340 -6
  37. package/dist/index25.cjs.js +519 -0
  38. package/dist/index25.mjs +518 -27
  39. package/dist/index26.cjs.js +11 -0
  40. package/dist/index26.mjs +10 -10
  41. package/dist/index27.cjs.js +11 -0
  42. package/dist/index27.mjs +10 -29
  43. package/dist/index28.cjs.js +90 -0
  44. package/dist/index28.mjs +88 -46
  45. package/dist/index29.cjs.js +370 -0
  46. package/dist/index29.mjs +369 -8
  47. package/dist/index3.cjs.js +19 -0
  48. package/dist/index3.mjs +18 -539
  49. package/dist/index30.cjs.js +72 -0
  50. package/dist/index30.mjs +71 -9
  51. package/dist/index31.cjs.js +191 -0
  52. package/dist/index31.mjs +190 -7
  53. package/dist/index32.cjs.js +21 -0
  54. package/dist/index32.mjs +20 -42
  55. package/dist/index33.cjs.js +336 -0
  56. package/dist/index33.mjs +335 -29
  57. package/dist/index34.cjs.js +539 -0
  58. package/dist/index34.mjs +537 -4
  59. package/dist/index4.cjs.js +150 -0
  60. package/dist/index4.mjs +149 -5
  61. package/dist/index5.cjs.js +254 -0
  62. package/dist/index5.mjs +252 -4
  63. package/dist/index6.cjs.js +125 -0
  64. package/dist/index6.mjs +124 -29
  65. package/dist/index7.cjs.js +33 -0
  66. package/dist/index7.mjs +32 -12
  67. package/dist/index8.cjs.js +12 -0
  68. package/dist/index8.mjs +10 -4
  69. package/dist/index9.cjs.js +11 -0
  70. package/dist/index9.mjs +10 -133
  71. package/package.json +1 -1
  72. package/dist/index.cjs10.js +0 -18
  73. package/dist/index.cjs11.js +0 -15
  74. package/dist/index.cjs12.js +0 -48
  75. package/dist/index.cjs13.js +0 -365
  76. package/dist/index.cjs14.js +0 -416
  77. package/dist/index.cjs15.js +0 -206
  78. package/dist/index.cjs16.js +0 -388
  79. package/dist/index.cjs17.js +0 -579
  80. package/dist/index.cjs18.js +0 -157
  81. package/dist/index.cjs19.js +0 -26
  82. package/dist/index.cjs2.js +0 -259
  83. package/dist/index.cjs20.js +0 -42
  84. package/dist/index.cjs21.js +0 -16
  85. package/dist/index.cjs22.js +0 -104
  86. package/dist/index.cjs23.js +0 -129
  87. package/dist/index.cjs24.js +0 -7
  88. package/dist/index.cjs25.js +0 -27
  89. package/dist/index.cjs26.js +0 -10
  90. package/dist/index.cjs27.js +0 -29
  91. package/dist/index.cjs28.js +0 -46
  92. package/dist/index.cjs29.js +0 -8
  93. package/dist/index.cjs3.js +0 -540
  94. package/dist/index.cjs30.js +0 -9
  95. package/dist/index.cjs31.js +0 -7
  96. package/dist/index.cjs32.js +0 -42
  97. package/dist/index.cjs33.js +0 -29
  98. package/dist/index.cjs34.js +0 -5
  99. package/dist/index.cjs4.js +0 -5
  100. package/dist/index.cjs5.js +0 -5
  101. package/dist/index.cjs6.js +0 -29
  102. package/dist/index.cjs7.js +0 -12
  103. package/dist/index.cjs8.js +0 -5
  104. package/dist/index.cjs9.js +0 -133
package/dist/index15.mjs CHANGED
@@ -1,207 +1,15 @@
1
- import queryId from "./index21.mjs";
2
- import randomId from "./index8.mjs";
3
- import batchOnNextTick from "./index28.mjs";
4
- class WorkerDataAdapter {
5
- worker;
6
- options;
7
- id;
8
- isDisposed = false;
9
- workerReady;
10
- log = () => {
11
- };
12
- collectionReady = /* @__PURE__ */ new Map();
13
- batchExecutionHelpers = /* @__PURE__ */ new Map();
14
- queries = {};
15
- constructor(worker, options) {
16
- this.worker = worker;
17
- this.options = options;
18
- this.id = this.options.id || "default-worker-data-adapter";
19
- if (this.options.log)
20
- this.log = this.options.log;
21
- this.workerReady = new Promise((resolve, reject) => {
22
- const timeoutId = setTimeout(() => {
23
- reject(new Error("WorkerDataAdapter initialization timed out"));
24
- }, 5e3);
25
- const handleMessage = (event) => {
26
- const { type, workerId } = event.data;
27
- if (workerId !== this.id)
28
- return;
29
- if (type === "ready") {
30
- resolve();
31
- clearTimeout(timeoutId);
32
- this.worker.removeEventListener("message", handleMessage);
33
- }
34
- };
35
- this.worker.addEventListener("message", handleMessage);
36
- });
37
- }
38
- async exec(method, collectionName, ...args) {
39
- await this.workerReady;
40
- if (method !== "isReady") {
41
- const collectionReady = this.collectionReady.get(collectionName);
42
- if (!collectionReady)
43
- throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
44
- await collectionReady;
45
- }
46
- if (this.isDisposed) {
47
- throw new Error("WorkerDataAdapter is disposed");
48
- }
49
- return new Promise((resolve, reject) => {
50
- const messageId = randomId();
51
- const handleMessage = (event) => {
52
- const { id, workerId, type, data, error } = event.data;
53
- if (workerId !== this.id)
54
- return;
55
- if (type !== "response")
56
- return;
57
- if (id !== messageId)
58
- return;
59
- this.log(method, "result", data ?? error);
60
- if (error) {
61
- reject(error);
62
- } else {
63
- resolve(data);
64
- }
65
- this.worker.removeEventListener("message", handleMessage);
66
- };
67
- this.worker.addEventListener("message", handleMessage);
68
- this.worker.postMessage({
69
- id: messageId,
70
- workerId: this.id,
71
- method,
72
- args: [collectionName, ...args]
73
- });
74
- });
75
- }
76
- enqueueBatched(collectionName, method, args) {
77
- const helper = this.batchExecutionHelpers.get(collectionName);
78
- if (!helper)
79
- throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
80
- return helper.enqueue(method, args);
81
- }
82
- // ---------- end batching integration ----------
83
- updateQuery(collectionName, query, update) {
84
- const id = queryId(query.selector, query.options);
85
- const collectionQueries = this.queries[collectionName];
86
- if (!collectionQueries)
87
- return;
88
- const existing = collectionQueries.get(id);
89
- const newState = {
90
- state: "active",
91
- error: null,
92
- items: [],
93
- stateChangeCallbacks: [],
94
- eventHandler: existing?.eventHandler,
95
- ...existing,
96
- ...update
97
- };
98
- collectionQueries.set(id, newState);
99
- this.queries[collectionName] = collectionQueries;
100
- }
101
- createCollectionBackend(collection, indices) {
102
- this.queries[collection.name] = /* @__PURE__ */ new Map();
103
- void this.exec("registerCollection", collection.name, indices);
104
- this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
105
- this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
106
- return {
107
- insert: async (item) => {
108
- return this.enqueueBatched(collection.name, "insert", [item]);
109
- },
110
- updateOne: async (selector, modifier) => {
111
- return this.enqueueBatched(collection.name, "updateOne", [selector, modifier]);
112
- },
113
- updateMany: async (selector, modifier) => {
114
- return this.enqueueBatched(collection.name, "updateMany", [selector, modifier]);
115
- },
116
- replaceOne: async (selector, replacement) => {
117
- return this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]);
118
- },
119
- removeOne: async (selector) => {
120
- return this.enqueueBatched(collection.name, "removeOne", [selector]);
121
- },
122
- removeMany: async (selector) => {
123
- return this.enqueueBatched(collection.name, "removeMany", [selector]);
124
- },
125
- // methods for registering and unregistering queries that will be called from the collection during find/findOne
126
- registerQuery: (selector, options) => {
127
- this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
128
- void this.exec("registerQuery", collection.name, selector, options);
129
- const handler = (event) => {
130
- const { type, data, workerId, error } = event.data;
131
- if (type !== "queryUpdate")
132
- return;
133
- if (data == null)
134
- return;
135
- const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
136
- if (workerId !== this.id)
137
- return;
138
- if (collectionName !== collection.name)
139
- return;
140
- if (queryId(responseSelector, responseOptions) !== queryId(selector, options))
141
- return;
142
- this.log("queryUpdate", responseSelector, responseOptions, state, data ?? error);
143
- this.updateQuery(collection.name, {
144
- selector: responseSelector,
145
- options: responseOptions
146
- }, { state, error, items });
147
- const query = this.queries[collection.name]?.get(queryId(selector, options));
148
- if (!query)
149
- return;
150
- query.stateChangeCallbacks.forEach((callback) => callback(state));
151
- };
152
- this.worker.addEventListener("message", handler);
153
- this.updateQuery(collection.name, { selector, options }, { eventHandler: handler });
154
- },
155
- unregisterQuery: (selector, options) => {
156
- const qid = queryId(selector, options);
157
- const query = this.queries[collection.name]?.get(qid);
158
- if (query?.eventHandler) {
159
- this.worker.removeEventListener("message", query.eventHandler);
160
- }
161
- this.queries[collection.name]?.delete(qid);
162
- void this.exec("unregisterQuery", collection.name, selector, options);
163
- },
164
- getQueryState: (selector, options) => {
165
- const query = this.queries[collection.name]?.get(queryId(selector, options));
166
- return query?.state || "active";
167
- },
168
- getQueryError: (selector, options) => {
169
- const query = this.queries[collection.name]?.get(queryId(selector, options));
170
- return query?.error || null;
171
- },
172
- getQueryResult: (selector, options) => {
173
- const query = this.queries[collection.name]?.get(queryId(selector, options));
174
- return query?.items || [];
175
- },
176
- onQueryStateChange: (selector, options, callback) => {
177
- this.updateQuery(collection.name, { selector, options }, {
178
- stateChangeCallbacks: [
179
- ...this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks || [],
180
- callback
181
- ]
182
- });
183
- return () => {
184
- const currentCallbacks = this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks;
185
- if (!currentCallbacks)
186
- throw new Error("State change callbacks are not defined!");
187
- this.updateQuery(collection.name, { selector, options }, {
188
- stateChangeCallbacks: currentCallbacks.filter((existingCallback) => existingCallback !== callback)
189
- });
190
- };
191
- },
192
- executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
193
- // lifecycle methods
194
- dispose: async () => {
195
- await this.exec("unregisterCollection", collection.name);
196
- this.isDisposed = true;
197
- this.worker.terminate();
198
- },
199
- isReady: async () => {
200
- await this.exec("isReady", collection.name);
201
- }
202
- };
203
- }
1
+ //#region src/utils/intersection.ts
2
+ /**
3
+ * Computes the intersection of multiple arrays, returning an array of unique elements
4
+ * that are present in all the input arrays.
5
+ * @template T - The type of elements in the arrays.
6
+ * @param arrays - A variable number of arrays to compute the intersection of.
7
+ * @returns An array containing the unique elements found in all the input arrays.
8
+ * - If no arrays are provided, returns an empty array.
9
+ */
10
+ function intersection(...arrays) {
11
+ if (arrays.length === 0) return [];
12
+ return [...new Set(arrays.reduce((a, b) => a.filter((c) => b.includes(c))))];
204
13
  }
205
- export {
206
- WorkerDataAdapter as default
207
- };
14
+ //#endregion
15
+ export { intersection as default };
@@ -0,0 +1,136 @@
1
+ const require_intersection = require("./index15.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
+ const process$or = ($orNew) => {
109
+ if (hasNonIndexField || $orNew && $orNew.length > 0) {
110
+ newSelector.$or = $or;
111
+ matched = matchedBefore;
112
+ ids = idsBefore;
113
+ }
114
+ return {
115
+ matched,
116
+ ids: ids || [],
117
+ optimizedSelector: newSelector
118
+ };
119
+ };
120
+ const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
121
+ if (match) {
122
+ ids = [...new Set([...ids, ...selIds])];
123
+ matched = true;
124
+ } else hasNonIndexField = true;
125
+ }) : void 0;
126
+ if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
127
+ return process$or($orNewOrPromise);
128
+ };
129
+ if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
130
+ return process$and($andNewOrPromise);
131
+ };
132
+ if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
133
+ return processFlatInfo(flatInfoOrPromise);
134
+ }
135
+ //#endregion
136
+ exports.default = getIndexInfo;