@signaldb/core 2.0.0-beta.1 → 2.0.0-beta.10

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 (129) hide show
  1. package/dist/.vite/manifest.json +122 -82
  2. package/dist/AsyncDataAdapter.d.ts +44 -7
  3. package/dist/AutoFetchDataAdapter.d.ts +5 -7
  4. package/dist/Collection/Cursor.d.ts +6 -5
  5. package/dist/Collection/Observer.d.ts +2 -0
  6. package/dist/Collection/index.d.ts +34 -25
  7. package/dist/Collection/types.d.ts +10 -2
  8. package/dist/DataAdapter.d.ts +12 -6
  9. package/dist/DefaultDataAdapter.d.ts +6 -6
  10. package/dist/WorkerDataAdapter.d.ts +3 -5
  11. package/dist/WorkerDataAdapterHost.d.ts +5 -5
  12. package/dist/createIndex.d.ts +2 -2
  13. package/dist/createIndexProvider.d.ts +2 -2
  14. package/dist/createReactivityAdapter.d.ts +2 -2
  15. package/dist/createStorageAdapter.d.ts +1 -1
  16. package/dist/getIndexInfo.d.ts +3 -4
  17. package/dist/index.cjs.js +33 -31
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.mjs +17 -32
  20. package/dist/index10.cjs.js +23 -0
  21. package/dist/index10.mjs +21 -17
  22. package/dist/index11.cjs.js +45 -0
  23. package/dist/index11.mjs +44 -15
  24. package/dist/index12.cjs.js +22 -0
  25. package/dist/index12.mjs +21 -350
  26. package/dist/index13.cjs.js +45 -0
  27. package/dist/index13.mjs +44 -419
  28. package/dist/index14.cjs.js +86 -0
  29. package/dist/index14.mjs +85 -184
  30. package/dist/index15.cjs.js +15 -0
  31. package/dist/index15.mjs +14 -386
  32. package/dist/index16.cjs.js +136 -0
  33. package/dist/index16.mjs +133 -577
  34. package/dist/index17.cjs.js +47 -0
  35. package/dist/index17.mjs +46 -150
  36. package/dist/index18.cjs.js +15 -0
  37. package/dist/index18.mjs +14 -26
  38. package/dist/index19.cjs.js +23 -0
  39. package/dist/index19.mjs +22 -42
  40. package/dist/index2.cjs.js +33 -0
  41. package/dist/index2.mjs +32 -257
  42. package/dist/index20.cjs.js +37 -0
  43. package/dist/index20.mjs +36 -7
  44. package/dist/index21.cjs.js +35 -0
  45. package/dist/index21.mjs +33 -103
  46. package/dist/index22.cjs.js +25 -0
  47. package/dist/index22.mjs +23 -127
  48. package/dist/index23.cjs.js +19 -0
  49. package/dist/index23.mjs +18 -7
  50. package/dist/index24.cjs.js +342 -0
  51. package/dist/index24.mjs +340 -26
  52. package/dist/index25.cjs.js +542 -0
  53. package/dist/index25.mjs +541 -10
  54. package/dist/index26.cjs.js +11 -0
  55. package/dist/index26.mjs +10 -29
  56. package/dist/index27.cjs.js +11 -0
  57. package/dist/index27.mjs +10 -46
  58. package/dist/index28.cjs.js +90 -0
  59. package/dist/index28.mjs +87 -7
  60. package/dist/index29.cjs.js +424 -0
  61. package/dist/index29.mjs +423 -9
  62. package/dist/index3.cjs.js +19 -0
  63. package/dist/index3.mjs +18 -541
  64. package/dist/index30.cjs.js +72 -0
  65. package/dist/index30.mjs +71 -5
  66. package/dist/index31.cjs.js +191 -0
  67. package/dist/index31.mjs +190 -7
  68. package/dist/index32.cjs.js +21 -0
  69. package/dist/index32.mjs +20 -29
  70. package/dist/index33.cjs.js +336 -0
  71. package/dist/index33.mjs +335 -42
  72. package/dist/index34.cjs.js +539 -0
  73. package/dist/index34.mjs +539 -0
  74. package/dist/index4.cjs.js +150 -0
  75. package/dist/index4.mjs +149 -5
  76. package/dist/index5.cjs.js +254 -0
  77. package/dist/index5.mjs +252 -4
  78. package/dist/index6.cjs.js +125 -0
  79. package/dist/index6.mjs +124 -29
  80. package/dist/index7.cjs.js +33 -0
  81. package/dist/index7.mjs +32 -12
  82. package/dist/index8.cjs.js +12 -0
  83. package/dist/index8.mjs +10 -4
  84. package/dist/index9.cjs.js +11 -0
  85. package/dist/index9.mjs +10 -133
  86. package/dist/types/IndexProvider.d.ts +2 -2
  87. package/dist/types/Modifier.d.ts +1 -1
  88. package/dist/types/ReactivityAdapter.d.ts +1 -1
  89. package/dist/utils/createSignal.d.ts +2 -2
  90. package/dist/utils/deepClone.d.ts +1 -1
  91. package/dist/utils/getMatchingKeys.d.ts +2 -2
  92. package/dist/utils/isFieldExpression.d.ts +1 -1
  93. package/dist/utils/match.d.ts +1 -1
  94. package/dist/utils/modify.d.ts +1 -1
  95. package/dist/utils/queryId.d.ts +2 -2
  96. package/dist/utils/reactiveOrAsync.d.ts +59 -0
  97. package/package.json +3 -3
  98. package/dist/index.cjs10.js +0 -18
  99. package/dist/index.cjs11.js +0 -15
  100. package/dist/index.cjs12.js +0 -350
  101. package/dist/index.cjs13.js +0 -419
  102. package/dist/index.cjs14.js +0 -184
  103. package/dist/index.cjs15.js +0 -386
  104. package/dist/index.cjs16.js +0 -579
  105. package/dist/index.cjs17.js +0 -150
  106. package/dist/index.cjs18.js +0 -26
  107. package/dist/index.cjs19.js +0 -43
  108. package/dist/index.cjs2.js +0 -258
  109. package/dist/index.cjs20.js +0 -7
  110. package/dist/index.cjs21.js +0 -104
  111. package/dist/index.cjs22.js +0 -129
  112. package/dist/index.cjs23.js +0 -7
  113. package/dist/index.cjs24.js +0 -27
  114. package/dist/index.cjs25.js +0 -10
  115. package/dist/index.cjs26.js +0 -29
  116. package/dist/index.cjs27.js +0 -46
  117. package/dist/index.cjs28.js +0 -8
  118. package/dist/index.cjs29.js +0 -9
  119. package/dist/index.cjs3.js +0 -542
  120. package/dist/index.cjs30.js +0 -5
  121. package/dist/index.cjs31.js +0 -7
  122. package/dist/index.cjs32.js +0 -29
  123. package/dist/index.cjs33.js +0 -42
  124. package/dist/index.cjs4.js +0 -5
  125. package/dist/index.cjs5.js +0 -5
  126. package/dist/index.cjs6.js +0 -29
  127. package/dist/index.cjs7.js +0 -12
  128. package/dist/index.cjs8.js +0 -5
  129. package/dist/index.cjs9.js +0 -133
