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

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 (126) hide show
  1. package/dist/.vite/manifest.json +118 -84
  2. package/dist/AsyncDataAdapter.d.ts +4 -6
  3. package/dist/AutoFetchDataAdapter.d.ts +4 -6
  4. package/dist/Collection/Cursor.d.ts +3 -3
  5. package/dist/Collection/index.d.ts +8 -9
  6. package/dist/Collection/types.d.ts +2 -2
  7. package/dist/DataAdapter.d.ts +3 -4
  8. package/dist/DefaultDataAdapter.d.ts +3 -5
  9. package/dist/WorkerDataAdapter.d.ts +2 -4
  10. package/dist/WorkerDataAdapterHost.d.ts +5 -5
  11. package/dist/createIndex.d.ts +2 -2
  12. package/dist/createIndexProvider.d.ts +2 -2
  13. package/dist/createReactivityAdapter.d.ts +2 -2
  14. package/dist/createStorageAdapter.d.ts +1 -1
  15. package/dist/getIndexInfo.d.ts +3 -4
  16. package/dist/index.cjs.js +33 -34
  17. package/dist/index.mjs +17 -35
  18. package/dist/index10.cjs.js +23 -0
  19. package/dist/index10.mjs +21 -17
  20. package/dist/index11.cjs.js +45 -0
  21. package/dist/index11.mjs +44 -15
  22. package/dist/index12.cjs.js +22 -0
  23. package/dist/index12.mjs +21 -47
  24. package/dist/index13.cjs.js +45 -0
  25. package/dist/index13.mjs +44 -365
  26. package/dist/index14.cjs.js +86 -0
  27. package/dist/index14.mjs +85 -416
  28. package/dist/index15.cjs.js +15 -0
  29. package/dist/index15.mjs +14 -206
  30. package/dist/index16.cjs.js +136 -0
  31. package/dist/index16.mjs +135 -388
  32. package/dist/index17.cjs.js +47 -0
  33. package/dist/index17.mjs +45 -578
  34. package/dist/index18.cjs.js +15 -0
  35. package/dist/index18.mjs +14 -157
  36. package/dist/index19.cjs.js +23 -0
  37. package/dist/index19.mjs +22 -26
  38. package/dist/index2.cjs.js +33 -0
  39. package/dist/index2.mjs +32 -258
  40. package/dist/index20.cjs.js +37 -0
  41. package/dist/index20.mjs +36 -41
  42. package/dist/index21.cjs.js +35 -0
  43. package/dist/index21.mjs +34 -16
  44. package/dist/index22.cjs.js +25 -0
  45. package/dist/index22.mjs +24 -104
  46. package/dist/index23.cjs.js +19 -0
  47. package/dist/index23.mjs +18 -128
  48. package/dist/index24.cjs.js +342 -0
  49. package/dist/index24.mjs +340 -6
  50. package/dist/index25.cjs.js +519 -0
  51. package/dist/index25.mjs +518 -27
  52. package/dist/index26.cjs.js +11 -0
  53. package/dist/index26.mjs +10 -10
  54. package/dist/index27.cjs.js +11 -0
  55. package/dist/index27.mjs +10 -29
  56. package/dist/index28.cjs.js +90 -0
  57. package/dist/index28.mjs +88 -46
  58. package/dist/index29.cjs.js +370 -0
  59. package/dist/index29.mjs +369 -8
  60. package/dist/index3.cjs.js +19 -0
  61. package/dist/index3.mjs +18 -539
  62. package/dist/index30.cjs.js +72 -0
  63. package/dist/index30.mjs +71 -9
  64. package/dist/index31.cjs.js +191 -0
  65. package/dist/index31.mjs +190 -7
  66. package/dist/index32.cjs.js +21 -0
  67. package/dist/index32.mjs +20 -42
  68. package/dist/index33.cjs.js +336 -0
  69. package/dist/index33.mjs +335 -29
  70. package/dist/index34.cjs.js +539 -0
  71. package/dist/index34.mjs +537 -4
  72. package/dist/index4.cjs.js +150 -0
  73. package/dist/index4.mjs +149 -5
  74. package/dist/index5.cjs.js +254 -0
  75. package/dist/index5.mjs +252 -4
  76. package/dist/index6.cjs.js +125 -0
  77. package/dist/index6.mjs +124 -29
  78. package/dist/index7.cjs.js +33 -0
  79. package/dist/index7.mjs +32 -12
  80. package/dist/index8.cjs.js +12 -0
  81. package/dist/index8.mjs +10 -4
  82. package/dist/index9.cjs.js +11 -0
  83. package/dist/index9.mjs +10 -133
  84. package/dist/types/IndexProvider.d.ts +2 -2
  85. package/dist/types/Modifier.d.ts +1 -1
  86. package/dist/types/ReactivityAdapter.d.ts +1 -1
  87. package/dist/utils/createSignal.d.ts +2 -2
  88. package/dist/utils/getMatchingKeys.d.ts +2 -2
  89. package/dist/utils/isFieldExpression.d.ts +1 -1
  90. package/dist/utils/match.d.ts +1 -1
  91. package/dist/utils/modify.d.ts +1 -1
  92. package/dist/utils/queryId.d.ts +2 -2
  93. package/package.json +1 -1
  94. package/dist/index.cjs10.js +0 -18
  95. package/dist/index.cjs11.js +0 -15
  96. package/dist/index.cjs12.js +0 -48
  97. package/dist/index.cjs13.js +0 -365
  98. package/dist/index.cjs14.js +0 -416
  99. package/dist/index.cjs15.js +0 -206
  100. package/dist/index.cjs16.js +0 -388
  101. package/dist/index.cjs17.js +0 -579
  102. package/dist/index.cjs18.js +0 -157
  103. package/dist/index.cjs19.js +0 -26
  104. package/dist/index.cjs2.js +0 -259
  105. package/dist/index.cjs20.js +0 -42
  106. package/dist/index.cjs21.js +0 -16
  107. package/dist/index.cjs22.js +0 -104
  108. package/dist/index.cjs23.js +0 -129
  109. package/dist/index.cjs24.js +0 -7
  110. package/dist/index.cjs25.js +0 -27
  111. package/dist/index.cjs26.js +0 -10
  112. package/dist/index.cjs27.js +0 -29
  113. package/dist/index.cjs28.js +0 -46
  114. package/dist/index.cjs29.js +0 -8
  115. package/dist/index.cjs3.js +0 -540
  116. package/dist/index.cjs30.js +0 -9
  117. package/dist/index.cjs31.js +0 -7
  118. package/dist/index.cjs32.js +0 -42
  119. package/dist/index.cjs33.js +0 -29
  120. package/dist/index.cjs34.js +0 -5
  121. package/dist/index.cjs4.js +0 -5
  122. package/dist/index.cjs5.js +0 -5
  123. package/dist/index.cjs6.js +0 -29
  124. package/dist/index.cjs7.js +0 -12
  125. package/dist/index.cjs8.js +0 -5
  126. package/dist/index.cjs9.js +0 -133
