@oimdb/react 2.1.2 → 2.2.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  React hooks and context helpers for OIMDB reactive collections, objects, and indexes.
4
4
 
5
- 📖 **[Full documentation](https://abaikov.github.io/oimdb/)**
5
+ 📖 **[Full documentation](https://oimdb.org/)**
6
6
 
7
7
  ## For AI Assistants
8
8
 
@@ -118,6 +118,13 @@ function TeamUsers({ teamId }: { teamId: string }) {
118
118
  - `useSelectEntitiesByIndexKeyArrayBased(collection, index, key)` watches entities referenced by one index key.
119
119
  - `useSelectEntitiesByIndexKeysArrayBased(collection, index, keys)` watches entities referenced by several index keys.
120
120
 
121
+ ### Composite (Key-Path) Indexes
122
+
123
+ For indexes keyed by a composite **key path** (an array, from `indexFactory.compositeSetIndex()` / `compositeArrayIndex()`). These stabilize the key path **by content**, so passing a freshly built `['p1', 'admin']` on every render does not re-subscribe.
124
+
125
+ - `useSelectPksByCompositeIndexKeySetBased(index, path)` / `…ArrayBased(index, path)` watch PKs for one key path.
126
+ - `useSelectEntitiesByCompositeIndexKeySetBased(collection, index, path)` / `…ArrayBased(collection, index, path)` watch entities for one key path.
127
+
121
128
  ### Reactive Objects
122
129
 
123
130
  - `useSelectValueByObjectKey(object, key)` watches one object key.
@@ -153,7 +160,7 @@ const ids = useSelectPksByIndexKeyArrayBasedSignal(cardsByDeck, deckId);
153
160
 
154
161
  Available signal hooks (same signatures as their default counterparts): `useSelectEntityByPkSignal`, `useSelectPksByIndexKeyArrayBasedSignal`, `useSelectPksByIndexKeySetBasedSignal`. Compose the fine-grained pattern from them: a parent reads pks by index key, each row reads its own entity by pk.
155
162
 
156
- **Use only when every reader is subscription-driven.** Trade-offs: not Concurrent-Mode tearing-safe; the entity reference is stable across changes, so `React.memo` on entities, prev/next diffing, time-travel, and the default uSES hooks on the same collection won't see updates. Select each entity where you render it (by pk); don't pass mutable entities into `React.memo` children. See [Performance](https://abaikov.github.io/oimdb/docs/guides/performance) for when this actually pays off (in plain React the per-component commit usually dominates).
163
+ **Use only when every reader is subscription-driven.** Trade-offs: not Concurrent-Mode tearing-safe; the entity reference is stable across changes, so `React.memo` on entities, prev/next diffing, time-travel, and the default uSES hooks on the same collection won't see updates. Select each entity where you render it (by pk); don't pass mutable entities into `React.memo` children. See [Performance](https://oimdb.org/docs/guides/performance) for when this actually pays off (in plain React the per-component commit usually dominates).
157
164
 
158
165
  ## Context API
159
166
 
package/dist/index.cjs CHANGED
@@ -24,6 +24,10 @@ __export(index_exports, {
24
24
  OIMCollectionsProvider: () => OIMCollectionsProvider,
25
25
  createOIMCollectionsContext: () => createOIMCollectionsContext,
26
26
  useOIMCollectionsContext: () => useOIMCollectionsContext,
27
+ useSelectEntitiesByCompositeIndexKeyArrayBased: () => useSelectEntitiesByCompositeIndexKeyArrayBased,
28
+ useSelectEntitiesByCompositeIndexKeySetBased: () => useSelectEntitiesByCompositeIndexKeySetBased,
29
+ useSelectEntitiesByGlobalIndexArrayBased: () => useSelectEntitiesByGlobalIndexArrayBased,
30
+ useSelectEntitiesByGlobalIndexSetBased: () => useSelectEntitiesByGlobalIndexSetBased,
27
31
  useSelectEntitiesByIndexKeyArrayBased: () => useSelectEntitiesByIndexKeyArrayBased,
28
32
  useSelectEntitiesByIndexKeySetBased: () => useSelectEntitiesByIndexKeySetBased,
29
33
  useSelectEntitiesByIndexKeysArrayBased: () => useSelectEntitiesByIndexKeysArrayBased,
@@ -31,6 +35,12 @@ __export(index_exports, {
31
35
  useSelectEntitiesByPks: () => useSelectEntitiesByPks,
32
36
  useSelectEntityByPk: () => useSelectEntityByPk,
33
37
  useSelectEntityByPkSignal: () => useSelectEntityByPkSignal,
38
+ useSelectPksByCompositeIndexKeyArrayBased: () => useSelectPksByCompositeIndexKeyArrayBased,
39
+ useSelectPksByCompositeIndexKeySetBased: () => useSelectPksByCompositeIndexKeySetBased,
40
+ useSelectPksByGlobalIndexArrayBased: () => useSelectPksByGlobalIndexArrayBased,
41
+ useSelectPksByGlobalIndexArrayBasedSignal: () => useSelectPksByGlobalIndexArrayBasedSignal,
42
+ useSelectPksByGlobalIndexSetBased: () => useSelectPksByGlobalIndexSetBased,
43
+ useSelectPksByGlobalIndexSetBasedSignal: () => useSelectPksByGlobalIndexSetBasedSignal,
34
44
  useSelectPksByIndexKeyArrayBased: () => useSelectPksByIndexKeyArrayBased,
35
45
  useSelectPksByIndexKeyArrayBasedSignal: () => useSelectPksByIndexKeyArrayBasedSignal,
36
46
  useSelectPksByIndexKeySetBased: () => useSelectPksByIndexKeySetBased,
@@ -43,14 +53,121 @@ __export(index_exports, {
43
53
  module.exports = __toCommonJS(index_exports);
44
54
 
45
55
  // src/hooks/index.ts
46
- var import_react2 = require("react");
47
56
  var import_react3 = require("react");
57
+ var import_react4 = require("react");
48
58
 
49
- // src/hooks/signal.ts
59
+ // src/hooks/global.ts
50
60
  var import_react = require("react");
51
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? import_react.useLayoutEffect : import_react.useEffect;
61
+ var useSelectPksByGlobalIndexArrayBased = (reactiveIndex) => {
62
+ const snapshotValueRef = (0, import_react.useRef)();
63
+ const subscribe = (0, import_react.useMemo)(() => {
64
+ snapshotValueRef.current = reactiveIndex.getPks();
65
+ return (onStoreChange) => {
66
+ return reactiveIndex.subscribe(() => {
67
+ snapshotValueRef.current = reactiveIndex.getPks();
68
+ onStoreChange();
69
+ });
70
+ };
71
+ }, [reactiveIndex]);
72
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotValueRef.current, []);
73
+ return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
74
+ };
75
+ var useSelectPksByGlobalIndexSetBased = (reactiveIndex) => {
76
+ const snapshotValueRef = (0, import_react.useRef)();
77
+ const subscribe = (0, import_react.useMemo)(() => {
78
+ snapshotValueRef.current = reactiveIndex.getPks();
79
+ return (onStoreChange) => {
80
+ return reactiveIndex.subscribe(() => {
81
+ snapshotValueRef.current = reactiveIndex.getPks();
82
+ onStoreChange();
83
+ });
84
+ };
85
+ }, [reactiveIndex]);
86
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotValueRef.current, []);
87
+ return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
88
+ };
89
+ var useSelectEntitiesByGlobalIndexArrayBased = (reactiveCollection, reactiveIndex) => {
90
+ const snapshotRef = (0, import_react.useRef)();
91
+ const subscribe = (0, import_react.useMemo)(() => {
92
+ const readPks = () => reactiveIndex.getPks();
93
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
94
+ snapshotRef.current = readSnapshot();
95
+ return (onStoreChange) => {
96
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
97
+ readPks(),
98
+ () => {
99
+ snapshotRef.current = readSnapshot();
100
+ onStoreChange();
101
+ }
102
+ );
103
+ const resubscribeCollection = () => {
104
+ unsubscribeFromCollection();
105
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
106
+ readPks(),
107
+ () => {
108
+ snapshotRef.current = readSnapshot();
109
+ onStoreChange();
110
+ }
111
+ );
112
+ };
113
+ const unsubscribeFromIndex = reactiveIndex.subscribe(() => {
114
+ resubscribeCollection();
115
+ snapshotRef.current = readSnapshot();
116
+ onStoreChange();
117
+ });
118
+ return () => {
119
+ unsubscribeFromIndex();
120
+ unsubscribeFromCollection();
121
+ };
122
+ };
123
+ }, [reactiveCollection, reactiveIndex]);
124
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
125
+ return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
126
+ };
127
+ var useSelectEntitiesByGlobalIndexSetBased = (reactiveCollection, reactiveIndex) => {
128
+ const snapshotRef = (0, import_react.useRef)();
129
+ const subscribe = (0, import_react.useMemo)(() => {
130
+ const readPks = () => Array.from(reactiveIndex.getPks());
131
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
132
+ snapshotRef.current = readSnapshot();
133
+ return (onStoreChange) => {
134
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
135
+ readPks(),
136
+ () => {
137
+ snapshotRef.current = readSnapshot();
138
+ onStoreChange();
139
+ }
140
+ );
141
+ const resubscribeCollection = () => {
142
+ unsubscribeFromCollection();
143
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
144
+ readPks(),
145
+ () => {
146
+ snapshotRef.current = readSnapshot();
147
+ onStoreChange();
148
+ }
149
+ );
150
+ };
151
+ const unsubscribeFromIndex = reactiveIndex.subscribe(() => {
152
+ resubscribeCollection();
153
+ snapshotRef.current = readSnapshot();
154
+ onStoreChange();
155
+ });
156
+ return () => {
157
+ unsubscribeFromIndex();
158
+ unsubscribeFromCollection();
159
+ };
160
+ };
161
+ }, [reactiveCollection, reactiveIndex]);
162
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
163
+ return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
164
+ };
165
+
166
+ // src/hooks/signal.ts
167
+ var import_react2 = require("react");
168
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? import_react2.useLayoutEffect : import_react2.useEffect;
52
169
  function useForceUpdate() {
53
- const [, force] = (0, import_react.useReducer)((c) => c + 1 | 0, 0);
170
+ const [, force] = (0, import_react2.useReducer)((c) => c + 1 | 0, 0);
54
171
  return force;
55
172
  }
56
173
  var useSelectEntityByPkSignal = (reactiveCollection, pk) => {
@@ -77,18 +194,34 @@ var useSelectPksByIndexKeySetBasedSignal = (reactiveIndex, key) => {
77
194
  );
78
195
  return reactiveIndex.getPksByKey(key);
79
196
  };
197
+ var useSelectPksByGlobalIndexArrayBasedSignal = (reactiveIndex) => {
198
+ const force = useForceUpdate();
199
+ useIsomorphicLayoutEffect(
200
+ () => reactiveIndex.subscribe(force),
201
+ [reactiveIndex]
202
+ );
203
+ return reactiveIndex.getPks();
204
+ };
205
+ var useSelectPksByGlobalIndexSetBasedSignal = (reactiveIndex) => {
206
+ const force = useForceUpdate();
207
+ useIsomorphicLayoutEffect(
208
+ () => reactiveIndex.subscribe(force),
209
+ [reactiveIndex]
210
+ );
211
+ return reactiveIndex.getPks();
212
+ };
80
213
 
81
214
  // src/hooks/index.ts
82
215
  var EMPTY_ARRAY = [];
83
216
  var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
84
- const prevKeysRef = (0, import_react2.useRef)();
217
+ const prevKeysRef = (0, import_react3.useRef)();
85
218
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
86
219
  if (!keysAreEqual) {
87
220
  prevKeysRef.current = keys;
88
221
  }
89
222
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
90
- const snapshotValueRef = (0, import_react2.useRef)();
91
- const subscribe = (0, import_react2.useMemo)(() => {
223
+ const snapshotValueRef = (0, import_react3.useRef)();
224
+ const subscribe = (0, import_react3.useMemo)(() => {
92
225
  snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
93
226
  return (onStoreChange) => {
94
227
  const updateSnapshot = () => {
@@ -104,17 +237,17 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
104
237
  };
105
238
  };
106
239
  }, [reactiveIndex, stableKeys]);
107
- const getSnapshot = (0, import_react2.useMemo)(() => {
240
+ const getSnapshot = (0, import_react3.useMemo)(() => {
108
241
  return () => {
109
242
  return snapshotValueRef.current;
110
243
  };
111
244
  }, []);
112
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
245
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
113
246
  return snapshot;
114
247
  };
115
248
  var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
116
- const snapshotValueRef = (0, import_react2.useRef)();
117
- const subscribe = (0, import_react2.useMemo)(() => {
249
+ const snapshotValueRef = (0, import_react3.useRef)();
250
+ const subscribe = (0, import_react3.useMemo)(() => {
118
251
  snapshotValueRef.current = reactiveIndex.getPksByKey(key);
119
252
  return (onStoreChange) => {
120
253
  const updateSnapshot = () => {
@@ -124,23 +257,23 @@ var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
124
257
  return reactiveIndex.subscribeOnKey(key, updateSnapshot);
125
258
  };
126
259
  }, [key, reactiveIndex]);
127
- const getSnapshot = (0, import_react2.useMemo)(() => {
260
+ const getSnapshot = (0, import_react3.useMemo)(() => {
128
261
  return () => {
129
262
  return snapshotValueRef.current;
130
263
  };
131
264
  }, []);
132
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
265
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
133
266
  return snapshot;
134
267
  };
135
268
  var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
136
- const prevKeysRef = (0, import_react2.useRef)();
269
+ const prevKeysRef = (0, import_react3.useRef)();
137
270
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
138
271
  if (!keysAreEqual) {
139
272
  prevKeysRef.current = keys;
140
273
  }
141
274
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
142
- const snapshotValueRef = (0, import_react2.useRef)();
143
- const subscribe = (0, import_react2.useMemo)(() => {
275
+ const snapshotValueRef = (0, import_react3.useRef)();
276
+ const subscribe = (0, import_react3.useMemo)(() => {
144
277
  snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
145
278
  return (onStoreChange) => {
146
279
  const updateSnapshot = () => {
@@ -156,22 +289,22 @@ var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
156
289
  };
157
290
  };
158
291
  }, [reactiveIndex, stableKeys]);
159
- const getSnapshot = (0, import_react2.useMemo)(() => {
292
+ const getSnapshot = (0, import_react3.useMemo)(() => {
160
293
  return () => {
161
294
  return snapshotValueRef.current;
162
295
  };
163
296
  }, []);
164
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
297
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
165
298
  return snapshot;
166
299
  };
167
300
  var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
168
- const prevKeyRef = (0, import_react2.useRef)();
301
+ const prevKeyRef = (0, import_react3.useRef)();
169
302
  const keyAreEqual = prevKeyRef.current === key;
170
303
  if (!keyAreEqual) {
171
304
  prevKeyRef.current = key;
172
305
  }
173
- const snapshotValueRef = (0, import_react2.useRef)();
174
- const subscribe = (0, import_react2.useMemo)(() => {
306
+ const snapshotValueRef = (0, import_react3.useRef)();
307
+ const subscribe = (0, import_react3.useMemo)(() => {
175
308
  snapshotValueRef.current = reactiveIndex.getPksByKey(key);
176
309
  return (onStoreChange) => {
177
310
  const prevKey = prevKeyRef.current;
@@ -192,49 +325,49 @@ var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
192
325
  };
193
326
  };
194
327
  }, [key, reactiveIndex]);
195
- const getSnapshot = (0, import_react2.useMemo)(() => {
328
+ const getSnapshot = (0, import_react3.useMemo)(() => {
196
329
  return () => {
197
330
  return snapshotValueRef.current;
198
331
  };
199
332
  }, []);
200
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
333
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
201
334
  return snapshot;
202
335
  };
203
336
  var useSelectEntityByPk = (reactiveCollection, pk) => {
204
- const getSnapshot = (0, import_react2.useMemo)(
337
+ const getSnapshot = (0, import_react3.useMemo)(
205
338
  () => () => reactiveCollection.getOneByPk(pk),
206
339
  [pk, reactiveCollection]
207
340
  );
208
- const subscribe = (0, import_react2.useMemo)(() => {
341
+ const subscribe = (0, import_react3.useMemo)(() => {
209
342
  return (onStoreChange) => {
210
343
  return reactiveCollection.subscribeOnKey(pk, onStoreChange);
211
344
  };
212
345
  }, [pk, reactiveCollection]);
213
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
346
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
214
347
  return snapshot;
215
348
  };
216
349
  var useSelectValueByObjectKey = (reactiveObject, key) => {
217
- const getSnapshot = (0, import_react2.useMemo)(
350
+ const getSnapshot = (0, import_react3.useMemo)(
218
351
  () => () => reactiveObject.get(key),
219
352
  [key, reactiveObject]
220
353
  );
221
- const subscribe = (0, import_react2.useMemo)(() => {
354
+ const subscribe = (0, import_react3.useMemo)(() => {
222
355
  return (onStoreChange) => {
223
356
  return reactiveObject.subscribeOnKey(key, onStoreChange);
224
357
  };
225
358
  }, [key, reactiveObject]);
226
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
359
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
227
360
  return snapshot;
228
361
  };
229
362
  var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
230
- const prevKeysRef = (0, import_react2.useRef)();
363
+ const prevKeysRef = (0, import_react3.useRef)();
231
364
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
232
365
  if (!keysAreEqual) {
233
366
  prevKeysRef.current = keys;
234
367
  }
235
368
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
236
- const snapshotRef = (0, import_react2.useRef)();
237
- const subscribe = (0, import_react2.useMemo)(() => {
369
+ const snapshotRef = (0, import_react3.useRef)();
370
+ const subscribe = (0, import_react3.useMemo)(() => {
238
371
  snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
239
372
  return (onStoreChange) => {
240
373
  const updateSnapshot = () => {
@@ -252,12 +385,12 @@ var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
252
385
  };
253
386
  };
254
387
  }, [reactiveObject, stableKeys]);
255
- const getSnapshot = (0, import_react2.useMemo)(() => {
388
+ const getSnapshot = (0, import_react3.useMemo)(() => {
256
389
  return () => {
257
390
  return snapshotRef.current;
258
391
  };
259
392
  }, []);
260
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
393
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
261
394
  return snapshot;
262
395
  };
263
396
  function arraysEqual(a, b) {
@@ -269,14 +402,14 @@ function arraysEqual(a, b) {
269
402
  return true;
270
403
  }
271
404
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
272
- const prevPksRef = (0, import_react2.useRef)();
405
+ const prevPksRef = (0, import_react3.useRef)();
273
406
  const pksAreEqual = arraysEqual(prevPksRef.current || EMPTY_ARRAY, pks);
274
407
  if (!pksAreEqual) {
275
408
  prevPksRef.current = pks;
276
409
  }
277
410
  const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
278
- const snapshotRef = (0, import_react2.useRef)();
279
- const subscribe = (0, import_react2.useMemo)(() => {
411
+ const snapshotRef = (0, import_react3.useRef)();
412
+ const subscribe = (0, import_react3.useMemo)(() => {
280
413
  snapshotRef.current = stablePks.map(
281
414
  (pk) => reactiveCollection.getOneByPk(pk)
282
415
  );
@@ -296,17 +429,17 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
296
429
  };
297
430
  };
298
431
  }, [reactiveCollection, stablePks]);
299
- const getSnapshot = (0, import_react2.useMemo)(() => {
432
+ const getSnapshot = (0, import_react3.useMemo)(() => {
300
433
  return () => {
301
434
  return snapshotRef.current;
302
435
  };
303
436
  }, []);
304
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
437
+ const snapshot = (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
305
438
  return snapshot;
306
439
  };
307
440
  var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
308
- const snapshotRef = (0, import_react2.useRef)();
309
- const subscribe = (0, import_react2.useMemo)(() => {
441
+ const snapshotRef = (0, import_react3.useRef)();
442
+ const subscribe = (0, import_react3.useMemo)(() => {
310
443
  const readSnapshot = () => Array.from(
311
444
  reactiveIndex.getPksByKey(key),
312
445
  (pk) => reactiveCollection.getOneByPk(pk)
@@ -339,18 +472,18 @@ var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, ke
339
472
  };
340
473
  };
341
474
  }, [key, reactiveCollection, reactiveIndex]);
342
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
343
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
475
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
476
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
344
477
  };
345
478
  var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
346
- const prevKeysRef = (0, import_react2.useRef)();
479
+ const prevKeysRef = (0, import_react3.useRef)();
347
480
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
348
481
  if (!keysAreEqual) {
349
482
  prevKeysRef.current = keys;
350
483
  }
351
484
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
352
- const snapshotRef = (0, import_react2.useRef)();
353
- const subscribe = (0, import_react2.useMemo)(() => {
485
+ const snapshotRef = (0, import_react3.useRef)();
486
+ const subscribe = (0, import_react3.useMemo)(() => {
354
487
  const readPks = () => stableKeys.flatMap(
355
488
  (key) => Array.from(reactiveIndex.getPksByKey(key))
356
489
  );
@@ -385,12 +518,12 @@ var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, k
385
518
  };
386
519
  };
387
520
  }, [reactiveCollection, reactiveIndex, stableKeys]);
388
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
389
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
521
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
522
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
390
523
  };
391
524
  var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
392
- const snapshotRef = (0, import_react2.useRef)();
393
- const subscribe = (0, import_react2.useMemo)(() => {
525
+ const snapshotRef = (0, import_react3.useRef)();
526
+ const subscribe = (0, import_react3.useMemo)(() => {
394
527
  const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
395
528
  const readPks = () => reactiveIndex.getPksByKey(key);
396
529
  snapshotRef.current = readSnapshot();
@@ -420,18 +553,18 @@ var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex,
420
553
  };
421
554
  };
422
555
  }, [key, reactiveCollection, reactiveIndex]);
423
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
424
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
556
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
557
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
425
558
  };
426
559
  var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
427
- const prevKeysRef = (0, import_react2.useRef)();
560
+ const prevKeysRef = (0, import_react3.useRef)();
428
561
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
429
562
  if (!keysAreEqual) {
430
563
  prevKeysRef.current = keys;
431
564
  }
432
565
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
433
- const snapshotRef = (0, import_react2.useRef)();
434
- const subscribe = (0, import_react2.useMemo)(() => {
566
+ const snapshotRef = (0, import_react3.useRef)();
567
+ const subscribe = (0, import_react3.useMemo)(() => {
435
568
  const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
436
569
  const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
437
570
  snapshotRef.current = readSnapshot();
@@ -464,18 +597,139 @@ var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex,
464
597
  };
465
598
  };
466
599
  }, [reactiveCollection, reactiveIndex, stableKeys]);
467
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
468
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
600
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
601
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
602
+ };
603
+ function useStableKeyPath(key) {
604
+ const prevRef = (0, import_react3.useRef)();
605
+ const equal = prevRef.current !== void 0 && arraysEqual(prevRef.current, key);
606
+ if (!equal) {
607
+ prevRef.current = key;
608
+ }
609
+ return equal ? prevRef.current : key;
610
+ }
611
+ var useSelectPksByCompositeIndexKeySetBased = (reactiveIndex, key) => {
612
+ const stableKey = useStableKeyPath(key);
613
+ const snapshotValueRef = (0, import_react3.useRef)();
614
+ const subscribe = (0, import_react3.useMemo)(() => {
615
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
616
+ return (onStoreChange) => {
617
+ const updateSnapshot = () => {
618
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
619
+ onStoreChange();
620
+ };
621
+ return reactiveIndex.subscribeOnKey(stableKey, updateSnapshot);
622
+ };
623
+ }, [stableKey, reactiveIndex]);
624
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotValueRef.current, []);
625
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
626
+ };
627
+ var useSelectPksByCompositeIndexKeyArrayBased = (reactiveIndex, key) => {
628
+ const stableKey = useStableKeyPath(key);
629
+ const snapshotValueRef = (0, import_react3.useRef)();
630
+ const subscribe = (0, import_react3.useMemo)(() => {
631
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
632
+ return (onStoreChange) => {
633
+ const updateSnapshot = () => {
634
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
635
+ onStoreChange();
636
+ };
637
+ return reactiveIndex.subscribeOnKey(stableKey, updateSnapshot);
638
+ };
639
+ }, [stableKey, reactiveIndex]);
640
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotValueRef.current, []);
641
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
642
+ };
643
+ var useSelectEntitiesByCompositeIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
644
+ const stableKey = useStableKeyPath(key);
645
+ const snapshotRef = (0, import_react3.useRef)();
646
+ const subscribe = (0, import_react3.useMemo)(() => {
647
+ const readSnapshot = () => Array.from(
648
+ reactiveIndex.getPksByKey(stableKey),
649
+ (pk) => reactiveCollection.getOneByPk(pk)
650
+ );
651
+ const readPks = () => Array.from(reactiveIndex.getPksByKey(stableKey));
652
+ snapshotRef.current = readSnapshot();
653
+ return (onStoreChange) => {
654
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
655
+ snapshotRef.current = readSnapshot();
656
+ onStoreChange();
657
+ });
658
+ const resubscribeCollection = () => {
659
+ unsubscribeFromCollection();
660
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
661
+ readPks(),
662
+ () => {
663
+ snapshotRef.current = readSnapshot();
664
+ onStoreChange();
665
+ }
666
+ );
667
+ };
668
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(
669
+ stableKey,
670
+ () => {
671
+ resubscribeCollection();
672
+ snapshotRef.current = readSnapshot();
673
+ onStoreChange();
674
+ }
675
+ );
676
+ return () => {
677
+ unsubscribeFromIndex();
678
+ unsubscribeFromCollection();
679
+ };
680
+ };
681
+ }, [stableKey, reactiveCollection, reactiveIndex]);
682
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
683
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
684
+ };
685
+ var useSelectEntitiesByCompositeIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
686
+ const stableKey = useStableKeyPath(key);
687
+ const snapshotRef = (0, import_react3.useRef)();
688
+ const subscribe = (0, import_react3.useMemo)(() => {
689
+ const readSnapshot = () => reactiveIndex.getPksByKey(stableKey).map((pk) => reactiveCollection.getOneByPk(pk));
690
+ const readPks = () => reactiveIndex.getPksByKey(stableKey);
691
+ snapshotRef.current = readSnapshot();
692
+ return (onStoreChange) => {
693
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
694
+ snapshotRef.current = readSnapshot();
695
+ onStoreChange();
696
+ });
697
+ const resubscribeCollection = () => {
698
+ unsubscribeFromCollection();
699
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
700
+ readPks(),
701
+ () => {
702
+ snapshotRef.current = readSnapshot();
703
+ onStoreChange();
704
+ }
705
+ );
706
+ };
707
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(
708
+ stableKey,
709
+ () => {
710
+ resubscribeCollection();
711
+ snapshotRef.current = readSnapshot();
712
+ onStoreChange();
713
+ }
714
+ );
715
+ return () => {
716
+ unsubscribeFromIndex();
717
+ unsubscribeFromCollection();
718
+ };
719
+ };
720
+ }, [stableKey, reactiveCollection, reactiveIndex]);
721
+ const getSnapshot = (0, import_react3.useMemo)(() => () => snapshotRef.current, []);
722
+ return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
469
723
  };
470
724
 
471
725
  // src/context/index.tsx
472
- var import_react4 = require("react");
726
+ var import_react5 = require("react");
473
727
  var import_jsx_runtime = require("react/jsx-runtime");
474
- var OIMCollectionsContext = (0, import_react4.createContext)(
728
+ var OIMCollectionsContext = (0, import_react5.createContext)(
475
729
  {}
476
730
  );
477
731
  function createOIMCollectionsContext() {
478
- return (0, import_react4.createContext)({});
732
+ return (0, import_react5.createContext)({});
479
733
  }
480
734
  function OIMCollectionsProvider({ collections, children, context }) {
481
735
  const ContextToUse = context || OIMCollectionsContext;
@@ -483,7 +737,7 @@ function OIMCollectionsProvider({ collections, children, context }) {
483
737
  }
484
738
  function useOIMCollectionsContext(context) {
485
739
  const contextToUse = context || OIMCollectionsContext;
486
- const collections = (0, import_react4.useContext)(contextToUse);
740
+ const collections = (0, import_react5.useContext)(contextToUse);
487
741
  if (!collections || Object.keys(collections).length === 0) {
488
742
  throw new Error(
489
743
  "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
@@ -497,6 +751,10 @@ function useOIMCollectionsContext(context) {
497
751
  OIMCollectionsProvider,
498
752
  createOIMCollectionsContext,
499
753
  useOIMCollectionsContext,
754
+ useSelectEntitiesByCompositeIndexKeyArrayBased,
755
+ useSelectEntitiesByCompositeIndexKeySetBased,
756
+ useSelectEntitiesByGlobalIndexArrayBased,
757
+ useSelectEntitiesByGlobalIndexSetBased,
500
758
  useSelectEntitiesByIndexKeyArrayBased,
501
759
  useSelectEntitiesByIndexKeySetBased,
502
760
  useSelectEntitiesByIndexKeysArrayBased,
@@ -504,6 +762,12 @@ function useOIMCollectionsContext(context) {
504
762
  useSelectEntitiesByPks,
505
763
  useSelectEntityByPk,
506
764
  useSelectEntityByPkSignal,
765
+ useSelectPksByCompositeIndexKeyArrayBased,
766
+ useSelectPksByCompositeIndexKeySetBased,
767
+ useSelectPksByGlobalIndexArrayBased,
768
+ useSelectPksByGlobalIndexArrayBasedSignal,
769
+ useSelectPksByGlobalIndexSetBased,
770
+ useSelectPksByGlobalIndexSetBasedSignal,
507
771
  useSelectPksByIndexKeyArrayBased,
508
772
  useSelectPksByIndexKeyArrayBasedSignal,
509
773
  useSelectPksByIndexKeySetBased,
package/dist/index.d.cts CHANGED
@@ -1,11 +1,22 @@
1
- import { TOIMPk, OIMReactiveCollection, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMReactiveObject } from '@oimdb/core';
1
+ import { TOIMPk, OIMGlobalIndexArrayBased, OIMReactiveGlobalIndexArrayBased, OIMGlobalIndexSetBased, OIMReactiveGlobalIndexSetBased, OIMReactiveCollection, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMReactiveObject, TOIMKeyPath } from '@oimdb/core';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import * as React from 'react';
4
4
  import { ReactNode } from 'react';
5
5
 
6
+ /**
7
+ * Hooks for keyless "Global" (whole-collection / "all") indexes. Same shape as
8
+ * the keyed `…ByIndexKey…` hooks, minus the `key` argument.
9
+ */
10
+ declare const useSelectPksByGlobalIndexArrayBased: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => TPk[] | undefined;
11
+ declare const useSelectPksByGlobalIndexSetBased: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => Set<TPk> | undefined;
12
+ declare const useSelectEntitiesByGlobalIndexArrayBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => readonly (TEntity | undefined)[] | undefined;
13
+ declare const useSelectEntitiesByGlobalIndexSetBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => readonly (TEntity | undefined)[] | undefined;
14
+
6
15
  declare const useSelectEntityByPkSignal: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
7
16
  declare const useSelectPksByIndexKeyArrayBasedSignal: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly TPk[];
8
17
  declare const useSelectPksByIndexKeySetBasedSignal: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => ReadonlySet<TPk>;
18
+ declare const useSelectPksByGlobalIndexArrayBasedSignal: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => readonly TPk[];
19
+ declare const useSelectPksByGlobalIndexSetBasedSignal: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => ReadonlySet<TPk>;
9
20
 
10
21
  declare const useSelectPksByIndexKeysSetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
11
22
  declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk> | undefined;
@@ -19,6 +30,10 @@ declare const useSelectEntitiesByIndexKeySetBased: <TEntity extends object, TPk
19
30
  declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
20
31
  declare const useSelectEntitiesByIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
21
32
  declare const useSelectEntitiesByIndexKeysArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
33
+ declare const useSelectPksByCompositeIndexKeySetBased: <TPk extends TOIMPk, TIndex extends OIMIndexSetBased<TOIMKeyPath, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => Set<TPk> | undefined;
34
+ declare const useSelectPksByCompositeIndexKeyArrayBased: <TPk extends TOIMPk, TIndex extends OIMIndexArrayBased<TOIMKeyPath, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => TPk[] | undefined;
35
+ declare const useSelectEntitiesByCompositeIndexKeySetBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMIndexSetBased<TOIMKeyPath, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => readonly (TEntity | undefined)[] | undefined;
36
+ declare const useSelectEntitiesByCompositeIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMIndexArrayBased<TOIMKeyPath, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => readonly (TEntity | undefined)[] | undefined;
22
37
 
23
38
  type CollectionsDictionary = Record<string, OIMReactiveCollection<any, any>>;
24
39
  type OIMContextValue<T extends CollectionsDictionary = CollectionsDictionary> = T;
@@ -32,4 +47,4 @@ interface OIMCollectionsProviderProps<T extends CollectionsDictionary = Collecti
32
47
  declare function OIMCollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMCollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
33
48
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
34
49
 
35
- export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectEntityByPkSignal, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeyArrayBasedSignal, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeySetBasedSignal, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
50
+ export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByCompositeIndexKeyArrayBased, useSelectEntitiesByCompositeIndexKeySetBased, useSelectEntitiesByGlobalIndexArrayBased, useSelectEntitiesByGlobalIndexSetBased, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectEntityByPkSignal, useSelectPksByCompositeIndexKeyArrayBased, useSelectPksByCompositeIndexKeySetBased, useSelectPksByGlobalIndexArrayBased, useSelectPksByGlobalIndexArrayBasedSignal, useSelectPksByGlobalIndexSetBased, useSelectPksByGlobalIndexSetBasedSignal, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeyArrayBasedSignal, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeySetBasedSignal, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,22 @@
1
- import { TOIMPk, OIMReactiveCollection, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMReactiveObject } from '@oimdb/core';
1
+ import { TOIMPk, OIMGlobalIndexArrayBased, OIMReactiveGlobalIndexArrayBased, OIMGlobalIndexSetBased, OIMReactiveGlobalIndexSetBased, OIMReactiveCollection, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMReactiveObject, TOIMKeyPath } from '@oimdb/core';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import * as React from 'react';
4
4
  import { ReactNode } from 'react';
5
5
 
6
+ /**
7
+ * Hooks for keyless "Global" (whole-collection / "all") indexes. Same shape as
8
+ * the keyed `…ByIndexKey…` hooks, minus the `key` argument.
9
+ */
10
+ declare const useSelectPksByGlobalIndexArrayBased: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => TPk[] | undefined;
11
+ declare const useSelectPksByGlobalIndexSetBased: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => Set<TPk> | undefined;
12
+ declare const useSelectEntitiesByGlobalIndexArrayBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => readonly (TEntity | undefined)[] | undefined;
13
+ declare const useSelectEntitiesByGlobalIndexSetBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => readonly (TEntity | undefined)[] | undefined;
14
+
6
15
  declare const useSelectEntityByPkSignal: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
7
16
  declare const useSelectPksByIndexKeyArrayBasedSignal: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly TPk[];
8
17
  declare const useSelectPksByIndexKeySetBasedSignal: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => ReadonlySet<TPk>;
18
+ declare const useSelectPksByGlobalIndexArrayBasedSignal: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexArrayBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexArrayBased<TPk, TIndex>) => readonly TPk[];
19
+ declare const useSelectPksByGlobalIndexSetBasedSignal: <TPk extends TOIMPk, TIndex extends OIMGlobalIndexSetBased<TPk>>(reactiveIndex: OIMReactiveGlobalIndexSetBased<TPk, TIndex>) => ReadonlySet<TPk>;
9
20
 
10
21
  declare const useSelectPksByIndexKeysSetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
11
22
  declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk> | undefined;
@@ -19,6 +30,10 @@ declare const useSelectEntitiesByIndexKeySetBased: <TEntity extends object, TPk
19
30
  declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
20
31
  declare const useSelectEntitiesByIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
21
32
  declare const useSelectEntitiesByIndexKeysArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
33
+ declare const useSelectPksByCompositeIndexKeySetBased: <TPk extends TOIMPk, TIndex extends OIMIndexSetBased<TOIMKeyPath, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => Set<TPk> | undefined;
34
+ declare const useSelectPksByCompositeIndexKeyArrayBased: <TPk extends TOIMPk, TIndex extends OIMIndexArrayBased<TOIMKeyPath, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => TPk[] | undefined;
35
+ declare const useSelectEntitiesByCompositeIndexKeySetBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMIndexSetBased<TOIMKeyPath, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => readonly (TEntity | undefined)[] | undefined;
36
+ declare const useSelectEntitiesByCompositeIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TIndex extends OIMIndexArrayBased<TOIMKeyPath, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TOIMKeyPath, TPk, TIndex>, key: TOIMKeyPath) => readonly (TEntity | undefined)[] | undefined;
22
37
 
23
38
  type CollectionsDictionary = Record<string, OIMReactiveCollection<any, any>>;
24
39
  type OIMContextValue<T extends CollectionsDictionary = CollectionsDictionary> = T;
@@ -32,4 +47,4 @@ interface OIMCollectionsProviderProps<T extends CollectionsDictionary = Collecti
32
47
  declare function OIMCollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMCollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
33
48
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
34
49
 
35
- export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectEntityByPkSignal, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeyArrayBasedSignal, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeySetBasedSignal, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
50
+ export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByCompositeIndexKeyArrayBased, useSelectEntitiesByCompositeIndexKeySetBased, useSelectEntitiesByGlobalIndexArrayBased, useSelectEntitiesByGlobalIndexSetBased, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectEntityByPkSignal, useSelectPksByCompositeIndexKeyArrayBased, useSelectPksByCompositeIndexKeySetBased, useSelectPksByGlobalIndexArrayBased, useSelectPksByGlobalIndexArrayBasedSignal, useSelectPksByGlobalIndexSetBased, useSelectPksByGlobalIndexSetBasedSignal, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeyArrayBasedSignal, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeySetBasedSignal, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.js CHANGED
@@ -1,6 +1,113 @@
1
1
  // src/hooks/index.ts
2
- import { useMemo, useRef } from "react";
3
- import { useSyncExternalStore } from "react";
2
+ import { useMemo as useMemo2, useRef as useRef2 } from "react";
3
+ import { useSyncExternalStore as useSyncExternalStore2 } from "react";
4
+
5
+ // src/hooks/global.ts
6
+ import { useMemo, useRef, useSyncExternalStore } from "react";
7
+ var useSelectPksByGlobalIndexArrayBased = (reactiveIndex) => {
8
+ const snapshotValueRef = useRef();
9
+ const subscribe = useMemo(() => {
10
+ snapshotValueRef.current = reactiveIndex.getPks();
11
+ return (onStoreChange) => {
12
+ return reactiveIndex.subscribe(() => {
13
+ snapshotValueRef.current = reactiveIndex.getPks();
14
+ onStoreChange();
15
+ });
16
+ };
17
+ }, [reactiveIndex]);
18
+ const getSnapshot = useMemo(() => () => snapshotValueRef.current, []);
19
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
20
+ };
21
+ var useSelectPksByGlobalIndexSetBased = (reactiveIndex) => {
22
+ const snapshotValueRef = useRef();
23
+ const subscribe = useMemo(() => {
24
+ snapshotValueRef.current = reactiveIndex.getPks();
25
+ return (onStoreChange) => {
26
+ return reactiveIndex.subscribe(() => {
27
+ snapshotValueRef.current = reactiveIndex.getPks();
28
+ onStoreChange();
29
+ });
30
+ };
31
+ }, [reactiveIndex]);
32
+ const getSnapshot = useMemo(() => () => snapshotValueRef.current, []);
33
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
34
+ };
35
+ var useSelectEntitiesByGlobalIndexArrayBased = (reactiveCollection, reactiveIndex) => {
36
+ const snapshotRef = useRef();
37
+ const subscribe = useMemo(() => {
38
+ const readPks = () => reactiveIndex.getPks();
39
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
40
+ snapshotRef.current = readSnapshot();
41
+ return (onStoreChange) => {
42
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
43
+ readPks(),
44
+ () => {
45
+ snapshotRef.current = readSnapshot();
46
+ onStoreChange();
47
+ }
48
+ );
49
+ const resubscribeCollection = () => {
50
+ unsubscribeFromCollection();
51
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
52
+ readPks(),
53
+ () => {
54
+ snapshotRef.current = readSnapshot();
55
+ onStoreChange();
56
+ }
57
+ );
58
+ };
59
+ const unsubscribeFromIndex = reactiveIndex.subscribe(() => {
60
+ resubscribeCollection();
61
+ snapshotRef.current = readSnapshot();
62
+ onStoreChange();
63
+ });
64
+ return () => {
65
+ unsubscribeFromIndex();
66
+ unsubscribeFromCollection();
67
+ };
68
+ };
69
+ }, [reactiveCollection, reactiveIndex]);
70
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
71
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
72
+ };
73
+ var useSelectEntitiesByGlobalIndexSetBased = (reactiveCollection, reactiveIndex) => {
74
+ const snapshotRef = useRef();
75
+ const subscribe = useMemo(() => {
76
+ const readPks = () => Array.from(reactiveIndex.getPks());
77
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
78
+ snapshotRef.current = readSnapshot();
79
+ return (onStoreChange) => {
80
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
81
+ readPks(),
82
+ () => {
83
+ snapshotRef.current = readSnapshot();
84
+ onStoreChange();
85
+ }
86
+ );
87
+ const resubscribeCollection = () => {
88
+ unsubscribeFromCollection();
89
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
90
+ readPks(),
91
+ () => {
92
+ snapshotRef.current = readSnapshot();
93
+ onStoreChange();
94
+ }
95
+ );
96
+ };
97
+ const unsubscribeFromIndex = reactiveIndex.subscribe(() => {
98
+ resubscribeCollection();
99
+ snapshotRef.current = readSnapshot();
100
+ onStoreChange();
101
+ });
102
+ return () => {
103
+ unsubscribeFromIndex();
104
+ unsubscribeFromCollection();
105
+ };
106
+ };
107
+ }, [reactiveCollection, reactiveIndex]);
108
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
109
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
110
+ };
4
111
 
5
112
  // src/hooks/signal.ts
6
113
  import { useEffect, useLayoutEffect, useReducer } from "react";
@@ -33,18 +140,34 @@ var useSelectPksByIndexKeySetBasedSignal = (reactiveIndex, key) => {
33
140
  );
34
141
  return reactiveIndex.getPksByKey(key);
35
142
  };
143
+ var useSelectPksByGlobalIndexArrayBasedSignal = (reactiveIndex) => {
144
+ const force = useForceUpdate();
145
+ useIsomorphicLayoutEffect(
146
+ () => reactiveIndex.subscribe(force),
147
+ [reactiveIndex]
148
+ );
149
+ return reactiveIndex.getPks();
150
+ };
151
+ var useSelectPksByGlobalIndexSetBasedSignal = (reactiveIndex) => {
152
+ const force = useForceUpdate();
153
+ useIsomorphicLayoutEffect(
154
+ () => reactiveIndex.subscribe(force),
155
+ [reactiveIndex]
156
+ );
157
+ return reactiveIndex.getPks();
158
+ };
36
159
 
37
160
  // src/hooks/index.ts
38
161
  var EMPTY_ARRAY = [];
39
162
  var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
40
- const prevKeysRef = useRef();
163
+ const prevKeysRef = useRef2();
41
164
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
42
165
  if (!keysAreEqual) {
43
166
  prevKeysRef.current = keys;
44
167
  }
45
168
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
46
- const snapshotValueRef = useRef();
47
- const subscribe = useMemo(() => {
169
+ const snapshotValueRef = useRef2();
170
+ const subscribe = useMemo2(() => {
48
171
  snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
49
172
  return (onStoreChange) => {
50
173
  const updateSnapshot = () => {
@@ -60,17 +183,17 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
60
183
  };
61
184
  };
62
185
  }, [reactiveIndex, stableKeys]);
63
- const getSnapshot = useMemo(() => {
186
+ const getSnapshot = useMemo2(() => {
64
187
  return () => {
65
188
  return snapshotValueRef.current;
66
189
  };
67
190
  }, []);
68
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
191
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
69
192
  return snapshot;
70
193
  };
71
194
  var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
72
- const snapshotValueRef = useRef();
73
- const subscribe = useMemo(() => {
195
+ const snapshotValueRef = useRef2();
196
+ const subscribe = useMemo2(() => {
74
197
  snapshotValueRef.current = reactiveIndex.getPksByKey(key);
75
198
  return (onStoreChange) => {
76
199
  const updateSnapshot = () => {
@@ -80,23 +203,23 @@ var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
80
203
  return reactiveIndex.subscribeOnKey(key, updateSnapshot);
81
204
  };
82
205
  }, [key, reactiveIndex]);
83
- const getSnapshot = useMemo(() => {
206
+ const getSnapshot = useMemo2(() => {
84
207
  return () => {
85
208
  return snapshotValueRef.current;
86
209
  };
87
210
  }, []);
88
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
211
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
89
212
  return snapshot;
90
213
  };
91
214
  var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
92
- const prevKeysRef = useRef();
215
+ const prevKeysRef = useRef2();
93
216
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
94
217
  if (!keysAreEqual) {
95
218
  prevKeysRef.current = keys;
96
219
  }
97
220
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
98
- const snapshotValueRef = useRef();
99
- const subscribe = useMemo(() => {
221
+ const snapshotValueRef = useRef2();
222
+ const subscribe = useMemo2(() => {
100
223
  snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
101
224
  return (onStoreChange) => {
102
225
  const updateSnapshot = () => {
@@ -112,22 +235,22 @@ var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
112
235
  };
113
236
  };
114
237
  }, [reactiveIndex, stableKeys]);
115
- const getSnapshot = useMemo(() => {
238
+ const getSnapshot = useMemo2(() => {
116
239
  return () => {
117
240
  return snapshotValueRef.current;
118
241
  };
119
242
  }, []);
120
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
243
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
121
244
  return snapshot;
122
245
  };
123
246
  var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
124
- const prevKeyRef = useRef();
247
+ const prevKeyRef = useRef2();
125
248
  const keyAreEqual = prevKeyRef.current === key;
126
249
  if (!keyAreEqual) {
127
250
  prevKeyRef.current = key;
128
251
  }
129
- const snapshotValueRef = useRef();
130
- const subscribe = useMemo(() => {
252
+ const snapshotValueRef = useRef2();
253
+ const subscribe = useMemo2(() => {
131
254
  snapshotValueRef.current = reactiveIndex.getPksByKey(key);
132
255
  return (onStoreChange) => {
133
256
  const prevKey = prevKeyRef.current;
@@ -148,49 +271,49 @@ var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
148
271
  };
149
272
  };
150
273
  }, [key, reactiveIndex]);
151
- const getSnapshot = useMemo(() => {
274
+ const getSnapshot = useMemo2(() => {
152
275
  return () => {
153
276
  return snapshotValueRef.current;
154
277
  };
155
278
  }, []);
156
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
279
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
157
280
  return snapshot;
158
281
  };
159
282
  var useSelectEntityByPk = (reactiveCollection, pk) => {
160
- const getSnapshot = useMemo(
283
+ const getSnapshot = useMemo2(
161
284
  () => () => reactiveCollection.getOneByPk(pk),
162
285
  [pk, reactiveCollection]
163
286
  );
164
- const subscribe = useMemo(() => {
287
+ const subscribe = useMemo2(() => {
165
288
  return (onStoreChange) => {
166
289
  return reactiveCollection.subscribeOnKey(pk, onStoreChange);
167
290
  };
168
291
  }, [pk, reactiveCollection]);
169
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
292
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
170
293
  return snapshot;
171
294
  };
172
295
  var useSelectValueByObjectKey = (reactiveObject, key) => {
173
- const getSnapshot = useMemo(
296
+ const getSnapshot = useMemo2(
174
297
  () => () => reactiveObject.get(key),
175
298
  [key, reactiveObject]
176
299
  );
177
- const subscribe = useMemo(() => {
300
+ const subscribe = useMemo2(() => {
178
301
  return (onStoreChange) => {
179
302
  return reactiveObject.subscribeOnKey(key, onStoreChange);
180
303
  };
181
304
  }, [key, reactiveObject]);
182
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
305
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
183
306
  return snapshot;
184
307
  };
185
308
  var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
186
- const prevKeysRef = useRef();
309
+ const prevKeysRef = useRef2();
187
310
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
188
311
  if (!keysAreEqual) {
189
312
  prevKeysRef.current = keys;
190
313
  }
191
314
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
192
- const snapshotRef = useRef();
193
- const subscribe = useMemo(() => {
315
+ const snapshotRef = useRef2();
316
+ const subscribe = useMemo2(() => {
194
317
  snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
195
318
  return (onStoreChange) => {
196
319
  const updateSnapshot = () => {
@@ -208,12 +331,12 @@ var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
208
331
  };
209
332
  };
210
333
  }, [reactiveObject, stableKeys]);
211
- const getSnapshot = useMemo(() => {
334
+ const getSnapshot = useMemo2(() => {
212
335
  return () => {
213
336
  return snapshotRef.current;
214
337
  };
215
338
  }, []);
216
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
339
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
217
340
  return snapshot;
218
341
  };
219
342
  function arraysEqual(a, b) {
@@ -225,14 +348,14 @@ function arraysEqual(a, b) {
225
348
  return true;
226
349
  }
227
350
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
228
- const prevPksRef = useRef();
351
+ const prevPksRef = useRef2();
229
352
  const pksAreEqual = arraysEqual(prevPksRef.current || EMPTY_ARRAY, pks);
230
353
  if (!pksAreEqual) {
231
354
  prevPksRef.current = pks;
232
355
  }
233
356
  const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
234
- const snapshotRef = useRef();
235
- const subscribe = useMemo(() => {
357
+ const snapshotRef = useRef2();
358
+ const subscribe = useMemo2(() => {
236
359
  snapshotRef.current = stablePks.map(
237
360
  (pk) => reactiveCollection.getOneByPk(pk)
238
361
  );
@@ -252,17 +375,17 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
252
375
  };
253
376
  };
254
377
  }, [reactiveCollection, stablePks]);
255
- const getSnapshot = useMemo(() => {
378
+ const getSnapshot = useMemo2(() => {
256
379
  return () => {
257
380
  return snapshotRef.current;
258
381
  };
259
382
  }, []);
260
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
383
+ const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
261
384
  return snapshot;
262
385
  };
263
386
  var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
264
- const snapshotRef = useRef();
265
- const subscribe = useMemo(() => {
387
+ const snapshotRef = useRef2();
388
+ const subscribe = useMemo2(() => {
266
389
  const readSnapshot = () => Array.from(
267
390
  reactiveIndex.getPksByKey(key),
268
391
  (pk) => reactiveCollection.getOneByPk(pk)
@@ -295,18 +418,18 @@ var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, ke
295
418
  };
296
419
  };
297
420
  }, [key, reactiveCollection, reactiveIndex]);
298
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
299
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
421
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
422
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
300
423
  };
301
424
  var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
302
- const prevKeysRef = useRef();
425
+ const prevKeysRef = useRef2();
303
426
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
304
427
  if (!keysAreEqual) {
305
428
  prevKeysRef.current = keys;
306
429
  }
307
430
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
308
- const snapshotRef = useRef();
309
- const subscribe = useMemo(() => {
431
+ const snapshotRef = useRef2();
432
+ const subscribe = useMemo2(() => {
310
433
  const readPks = () => stableKeys.flatMap(
311
434
  (key) => Array.from(reactiveIndex.getPksByKey(key))
312
435
  );
@@ -341,12 +464,12 @@ var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, k
341
464
  };
342
465
  };
343
466
  }, [reactiveCollection, reactiveIndex, stableKeys]);
344
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
345
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
467
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
468
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
346
469
  };
347
470
  var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
348
- const snapshotRef = useRef();
349
- const subscribe = useMemo(() => {
471
+ const snapshotRef = useRef2();
472
+ const subscribe = useMemo2(() => {
350
473
  const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
351
474
  const readPks = () => reactiveIndex.getPksByKey(key);
352
475
  snapshotRef.current = readSnapshot();
@@ -376,18 +499,18 @@ var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex,
376
499
  };
377
500
  };
378
501
  }, [key, reactiveCollection, reactiveIndex]);
379
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
380
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
502
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
503
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
381
504
  };
382
505
  var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
383
- const prevKeysRef = useRef();
506
+ const prevKeysRef = useRef2();
384
507
  const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
385
508
  if (!keysAreEqual) {
386
509
  prevKeysRef.current = keys;
387
510
  }
388
511
  const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
389
- const snapshotRef = useRef();
390
- const subscribe = useMemo(() => {
512
+ const snapshotRef = useRef2();
513
+ const subscribe = useMemo2(() => {
391
514
  const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
392
515
  const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
393
516
  snapshotRef.current = readSnapshot();
@@ -420,8 +543,129 @@ var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex,
420
543
  };
421
544
  };
422
545
  }, [reactiveCollection, reactiveIndex, stableKeys]);
423
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
424
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
546
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
547
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
548
+ };
549
+ function useStableKeyPath(key) {
550
+ const prevRef = useRef2();
551
+ const equal = prevRef.current !== void 0 && arraysEqual(prevRef.current, key);
552
+ if (!equal) {
553
+ prevRef.current = key;
554
+ }
555
+ return equal ? prevRef.current : key;
556
+ }
557
+ var useSelectPksByCompositeIndexKeySetBased = (reactiveIndex, key) => {
558
+ const stableKey = useStableKeyPath(key);
559
+ const snapshotValueRef = useRef2();
560
+ const subscribe = useMemo2(() => {
561
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
562
+ return (onStoreChange) => {
563
+ const updateSnapshot = () => {
564
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
565
+ onStoreChange();
566
+ };
567
+ return reactiveIndex.subscribeOnKey(stableKey, updateSnapshot);
568
+ };
569
+ }, [stableKey, reactiveIndex]);
570
+ const getSnapshot = useMemo2(() => () => snapshotValueRef.current, []);
571
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
572
+ };
573
+ var useSelectPksByCompositeIndexKeyArrayBased = (reactiveIndex, key) => {
574
+ const stableKey = useStableKeyPath(key);
575
+ const snapshotValueRef = useRef2();
576
+ const subscribe = useMemo2(() => {
577
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
578
+ return (onStoreChange) => {
579
+ const updateSnapshot = () => {
580
+ snapshotValueRef.current = reactiveIndex.getPksByKey(stableKey);
581
+ onStoreChange();
582
+ };
583
+ return reactiveIndex.subscribeOnKey(stableKey, updateSnapshot);
584
+ };
585
+ }, [stableKey, reactiveIndex]);
586
+ const getSnapshot = useMemo2(() => () => snapshotValueRef.current, []);
587
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
588
+ };
589
+ var useSelectEntitiesByCompositeIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
590
+ const stableKey = useStableKeyPath(key);
591
+ const snapshotRef = useRef2();
592
+ const subscribe = useMemo2(() => {
593
+ const readSnapshot = () => Array.from(
594
+ reactiveIndex.getPksByKey(stableKey),
595
+ (pk) => reactiveCollection.getOneByPk(pk)
596
+ );
597
+ const readPks = () => Array.from(reactiveIndex.getPksByKey(stableKey));
598
+ snapshotRef.current = readSnapshot();
599
+ return (onStoreChange) => {
600
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
601
+ snapshotRef.current = readSnapshot();
602
+ onStoreChange();
603
+ });
604
+ const resubscribeCollection = () => {
605
+ unsubscribeFromCollection();
606
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
607
+ readPks(),
608
+ () => {
609
+ snapshotRef.current = readSnapshot();
610
+ onStoreChange();
611
+ }
612
+ );
613
+ };
614
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(
615
+ stableKey,
616
+ () => {
617
+ resubscribeCollection();
618
+ snapshotRef.current = readSnapshot();
619
+ onStoreChange();
620
+ }
621
+ );
622
+ return () => {
623
+ unsubscribeFromIndex();
624
+ unsubscribeFromCollection();
625
+ };
626
+ };
627
+ }, [stableKey, reactiveCollection, reactiveIndex]);
628
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
629
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
630
+ };
631
+ var useSelectEntitiesByCompositeIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
632
+ const stableKey = useStableKeyPath(key);
633
+ const snapshotRef = useRef2();
634
+ const subscribe = useMemo2(() => {
635
+ const readSnapshot = () => reactiveIndex.getPksByKey(stableKey).map((pk) => reactiveCollection.getOneByPk(pk));
636
+ const readPks = () => reactiveIndex.getPksByKey(stableKey);
637
+ snapshotRef.current = readSnapshot();
638
+ return (onStoreChange) => {
639
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
640
+ snapshotRef.current = readSnapshot();
641
+ onStoreChange();
642
+ });
643
+ const resubscribeCollection = () => {
644
+ unsubscribeFromCollection();
645
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
646
+ readPks(),
647
+ () => {
648
+ snapshotRef.current = readSnapshot();
649
+ onStoreChange();
650
+ }
651
+ );
652
+ };
653
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(
654
+ stableKey,
655
+ () => {
656
+ resubscribeCollection();
657
+ snapshotRef.current = readSnapshot();
658
+ onStoreChange();
659
+ }
660
+ );
661
+ return () => {
662
+ unsubscribeFromIndex();
663
+ unsubscribeFromCollection();
664
+ };
665
+ };
666
+ }, [stableKey, reactiveCollection, reactiveIndex]);
667
+ const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
668
+ return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
425
669
  };
426
670
 
427
671
  // src/context/index.tsx
@@ -452,6 +696,10 @@ export {
452
696
  OIMCollectionsProvider,
453
697
  createOIMCollectionsContext,
454
698
  useOIMCollectionsContext,
699
+ useSelectEntitiesByCompositeIndexKeyArrayBased,
700
+ useSelectEntitiesByCompositeIndexKeySetBased,
701
+ useSelectEntitiesByGlobalIndexArrayBased,
702
+ useSelectEntitiesByGlobalIndexSetBased,
455
703
  useSelectEntitiesByIndexKeyArrayBased,
456
704
  useSelectEntitiesByIndexKeySetBased,
457
705
  useSelectEntitiesByIndexKeysArrayBased,
@@ -459,6 +707,12 @@ export {
459
707
  useSelectEntitiesByPks,
460
708
  useSelectEntityByPk,
461
709
  useSelectEntityByPkSignal,
710
+ useSelectPksByCompositeIndexKeyArrayBased,
711
+ useSelectPksByCompositeIndexKeySetBased,
712
+ useSelectPksByGlobalIndexArrayBased,
713
+ useSelectPksByGlobalIndexArrayBasedSignal,
714
+ useSelectPksByGlobalIndexSetBased,
715
+ useSelectPksByGlobalIndexSetBasedSignal,
462
716
  useSelectPksByIndexKeyArrayBased,
463
717
  useSelectPksByIndexKeyArrayBasedSignal,
464
718
  useSelectPksByIndexKeySetBased,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "React integration for OIMDB - Hooks for selection and subscription with external storage",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -36,7 +36,7 @@
36
36
  "dist"
37
37
  ],
38
38
  "peerDependencies": {
39
- "@oimdb/core": "^2.0.0 || ^3.0.0",
39
+ "@oimdb/core": "^3.8.0",
40
40
  "react": "^18.0.0 || ^19.0.0"
41
41
  },
42
42
  "devDependencies": {