package/dist/index14.mjs CHANGED
@@ -1,185 +1,86 @@
1
- import queryId from "./index20.mjs";
2
- import randomId from "./index8.mjs";
3
- import batchOnNextTick from "./index27.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
- ...existing,
94
- ...update
95
- };
96
- collectionQueries.set(id, newState);
97
- this.queries[collectionName] = collectionQueries;
98
- }
99
- createCollectionBackend(collection, indices = []) {
100
- this.queries[collection.name] = /* @__PURE__ */ new Map();
101
- void this.exec("registerCollection", collection.name, indices);
102
- this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
103
- this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
104
- return {
105
- insert: async (item) => {
106
- return this.enqueueBatched(collection.name, "insert", [item]);
107
- },
108
- updateOne: async (selector, modifier) => {
109
- return this.enqueueBatched(collection.name, "updateOne", [selector, modifier]);
110
- },
111
- updateMany: async (selector, modifier) => {
112
- return this.enqueueBatched(collection.name, "updateMany", [selector, modifier]);
113
- },
114
- replaceOne: async (selector, replacement) => {
115
- return this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]);
116
- },
117
- removeOne: async (selector) => {
118
- return this.enqueueBatched(collection.name, "removeOne", [selector]);
119
- },
120
- removeMany: async (selector) => {
121
- return this.enqueueBatched(collection.name, "removeMany", [selector]);
122
- },
123
- // methods for registering and unregistering queries that will be called from the collection during find/findOne
124
- registerQuery: (selector, options) => {
125
- this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
126
- void this.exec("registerQuery", collection.name, selector, options);
127
- },
128
- unregisterQuery: (selector, options) => {
129
- this.queries[collection.name]?.delete(queryId(selector, options));
130
- void this.exec("unregisterQuery", collection.name, selector, options);
131
- },
132
- getQueryState: (selector, options) => {
133
- const query = this.queries[collection.name]?.get(queryId(selector, options));
134
- return query?.state || "active";
135
- },
136
- getQueryError: (selector, options) => {
137
- const query = this.queries[collection.name]?.get(queryId(selector, options));
138
- return query?.error || null;
139
- },
140
- getQueryResult: (selector, options) => {
141
- const query = this.queries[collection.name]?.get(queryId(selector, options));
142
- return query?.items || [];
143
- },
144
- onQueryStateChange: (selector, options, callback) => {
145
- const handler = (event) => {
146
- const { type, data, workerId, error } = event.data;
147
- if (type !== "queryUpdate")
148
- return;
149
- if (data == null)
150
- return;
151
- const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
152
- if (workerId !== this.id)
153
- return;
154
- if (collectionName !== collection.name)
155
- return;
156
- if (queryId(responseSelector, responseOptions) !== queryId(selector, options))
157
- return;
158
- this.log("queryUpdate", responseSelector, responseOptions, state, data ?? error);
159
- this.updateQuery(collection.name, {
160
- selector: responseSelector,
161
- options: responseOptions
162
- }, { state, error, items });
163
- callback(state);
164
- };
165
- this.worker.addEventListener("message", handler);
166
- return () => {
167
- this.worker.removeEventListener("message", handler);
168
- };
169
- },
170
- executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
171
- // lifecycle methods
172
- dispose: async () => {
173
- await this.exec("unregisterCollection", collection.name);
174
- this.isDisposed = true;
175
- this.worker.terminate();
176
- },
177
- isReady: async () => {
178
- await this.exec("isReady", collection.name);
179
- }
180
- };
181
- }
1
+ import createIndexProvider from "./index9.mjs";
2
+ import get from "./index10.mjs";
3
+ import serializeValue from "./index12.mjs";
4
+ import getMatchingKeys from "./index13.mjs";
5
+ //#region src/createIndex.ts
6
+ /**
7
+ * creates an index for a specific field
8
+ * @param field name of the field
9
+ * @returns an index provider to pass to the `indices` option of the collection constructor
10
+ */
11
+ function createIndex(field) {
12
+ const index = /* @__PURE__ */ new Map();
13
+ const ensureSet = (key) => {
14
+ let set = index.get(key);
15
+ if (!set) {
16
+ set = /* @__PURE__ */ new Set();
17
+ index.set(key, set);
18
+ }
19
+ return set;
20
+ };
21
+ return createIndexProvider({
22
+ query(selector) {
23
+ if (!Object.hasOwnProperty.call(selector, field)) return { matched: false };
24
+ const fieldSelector = selector[field];
25
+ const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
26
+ const keys = filteresForNull ? {
27
+ include: null,
28
+ exclude: [...index.keys()].filter((key) => key != null)
29
+ } : getMatchingKeys(field, selector);
30
+ if (keys.include == null && keys.exclude == null) return { matched: false };
31
+ let includedIds = [];
32
+ if (keys.include == null) for (const set of index.values()) for (const pos of set) includedIds.push(pos);
33
+ else for (const key of keys.include) {
34
+ const posSet = index.get(key);
35
+ if (posSet) for (const pos of posSet) includedIds.push(pos);
36
+ }
37
+ if (keys.exclude != null) {
38
+ const excludeIds = /* @__PURE__ */ new Set();
39
+ for (const key of keys.exclude) {
40
+ const posSet = index.get(key);
41
+ if (posSet) for (const pos of posSet) excludeIds.add(pos);
42
+ }
43
+ includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
44
+ }
45
+ return {
46
+ matched: true,
47
+ ids: includedIds,
48
+ fields: [field],
49
+ keepSelector: filteresForNull
50
+ };
51
+ },
52
+ rebuild(items) {
53
+ index.clear();
54
+ items.forEach((item) => {
55
+ ensureSet(serializeValue(get(item, field))).add(item.id);
56
+ });
57
+ },
58
+ insert(items) {
59
+ for (const item of items) ensureSet(serializeValue(get(item, field))).add(item.id);
60
+ },
61
+ remove(items) {
62
+ for (const item of items) {
63
+ const value = serializeValue(get(item, field));
64
+ const set = index.get(value);
65
+ if (!set) continue;
66
+ set.delete(item.id);
67
+ if (set.size === 0) index.delete(value);
68
+ }
69
+ },
70
+ update(pairs) {
71
+ for (const { oldItem, newItem } of pairs) {
72
+ const oldValue = serializeValue(get(oldItem, field));
73
+ const newValue = serializeValue(get(newItem, field));
74
+ if (oldValue === newValue) continue;
75
+ const oldSet = index.get(oldValue);
76
+ if (oldSet) {
77
+ oldSet.delete(oldItem.id);
78
+ if (oldSet.size === 0) index.delete(oldValue);
79
+ }
80
+ ensureSet(newValue).add(newItem.id);
81
+ }
82
+ }
83
+ });
182
84
  }
183
- export {
184
- WorkerDataAdapter as default
185
- };
85
+ //#endregion
86
+ export { createIndex as default };
@@ -0,0 +1,15 @@
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))))];
13
+ }
14
+ //#endregion
15
+ exports.default = intersection;