package/dist/index14.mjs CHANGED
@@ -1,417 +1,86 @@
1
- import deepClone from "./index20.mjs";
2
- import match from "./index24.mjs";
3
- import modify from "./index7.mjs";
4
- import queryId from "./index21.mjs";
5
- import isEqual from "./index6.mjs";
6
- import getIndexInfo from "./index23.mjs";
7
- import getMatchingKeys from "./index27.mjs";
8
- import sortItems from "./index26.mjs";
9
- import project from "./index25.mjs";
10
- class AsyncDataAdapter {
11
- options;
12
- id;
13
- onError;
14
- // Per-collection resources
15
- storageAdapters = /* @__PURE__ */ new Map();
16
- storageAdapterReady = /* @__PURE__ */ new Map();
17
- collectionIndices = /* @__PURE__ */ new Map();
18
- // Per-collection query registries
19
- queries = /* @__PURE__ */ new Map();
20
- constructor(options) {
21
- this.options = options;
22
- this.id = options.id || "async-data-adapter";
23
- this.onError = options.onError ?? ((error) => {
24
- console.error(error);
25
- });
26
- }
27
- createCollectionBackend(collection, indices) {
28
- this.collectionIndices.set(collection.name, indices);
29
- this.queries.set(collection.name, /* @__PURE__ */ new Map());
30
- this.ensureStorageAdapter(collection.name);
31
- const ready = (async () => {
32
- try {
33
- await this.setupStorage(collection.name, indices);
34
- } catch (error) {
35
- this.onError(error);
36
- throw error;
37
- }
38
- })();
39
- this.storageAdapterReady.set(collection.name, ready);
40
- const registerQuery = (selector, options) => {
41
- const qid = queryId(selector, options);
42
- const registry = this.queries.get(collection.name);
43
- if (!registry)
44
- throw new Error(`Collection ${collection.name} not initialized!`);
45
- registry.set(qid, {
46
- selector,
47
- options,
48
- items: [],
49
- listeners: /* @__PURE__ */ new Set(),
50
- ...registry.get(qid),
51
- state: "active",
52
- error: null
53
- });
54
- void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
55
- };
56
- const unregisterQuery = (selector, options) => {
57
- const qid = queryId(selector, options);
58
- this.queries.get(collection.name)?.delete(qid);
59
- };
60
- const getQueryState = (selector, options) => {
61
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
62
- return q?.state ?? "active";
63
- };
64
- const getQueryError = (selector, options) => {
65
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
66
- return q?.error ?? null;
67
- };
68
- const getQueryResult = (selector, options) => {
69
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
70
- return q?.items ?? [];
71
- };
72
- const onQueryStateChange = (selector, options, callback) => {
73
- const qid = queryId(selector, options);
74
- const registry = this.queries.get(collection.name);
75
- if (!registry)
76
- throw new Error(`Collection ${collection.name} not initialized!`);
77
- if (!registry.has(qid)) {
78
- registry.set(qid, {
79
- selector,
80
- options,
81
- state: "active",
82
- error: null,
83
- items: [],
84
- listeners: /* @__PURE__ */ new Set()
85
- });
86
- }
87
- registry.get(qid)?.listeners.add(callback);
88
- return () => registry.get(qid)?.listeners.delete(callback);
89
- };
90
- return {
91
- insert: async (item) => {
92
- await ready;
93
- const inserted = await this.insert(collection.name, item);
94
- return inserted;
95
- },
96
- updateOne: async (selector, modifier) => {
97
- await ready;
98
- return this.updateOne(collection.name, selector, modifier);
99
- },
100
- updateMany: async (selector, modifier) => {
101
- await ready;
102
- return this.updateMany(collection.name, selector, modifier);
103
- },
104
- replaceOne: async (selector, replacement) => {
105
- await ready;
106
- return this.replaceOne(collection.name, selector, replacement);
107
- },
108
- removeOne: async (selector) => {
109
- await ready;
110
- return this.removeOne(collection.name, selector);
111
- },
112
- removeMany: async (selector) => {
113
- await ready;
114
- return this.removeMany(collection.name, selector);
115
- },
116
- registerQuery,
117
- unregisterQuery,
118
- getQueryState,
119
- getQueryError,
120
- getQueryResult,
121
- onQueryStateChange,
122
- executeQuery: async (selector, options) => {
123
- await ready;
124
- return this.executeQuery(collection.name, selector, options);
125
- },
126
- dispose: async () => {
127
- this.storageAdapters.delete(collection.name);
128
- this.queries.delete(collection.name);
129
- this.collectionIndices.delete(collection.name);
130
- this.storageAdapterReady.delete(collection.name);
131
- },
132
- isReady: async () => {
133
- await ready;
134
- }
135
- };
136
- }
137
- async setupStorage(collectionName, indices) {
138
- const storage = this.storageAdapters.get(collectionName);
139
- if (!storage)
140
- throw new Error(`No persistence adapter for collection ${collectionName}`);
141
- await storage.setup();
142
- await Promise.all(indices.map((index) => storage.createIndex(index)));
143
- }
144
- ensureStorageAdapter(name) {
145
- if (this.storageAdapters.has(name))
146
- return;
147
- const adapter = this.options.storage(name);
148
- if (!adapter)
149
- return;
150
- this.storageAdapters.set(name, adapter);
151
- }
152
- /**
153
- * Compute and publish the result for a specific query
154
- * @param collectionName - name of the collection
155
- * @param selector - query selector
156
- * @param options - query options
157
- */
158
- async fulfillQuery(collectionName, selector, options) {
159
- const qid = queryId(selector, options);
160
- const registry = this.queries.get(collectionName);
161
- if (!registry)
162
- throw new Error(`Collection ${collectionName} not initialized!`);
163
- const rec = registry.get(qid);
164
- if (!rec)
165
- return;
166
- this.publishState(collectionName, qid, "active", null);
167
- try {
168
- const items = await this.executeQuery(collectionName, selector, options);
169
- this.publishResult(collectionName, qid, items);
170
- this.publishState(collectionName, qid, "complete", null);
171
- } catch (error) {
172
- this.publishState(collectionName, qid, "error", error);
173
- }
174
- }
175
- /**
176
- * Notify listeners about state changes and keep the cache updated
177
- * @param collectionName - name of the collection
178
- * @param qid - query id
179
- * @param state - new state
180
- * @param error - error if state is 'error', null otherwise
181
- */
182
- publishState(collectionName, qid, state, error) {
183
- const rec = this.queries.get(collectionName)?.get(qid);
184
- if (!rec)
185
- return;
186
- rec.state = state;
187
- rec.error = error;
188
- for (const callback of rec.listeners) {
189
- try {
190
- callback(state);
191
- } catch (error_) {
192
- this.onError(error_);
193
- }
194
- }
195
- }
196
- publishResult(collectionName, qid, items) {
197
- const rec = this.queries.get(collectionName)?.get(qid);
198
- if (!rec)
199
- return;
200
- rec.items = items;
201
- }
202
- async getIndexInfo(collectionName, selector) {
203
- const storageAdapter = this.storageAdapters.get(collectionName);
204
- if (!storageAdapter)
205
- throw new Error(`No persistence adapter for collection ${collectionName}`);
206
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
207
- return {
208
- matched: true,
209
- ids: [selector.id].filter(Boolean),
210
- optimizedSelector: {}
211
- };
212
- }
213
- if (selector == null) {
214
- return {
215
- matched: false,
216
- ids: [],
217
- optimizedSelector: {}
218
- };
219
- }
220
- const indices = this.collectionIndices.get(collectionName) ?? [];
221
- return getIndexInfo(indices.map((field) => async (flatSelector) => {
222
- if (!Object.hasOwnProperty.call(flatSelector, field))
223
- return { matched: false };
224
- const index = await storageAdapter.readIndex(field);
225
- const fieldSelector = flatSelector[field];
226
- const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
227
- const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
228
- if (keys.include == null && keys.exclude == null)
229
- return { matched: false };
230
- let includedIds = [];
231
- if (keys.include == null) {
232
- for (const set of index.values())
233
- for (const pos of set)
234
- includedIds.push(pos);
235
- } else {
236
- for (const key of keys.include) {
237
- const idSet = index.get(key);
238
- if (idSet)
239
- for (const id of idSet)
240
- includedIds.push(id);
241
- }
242
- }
243
- if (keys.exclude != null) {
244
- const excludeIds = /* @__PURE__ */ new Set();
245
- for (const key of keys.exclude) {
246
- const idSet = index.get(key);
247
- if (idSet)
248
- for (const id of idSet)
249
- excludeIds.add(id);
250
- }
251
- includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
252
- }
253
- return { matched: true, ids: includedIds, fields: [field], keepSelector: filtersForNull };
254
- }), selector);
255
- }
256
- async queryItems(collectionName, selector) {
257
- const storage = this.storageAdapters.get(collectionName);
258
- if (!storage)
259
- throw new Error(`No persistence adapter for collection ${collectionName}`);
260
- const index = await this.getIndexInfo(collectionName, selector);
261
- const matchItems = (item) => {
262
- if (index.optimizedSelector == null)
263
- return true;
264
- if (Object.keys(index.optimizedSelector).length <= 0)
265
- return true;
266
- return match(item, index.optimizedSelector);
267
- };
268
- if (index.matched) {
269
- const items = await storage.readIds(index.ids);
270
- if (isEqual(index.optimizedSelector, {}))
271
- return items;
272
- return items.filter(matchItems);
273
- } else {
274
- const allItems = await storage.readAll();
275
- if (isEqual(selector, {}))
276
- return allItems;
277
- return allItems.filter(matchItems);
278
- }
279
- }
280
- async executeQuery(collectionName, selector, options) {
281
- const items = await this.queryItems(collectionName, selector || {});
282
- const { sort, skip, limit, fields } = options || {};
283
- const sorted = sort ? sortItems(items, sort) : items;
284
- const skipped = skip ? sorted.slice(skip) : sorted;
285
- const limited = limit ? skipped.slice(0, limit) : skipped;
286
- const idExcluded = fields && fields.id === 0;
287
- return limited.map((item) => {
288
- if (!fields)
289
- return item;
290
- return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
291
- });
292
- }
293
- /**
294
- * After mutations, recompute and push updates for affected active queries
295
- * @param collectionName - name of the collection
296
- * @param changedItems - items that were inserted/updated/replaced/removed
297
- */
298
- async checkQueryUpdates(collectionName, changedItems) {
299
- const registry = this.queries.get(collectionName);
300
- if (!registry)
301
- throw new Error(`Collection ${collectionName} not initialized!`);
302
- if (registry.size === 0)
303
- return;
304
- const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
305
- if (affected.length === 0)
306
- return;
307
- for (const { selector, options } of affected) {
308
- const qid = queryId(selector, options);
309
- this.publishState(collectionName, qid, "active", null);
310
- }
311
- await Promise.all(affected.map(async ({ selector, options }) => {
312
- const qid = queryId(selector, options);
313
- try {
314
- const items = await this.executeQuery(collectionName, selector, options);
315
- this.publishResult(collectionName, qid, items);
316
- this.publishState(collectionName, qid, "complete", null);
317
- } catch (error) {
318
- this.publishState(collectionName, qid, "error", error);
319
- }
320
- }));
321
- }
322
- async insert(collectionName, newItem) {
323
- const storage = this.storageAdapters.get(collectionName);
324
- if (!storage)
325
- throw new Error(`No persistence adapter for collection ${collectionName}`);
326
- const existingItems = await storage.readIds([newItem.id]);
327
- if (existingItems.length > 0)
328
- throw new Error(`Item with id ${String(newItem.id)} already exists`);
329
- await storage.insert([newItem]);
330
- await this.checkQueryUpdates(collectionName, [newItem]);
331
- return newItem;
332
- }
333
- async updateOne(collectionName, selector, modifier) {
334
- const storage = this.storageAdapters.get(collectionName);
335
- if (!storage)
336
- throw new Error(`No persistence adapter for collection ${collectionName}`);
337
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
338
- const [item] = items;
339
- const { $setOnInsert, ...rest } = modifier;
340
- if (item == null)
341
- return [];
342
- const modified = modify(deepClone(item), rest);
343
- if (item.id !== modified.id) {
344
- const existing = await storage.readIds([modified.id]);
345
- if (existing.length > 0)
346
- throw new Error(`Item with id ${String(modified.id)} already exists`);
347
- }
348
- await storage.replace([modified]);
349
- await this.checkQueryUpdates(collectionName, [modified]);
350
- return [modified];
351
- }
352
- async updateMany(collectionName, selector, modifier) {
353
- const storage = this.storageAdapters.get(collectionName);
354
- if (!storage)
355
- throw new Error(`No persistence adapter for collection ${collectionName}`);
356
- const items = await this.executeQuery(collectionName, selector);
357
- if (items.length === 0)
358
- return [];
359
- const { $setOnInsert, ...rest } = modifier;
360
- const changed = await Promise.all(items.map(async (item) => {
361
- const modified = modify(deepClone(item), rest);
362
- if (item.id !== modified.id) {
363
- const existing = await storage.readIds([modified.id]);
364
- if (existing.length > 0)
365
- throw new Error(`Item with id ${String(modified.id)} already exists`);
366
- }
367
- return modified;
368
- }));
369
- await storage.replace(changed);
370
- await this.checkQueryUpdates(collectionName, changed);
371
- return changed;
372
- }
373
- async replaceOne(collectionName, selector, replacement) {
374
- const storage = this.storageAdapters.get(collectionName);
375
- if (!storage)
376
- throw new Error(`No persistence adapter for collection ${collectionName}`);
377
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
378
- const [item] = items;
379
- if (item == null)
380
- return [];
381
- const modified = { ...replacement, id: replacement.id ?? item.id };
382
- if (item.id !== modified.id) {
383
- const existing = await storage.readIds([modified.id]);
384
- if (existing.length > 0)
385
- throw new Error(`Item with id ${String(modified.id)} already exists`);
386
- }
387
- await storage.replace([modified]);
388
- await this.checkQueryUpdates(collectionName, [modified]);
389
- return [modified];
390
- }
391
- async removeOne(collectionName, selector) {
392
- const storage = this.storageAdapters.get(collectionName);
393
- if (!storage)
394
- throw new Error(`No persistence adapter for collection ${collectionName}`);
395
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
396
- const [item] = items;
397
- if (item == null)
398
- return [];
399
- await storage.remove([item]);
400
- await this.checkQueryUpdates(collectionName, [item]);
401
- return [item];
402
- }
403
- async removeMany(collectionName, selector) {
404
- const storage = this.storageAdapters.get(collectionName);
405
- if (!storage)
406
- throw new Error(`No persistence adapter for collection ${collectionName}`);
407
- const items = await this.executeQuery(collectionName, selector);
408
- if (items.length === 0)
409
- return [];
410
- await storage.remove(items);
411
- await this.checkQueryUpdates(collectionName, items);
412
- return items;
413
- }
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
+ });
414
84
  }
415
- export {
416
- AsyncDataAdapter as default
417
- };
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;