@oimdb/react 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,724 +1,351 @@
1
- // src/hooks/index.ts
2
- import { useMemo as useMemo2, useRef as useRef2 } from "react";
3
- import { useSyncExternalStore as useSyncExternalStore2 } from "react";
1
+ // src/OIMDBContext.ts
2
+ import { createContext } from "react";
3
+ var OIMDBContext = createContext(void 0);
4
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
- };
111
-
112
- // src/hooks/signal.ts
113
- import { useEffect, useLayoutEffect, useReducer } from "react";
114
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
115
- function useForceUpdate() {
116
- const [, force] = useReducer((c) => c + 1 | 0, 0);
117
- return force;
5
+ // src/OIMDBProvider.tsx
6
+ import { jsx } from "react/jsx-runtime";
7
+ function OIMDBProvider({ db, children }) {
8
+ return /* @__PURE__ */ jsx(OIMDBContext.Provider, { value: db, children });
118
9
  }
119
- var useSelectEntityByPkSignal = (reactiveCollection, pk) => {
120
- const force = useForceUpdate();
121
- useIsomorphicLayoutEffect(
122
- () => reactiveCollection.subscribeOnKey(pk, force),
123
- [reactiveCollection, pk]
124
- );
125
- return reactiveCollection.getOneByPk(pk);
126
- };
127
- var useSelectPksByIndexKeyArrayBasedSignal = (reactiveIndex, key) => {
128
- const force = useForceUpdate();
129
- useIsomorphicLayoutEffect(
130
- () => reactiveIndex.subscribeOnKey(key, force),
131
- [reactiveIndex, key]
132
- );
133
- return reactiveIndex.getPksByKey(key);
134
- };
135
- var useSelectPksByIndexKeySetBasedSignal = (reactiveIndex, key) => {
136
- const force = useForceUpdate();
137
- useIsomorphicLayoutEffect(
138
- () => reactiveIndex.subscribeOnKey(key, force),
139
- [reactiveIndex, key]
140
- );
141
- return reactiveIndex.getPksByKey(key);
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
- };
159
10
 
160
- // src/hooks/index.ts
161
- var EMPTY_ARRAY = [];
162
- var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
163
- const prevKeysRef = useRef2();
164
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
165
- if (!keysAreEqual) {
166
- prevKeysRef.current = keys;
11
+ // src/useOIMDB.ts
12
+ import { useContext } from "react";
13
+ function useOIMDB() {
14
+ const db = useContext(OIMDBContext);
15
+ if (db === void 0) {
16
+ throw new Error(
17
+ "useOIMDB: no <OIMDBProvider db={...}> above this component. The hooks that build a computed for the component take the database from it."
18
+ );
167
19
  }
168
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
169
- const snapshotValueRef = useRef2();
170
- const subscribe = useMemo2(() => {
171
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
172
- return (onStoreChange) => {
173
- const updateSnapshot = () => {
174
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
175
- onStoreChange();
176
- };
177
- const unsubscribe = reactiveIndex.subscribeOnKeys(
178
- stableKeys,
179
- updateSnapshot
180
- );
181
- return () => {
182
- unsubscribe();
183
- };
184
- };
185
- }, [reactiveIndex, stableKeys]);
186
- const getSnapshot = useMemo2(() => {
187
- return () => {
188
- return snapshotValueRef.current;
189
- };
190
- }, []);
191
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
192
- return snapshot;
193
- };
194
- var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
195
- const snapshotValueRef = useRef2();
196
- const subscribe = useMemo2(() => {
197
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
198
- return (onStoreChange) => {
199
- const updateSnapshot = () => {
200
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
201
- onStoreChange();
202
- };
203
- return reactiveIndex.subscribeOnKey(key, updateSnapshot);
204
- };
205
- }, [key, reactiveIndex]);
206
- const getSnapshot = useMemo2(() => {
207
- return () => {
208
- return snapshotValueRef.current;
209
- };
210
- }, []);
211
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
212
- return snapshot;
213
- };
214
- var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
215
- const prevKeysRef = useRef2();
216
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
217
- if (!keysAreEqual) {
218
- prevKeysRef.current = keys;
20
+ return db;
21
+ }
22
+
23
+ // src/useOIMValue.ts
24
+ import { useMemo } from "react";
25
+ import { OIM_FROZEN_EMPTY_SLOTS, oimRefreshSourceComputeds } from "@oimdb/core";
26
+
27
+ // src/OIMSnapshotVersion.ts
28
+ import { OIMEffectService } from "@oimdb/core";
29
+ var OIMSnapshotVersion = class {
30
+ constructor() {
31
+ this.version = 0;
32
+ this.getVersion = () => this.version;
219
33
  }
220
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
221
- const snapshotValueRef = useRef2();
222
- const subscribe = useMemo2(() => {
223
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
224
- return (onStoreChange) => {
225
- const updateSnapshot = () => {
226
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
227
- onStoreChange();
228
- };
229
- const unsubscribe = reactiveIndex.subscribeOnKeys(
230
- stableKeys,
231
- updateSnapshot
232
- );
233
- return () => {
234
- unsubscribe();
235
- };
236
- };
237
- }, [reactiveIndex, stableKeys]);
238
- const getSnapshot = useMemo2(() => {
239
- return () => {
240
- return snapshotValueRef.current;
241
- };
242
- }, []);
243
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
244
- return snapshot;
245
- };
246
- var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
247
- const prevKeyRef = useRef2();
248
- const keyAreEqual = prevKeyRef.current === key;
249
- if (!keyAreEqual) {
250
- prevKeyRef.current = key;
34
+ subscribeEffect(channelMap, sourceSlots, sourceComputeds, onStoreChange) {
35
+ this.version++;
36
+ return OIMEffectService.INSTANCE.subscribeEffect(channelMap, sourceSlots, sourceComputeds, () => {
37
+ this.version++;
38
+ onStoreChange();
39
+ });
251
40
  }
252
- const snapshotValueRef = useRef2();
253
- const subscribe = useMemo2(() => {
254
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
255
- return (onStoreChange) => {
256
- const prevKey = prevKeyRef.current;
257
- if (!prevKey) {
258
- return () => {
259
- };
260
- }
261
- const updateSnapshot = () => {
262
- snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
263
- onStoreChange();
264
- };
265
- const unsubscribe = reactiveIndex.subscribeOnKey(
266
- prevKey,
267
- updateSnapshot
268
- );
269
- return () => {
270
- unsubscribe();
271
- };
272
- };
273
- }, [key, reactiveIndex]);
274
- const getSnapshot = useMemo2(() => {
275
- return () => {
276
- return snapshotValueRef.current;
277
- };
278
- }, []);
279
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
280
- return snapshot;
281
41
  };
282
- var useSelectEntityByPk = (reactiveCollection, pk) => {
283
- const getSnapshot = useMemo2(
284
- () => () => reactiveCollection.getOneByPk(pk),
285
- [pk, reactiveCollection]
286
- );
287
- const subscribe = useMemo2(() => {
288
- return (onStoreChange) => {
289
- return reactiveCollection.subscribeOnKey(pk, onStoreChange);
42
+
43
+ // src/useOIMSnapshotVersion.ts
44
+ import { useSyncExternalStore } from "react";
45
+ function useOIMSnapshotVersion(snapshotVersion, subscribe) {
46
+ return useSyncExternalStore(subscribe, snapshotVersion.getVersion, snapshotVersion.getVersion);
47
+ }
48
+
49
+ // src/useOIMValue.ts
50
+ function useOIMValue(binding) {
51
+ const db = useOIMDB();
52
+ const sourceComputeds = binding.sourceComputeds ?? OIM_FROZEN_EMPTY_SLOTS;
53
+ const store = useMemo(() => {
54
+ const snapshotVersion = new OIMSnapshotVersion();
55
+ return {
56
+ snapshotVersion,
57
+ subscribe: (onStoreChange) => snapshotVersion.subscribeEffect(
58
+ db,
59
+ binding.sourceSlots ?? OIM_FROZEN_EMPTY_SLOTS,
60
+ sourceComputeds,
61
+ onStoreChange
62
+ )
290
63
  };
291
- }, [pk, reactiveCollection]);
292
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
293
- return snapshot;
294
- };
295
- var useSelectValueByObjectKey = (reactiveObject, key) => {
296
- const getSnapshot = useMemo2(
297
- () => () => reactiveObject.get(key),
298
- [key, reactiveObject]
64
+ }, [db, binding, sourceComputeds]);
65
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
66
+ oimRefreshSourceComputeds(sourceComputeds);
67
+ return binding.derive();
68
+ }
69
+
70
+ // src/useOIMComputedValue.ts
71
+ import { useMemo as useMemo2 } from "react";
72
+ function useOIMComputedValue(computedSlot) {
73
+ const binding = useMemo2(() => ({
74
+ sourceComputeds: [computedSlot],
75
+ derive: () => computedSlot.value
76
+ }), [computedSlot]);
77
+ return useOIMValue(binding);
78
+ }
79
+
80
+ // src/useOIMSlotByKey.ts
81
+ import { useMemo as useMemo3, useRef } from "react";
82
+ import { OIM_FROZEN_EMPTY_SLOTS as OIM_FROZEN_EMPTY_SLOTS2 } from "@oimdb/core";
83
+
84
+ // src/oimAssertOneChannel.ts
85
+ function oimAssertOneChannel(channelMap, slot, door) {
86
+ const storeSlot = slot.storeSlot;
87
+ if (storeSlot === void 0 || storeSlot.channelSlot === channelMap.defaultChannelSlot) return;
88
+ throw new Error(
89
+ `${door}: the slot was filed on another OIMDB than the one <OIMDBProvider> was given, so a write would never wake this component. Build the services and their store slots on the one OIMDB the provider holds.`
299
90
  );
300
- const subscribe = useMemo2(() => {
301
- return (onStoreChange) => {
302
- return reactiveObject.subscribeOnKey(key, onStoreChange);
303
- };
304
- }, [key, reactiveObject]);
305
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
306
- return snapshot;
307
- };
308
- var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
309
- const prevKeysRef = useRef2();
310
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
311
- if (!keysAreEqual) {
312
- prevKeysRef.current = keys;
313
- }
314
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
315
- const snapshotRef = useRef2();
316
- const subscribe = useMemo2(() => {
317
- snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
318
- return (onStoreChange) => {
319
- const updateSnapshot = () => {
320
- snapshotRef.current = stableKeys.map(
321
- (key) => reactiveObject.get(key)
322
- );
323
- onStoreChange();
324
- };
325
- const unsubscribe = reactiveObject.subscribeOnKeys(
326
- stableKeys,
327
- updateSnapshot
328
- );
329
- return () => {
330
- unsubscribe();
331
- };
332
- };
333
- }, [reactiveObject, stableKeys]);
334
- const getSnapshot = useMemo2(() => {
335
- return () => {
336
- return snapshotRef.current;
337
- };
338
- }, []);
339
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
340
- return snapshot;
341
- };
342
- function arraysEqual(a, b) {
343
- if (a === b) return true;
344
- if (a.length !== b.length) return false;
345
- for (let i = 0; i < a.length; i++) {
346
- if (a[i] !== b[i]) return false;
347
- }
348
- return true;
349
91
  }
350
- var useSelectEntitiesByPks = (reactiveCollection, pks) => {
351
- const prevPksRef = useRef2();
352
- const pksAreEqual = arraysEqual(prevPksRef.current || EMPTY_ARRAY, pks);
353
- if (!pksAreEqual) {
354
- prevPksRef.current = pks;
355
- }
356
- const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
357
- const snapshotRef = useRef2();
358
- const subscribe = useMemo2(() => {
359
- snapshotRef.current = stablePks.map(
360
- (pk) => reactiveCollection.getOneByPk(pk)
361
- );
362
- return (onStoreChange) => {
363
- const updateSnapshot = () => {
364
- snapshotRef.current = stablePks.map(
365
- (pk) => reactiveCollection.getOneByPk(pk)
366
- );
367
- onStoreChange();
368
- };
369
- const unsubscribe = reactiveCollection.subscribeOnKeys(
370
- stablePks,
371
- updateSnapshot
372
- );
373
- return () => {
374
- unsubscribe();
375
- };
376
- };
377
- }, [reactiveCollection, stablePks]);
378
- const getSnapshot = useMemo2(() => {
379
- return () => {
380
- return snapshotRef.current;
381
- };
382
- }, []);
383
- const snapshot = useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
384
- return snapshot;
385
- };
386
- var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
387
- const snapshotRef = useRef2();
388
- const subscribe = useMemo2(() => {
389
- const readSnapshot = () => Array.from(
390
- reactiveIndex.getPksByKey(key),
391
- (pk) => reactiveCollection.getOneByPk(pk)
392
- );
393
- const readPks = () => Array.from(reactiveIndex.getPksByKey(key));
394
- snapshotRef.current = readSnapshot();
395
- return (onStoreChange) => {
396
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
397
- snapshotRef.current = readSnapshot();
398
- onStoreChange();
399
- });
400
- const resubscribeCollection = () => {
401
- unsubscribeFromCollection();
402
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
403
- readPks(),
404
- () => {
405
- snapshotRef.current = readSnapshot();
406
- onStoreChange();
407
- }
408
- );
409
- };
410
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
411
- resubscribeCollection();
412
- snapshotRef.current = readSnapshot();
413
- onStoreChange();
414
- });
415
- return () => {
416
- unsubscribeFromIndex();
417
- unsubscribeFromCollection();
418
- };
419
- };
420
- }, [key, reactiveCollection, reactiveIndex]);
421
- const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
422
- return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
423
- };
424
- var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
425
- const prevKeysRef = useRef2();
426
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
427
- if (!keysAreEqual) {
428
- prevKeysRef.current = keys;
92
+
93
+ // src/useOIMSlotByKey.ts
94
+ var sourcesOfRootSlot = (rootSlot) => [[rootSlot], OIM_FROZEN_EMPTY_SLOTS2];
95
+ function useOIMSlotByKey(hookName, slotCollectionService, storeSlot, key, sourcesOfSlot = sourcesOfRootSlot) {
96
+ const db = useOIMDB();
97
+ const lastSeenKeyRef = useRef(key);
98
+ const settledKeyRef = useRef(key);
99
+ if (key !== lastSeenKeyRef.current) {
100
+ lastSeenKeyRef.current = key;
101
+ if (slotCollectionService.getSlotByKey(storeSlot, key) !== slotCollectionService.getSlotByKey(storeSlot, settledKeyRef.current)) {
102
+ settledKeyRef.current = key;
103
+ }
429
104
  }
430
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
431
- const snapshotRef = useRef2();
432
- const subscribe = useMemo2(() => {
433
- const readPks = () => stableKeys.flatMap(
434
- (key) => Array.from(reactiveIndex.getPksByKey(key))
435
- );
436
- const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
437
- snapshotRef.current = readSnapshot();
438
- return (onStoreChange) => {
439
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
440
- snapshotRef.current = readSnapshot();
441
- onStoreChange();
442
- });
443
- const resubscribeCollection = () => {
444
- unsubscribeFromCollection();
445
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
446
- readPks(),
447
- () => {
448
- snapshotRef.current = readSnapshot();
449
- onStoreChange();
450
- }
451
- );
452
- };
453
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
454
- stableKeys,
455
- () => {
456
- resubscribeCollection();
457
- snapshotRef.current = readSnapshot();
458
- onStoreChange();
105
+ const settledKey = settledKeyRef.current;
106
+ const store = useMemo3(() => {
107
+ const snapshotVersion = new OIMSnapshotVersion();
108
+ let subscribedSlot = void 0;
109
+ return {
110
+ snapshotVersion,
111
+ subscribe: (onStoreChange) => {
112
+ const holdingSlot = slotCollectionService.holdSlotByKey(storeSlot, settledKey);
113
+ try {
114
+ oimAssertOneChannel(db, holdingSlot, hookName);
115
+ const [sourceSlots, sourceComputeds] = sourcesOfSlot(holdingSlot);
116
+ const disposeEffect = snapshotVersion.subscribeEffect(db, sourceSlots, sourceComputeds, onStoreChange);
117
+ subscribedSlot = holdingSlot;
118
+ return () => {
119
+ subscribedSlot = void 0;
120
+ disposeEffect();
121
+ };
122
+ } finally {
123
+ holdingSlot.owner.unholdSlot(holdingSlot);
459
124
  }
460
- );
461
- return () => {
462
- unsubscribeFromIndex();
463
- unsubscribeFromCollection();
464
- };
125
+ },
126
+ getSlot: () => subscribedSlot ?? slotCollectionService.getSlotByKey(storeSlot, settledKey)
465
127
  };
466
- }, [reactiveCollection, reactiveIndex, stableKeys]);
467
- const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
468
- return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
469
- };
470
- var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
471
- const snapshotRef = useRef2();
472
- const subscribe = useMemo2(() => {
473
- const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
474
- const readPks = () => reactiveIndex.getPksByKey(key);
475
- snapshotRef.current = readSnapshot();
476
- return (onStoreChange) => {
477
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
478
- snapshotRef.current = readSnapshot();
479
- onStoreChange();
480
- });
481
- const resubscribeCollection = () => {
482
- unsubscribeFromCollection();
483
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
484
- readPks(),
485
- () => {
486
- snapshotRef.current = readSnapshot();
487
- onStoreChange();
488
- }
489
- );
490
- };
491
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
492
- resubscribeCollection();
493
- snapshotRef.current = readSnapshot();
494
- onStoreChange();
495
- });
496
- return () => {
497
- unsubscribeFromIndex();
498
- unsubscribeFromCollection();
499
- };
500
- };
501
- }, [key, reactiveCollection, reactiveIndex]);
502
- const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
503
- return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
504
- };
505
- var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
506
- const prevKeysRef = useRef2();
507
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
508
- if (!keysAreEqual) {
509
- prevKeysRef.current = keys;
510
- }
511
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
512
- const snapshotRef = useRef2();
513
- const subscribe = useMemo2(() => {
514
- const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
515
- const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
516
- snapshotRef.current = readSnapshot();
517
- return (onStoreChange) => {
518
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
519
- snapshotRef.current = readSnapshot();
520
- onStoreChange();
521
- });
522
- const resubscribeCollection = () => {
523
- unsubscribeFromCollection();
524
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
525
- readPks(),
526
- () => {
527
- snapshotRef.current = readSnapshot();
528
- onStoreChange();
529
- }
530
- );
531
- };
532
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
533
- stableKeys,
534
- () => {
535
- resubscribeCollection();
536
- snapshotRef.current = readSnapshot();
537
- onStoreChange();
128
+ }, [hookName, db, slotCollectionService, storeSlot, settledKey, sourcesOfSlot]);
129
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
130
+ return store.getSlot();
131
+ }
132
+
133
+ // src/useOIMEntity.ts
134
+ function useOIMEntity(entityCollectionService, entityStoreSlot, key) {
135
+ return useOIMSlotByKey("useOIMEntity", entityCollectionService, entityStoreSlot, key).entity;
136
+ }
137
+
138
+ // src/useOIMSingleSlot.ts
139
+ import { useMemo as useMemo4 } from "react";
140
+ function useOIMSingleSlot(hookName, singleSlotService, singleSlotStoreSlot) {
141
+ const db = useOIMDB();
142
+ const store = useMemo4(() => {
143
+ const snapshotVersion = new OIMSnapshotVersion();
144
+ let subscribedSlot = void 0;
145
+ return {
146
+ snapshotVersion,
147
+ subscribe: (onStoreChange) => {
148
+ const holdingSlot = singleSlotService.holdSingleSlot(singleSlotStoreSlot);
149
+ try {
150
+ oimAssertOneChannel(db, holdingSlot, hookName);
151
+ const disposeEffect = snapshotVersion.subscribeEffect(db, [holdingSlot], [], onStoreChange);
152
+ subscribedSlot = holdingSlot;
153
+ return () => {
154
+ subscribedSlot = void 0;
155
+ disposeEffect();
156
+ };
157
+ } finally {
158
+ holdingSlot.owner.unholdSlot(holdingSlot);
538
159
  }
539
- );
540
- return () => {
541
- unsubscribeFromIndex();
542
- unsubscribeFromCollection();
543
- };
160
+ },
161
+ getSlot: () => subscribedSlot ?? singleSlotService.getSingleSlot(singleSlotStoreSlot)
544
162
  };
545
- }, [reactiveCollection, reactiveIndex, stableKeys]);
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;
163
+ }, [hookName, db, singleSlotService, singleSlotStoreSlot]);
164
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
165
+ return store.getSlot();
166
+ }
167
+
168
+ // src/useOIMSingleEntity.ts
169
+ function useOIMSingleEntity(singleSlotService, singleSlotStoreSlot) {
170
+ return useOIMSingleSlot("useOIMSingleEntity", singleSlotService, singleSlotStoreSlot).entity;
171
+ }
172
+
173
+ // src/useOIMSingleBucketSlots.ts
174
+ function useOIMSingleBucketSlots(singleSlotService, singleSlotStoreSlot) {
175
+ const bucketSlot = useOIMSingleSlot("useOIMSingleBucketSlots", singleSlotService, singleSlotStoreSlot);
176
+ return Array.from(singleSlotService.slotContentService.getBucketMemberSlots(bucketSlot));
177
+ }
178
+
179
+ // src/useOIMField.ts
180
+ import { useMemo as useMemo5 } from "react";
181
+
182
+ // src/holdWhileSubscribed.ts
183
+ import { oimHoldSlots, oimUnholdSlotsIfAlive } from "@oimdb/core";
184
+ function holdWhileSubscribed(slots, subscribe) {
185
+ oimHoldSlots(slots);
186
+ const giveEveryHoldBack = () => {
187
+ oimUnholdSlotsIfAlive(slots);
188
+ };
189
+ let disposer;
190
+ try {
191
+ disposer = subscribe();
192
+ } catch (failure) {
193
+ giveEveryHoldBack();
194
+ throw failure;
554
195
  }
555
- return equal ? prevRef.current : key;
196
+ return () => {
197
+ try {
198
+ disposer();
199
+ } finally {
200
+ giveEveryHoldBack();
201
+ }
202
+ };
556
203
  }
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
- };
204
+
205
+ // src/useOIMField.ts
206
+ function useOIMField(entityCollectionService, slot, name) {
207
+ const db = useOIMDB();
208
+ const store = useMemo5(() => {
209
+ oimAssertOneChannel(db, slot, "useOIMField");
210
+ const holds = [slot];
211
+ const snapshotVersion = new OIMSnapshotVersion();
212
+ return {
213
+ snapshotVersion,
214
+ subscribe: (onStoreChange) => holdWhileSubscribed(holds, () => snapshotVersion.subscribeEffect(
215
+ db,
216
+ [entityCollectionService.getColumn(slot, name)],
217
+ [],
218
+ onStoreChange
219
+ ))
665
220
  };
666
- }, [stableKey, reactiveCollection, reactiveIndex]);
667
- const getSnapshot = useMemo2(() => () => snapshotRef.current, []);
668
- return useSyncExternalStore2(subscribe, getSnapshot, getSnapshot);
669
- };
221
+ }, [db, entityCollectionService, slot, name]);
222
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
223
+ return slot.entity?.[name];
224
+ }
670
225
 
671
- // src/context/index.tsx
672
- import { createContext, useContext } from "react";
673
- import { jsx } from "react/jsx-runtime";
674
- var OIMCollectionsContext = createContext(
675
- {}
676
- );
677
- function createOIMCollectionsContext() {
678
- return createContext({});
226
+ // src/useOIMBucketSlots.ts
227
+ import { useMemo as useMemo6 } from "react";
228
+ function useOIMBucketSlots(bucketCollectionService, bucketSlot) {
229
+ const db = useOIMDB();
230
+ const binding = useMemo6(() => {
231
+ oimAssertOneChannel(db, bucketSlot, "useOIMBucketSlots");
232
+ return { sourceSlots: [bucketSlot], derive: () => bucketSlot };
233
+ }, [db, bucketSlot]);
234
+ const { changedAtRevision, changedAtEpoch } = useOIMValue(binding);
235
+ return useMemo6(
236
+ () => Array.from(bucketCollectionService.slotContentService.getBucketMemberSlots(bucketSlot)),
237
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the bucket slot's change stamp moves exactly when its members do
238
+ [bucketCollectionService, bucketSlot, changedAtRevision, changedAtEpoch]
239
+ );
679
240
  }
680
- function OIMCollectionsProvider({ collections, children, context }) {
681
- const ContextToUse = context || OIMCollectionsContext;
682
- return /* @__PURE__ */ jsx(ContextToUse.Provider, { value: collections, children });
241
+
242
+ // src/useOIMBucketSlotsByKey.ts
243
+ function useOIMBucketSlotsByKey(bucketCollectionService, bucketStoreSlot, key) {
244
+ const bucketSlot = useOIMSlotByKey("useOIMBucketSlotsByKey", bucketCollectionService, bucketStoreSlot, key);
245
+ return Array.from(bucketCollectionService.slotContentService.getBucketMemberSlots(bucketSlot));
683
246
  }
684
- function useOIMCollectionsContext(context) {
685
- const contextToUse = context || OIMCollectionsContext;
686
- const collections = useContext(contextToUse);
687
- if (!collections || Object.keys(collections).length === 0) {
688
- throw new Error(
689
- "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
690
- );
247
+
248
+ // src/useOIMSlotsByKeys.ts
249
+ import { useEffect, useRef as useRef2, useState } from "react";
250
+ import { oimSameKeySequence } from "@oimdb/core";
251
+ function useOIMSlotsByKeys(slotCollectionService, storeSlot, keys) {
252
+ const settledKeys = useRef2(keys);
253
+ const previousKeys = settledKeys.current;
254
+ if (!oimSameKeySequence(previousKeys, keys)) settledKeys.current = keys;
255
+ const keysToHold = settledKeys.current;
256
+ const [, resolveAgain] = useState(0);
257
+ const resolvedSlots = useRef2(void 0);
258
+ const resolvedKeys = useRef2(keysToHold);
259
+ const resolvedSlotCollectionService = useRef2(slotCollectionService);
260
+ const resolvedStoreSlot = useRef2(storeSlot);
261
+ if (resolvedSlots.current === void 0 || resolvedKeys.current !== keysToHold || resolvedSlotCollectionService.current !== slotCollectionService || resolvedStoreSlot.current !== storeSlot) {
262
+ const slots2 = [];
263
+ for (let i = 0; i < keysToHold.length; i++) {
264
+ slots2.push(slotCollectionService.getSlotByKey(storeSlot, keysToHold[i]));
265
+ }
266
+ resolvedSlots.current = slots2;
267
+ resolvedKeys.current = keysToHold;
268
+ resolvedSlotCollectionService.current = slotCollectionService;
269
+ resolvedStoreSlot.current = storeSlot;
691
270
  }
692
- return collections;
271
+ const slots = resolvedSlots.current;
272
+ useEffect(() => {
273
+ const renderedSlots = resolvedSlots.current;
274
+ const heldSlots = [];
275
+ let someKeyNamesAnotherIdentityNow = false;
276
+ for (let i = 0; i < keysToHold.length; i++) {
277
+ const slot = slotCollectionService.holdSlotByKey(storeSlot, keysToHold[i]);
278
+ heldSlots.push(slot);
279
+ if (slot !== renderedSlots?.[i]) someKeyNamesAnotherIdentityNow = true;
280
+ }
281
+ if (someKeyNamesAnotherIdentityNow) {
282
+ resolvedSlots.current = void 0;
283
+ resolveAgain((version) => version + 1);
284
+ }
285
+ return () => {
286
+ for (let i = 0; i < heldSlots.length; i++) {
287
+ const heldSlot = heldSlots[i];
288
+ heldSlot.owner.unholdSlot(heldSlot);
289
+ }
290
+ };
291
+ }, [slotCollectionService, storeSlot, keysToHold]);
292
+ return slots;
293
+ }
294
+
295
+ // src/useOIMComputedValueByKey.ts
296
+ import { OIM_FROZEN_EMPTY_SLOTS as OIM_FROZEN_EMPTY_SLOTS3 } from "@oimdb/core";
297
+ var sourcesOfComputedSlot = (computedSlot) => [OIM_FROZEN_EMPTY_SLOTS3, [computedSlot]];
298
+ function useOIMComputedValueByKey(computedCollectionService, computedStoreSlot, key) {
299
+ const computedSlot = useOIMSlotByKey(
300
+ "useOIMComputedValueByKey",
301
+ computedCollectionService,
302
+ computedStoreSlot,
303
+ key,
304
+ sourcesOfComputedSlot
305
+ );
306
+ return computedSlot.computedValueService.getValueForSlot(computedSlot);
307
+ }
308
+
309
+ // src/useOIMSingleComputedValue.ts
310
+ import { useMemo as useMemo7 } from "react";
311
+ function useOIMSingleComputedValue(computedService, derive, createSources, dependencyList) {
312
+ const db = useOIMDB();
313
+ const computedStoreSlot = useMemo7(
314
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
315
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
316
+ [db, computedService, ...dependencyList]
317
+ );
318
+ return useOIMComputedValueByKey(
319
+ computedService.computedCollectionService,
320
+ computedStoreSlot,
321
+ void 0
322
+ );
323
+ }
324
+
325
+ // src/useOIMComputedCollection.ts
326
+ import { useMemo as useMemo8 } from "react";
327
+ function useOIMComputedCollection(computedService, derive, createSources, dependencyList) {
328
+ const db = useOIMDB();
329
+ return useMemo8(
330
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
331
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
332
+ [db, computedService, ...dependencyList]
333
+ );
693
334
  }
694
335
  export {
695
- OIMCollectionsContext,
696
- OIMCollectionsProvider,
697
- createOIMCollectionsContext,
698
- useOIMCollectionsContext,
699
- useSelectEntitiesByCompositeIndexKeyArrayBased,
700
- useSelectEntitiesByCompositeIndexKeySetBased,
701
- useSelectEntitiesByGlobalIndexArrayBased,
702
- useSelectEntitiesByGlobalIndexSetBased,
703
- useSelectEntitiesByIndexKeyArrayBased,
704
- useSelectEntitiesByIndexKeySetBased,
705
- useSelectEntitiesByIndexKeysArrayBased,
706
- useSelectEntitiesByIndexKeysSetBased,
707
- useSelectEntitiesByPks,
708
- useSelectEntityByPk,
709
- useSelectEntityByPkSignal,
710
- useSelectPksByCompositeIndexKeyArrayBased,
711
- useSelectPksByCompositeIndexKeySetBased,
712
- useSelectPksByGlobalIndexArrayBased,
713
- useSelectPksByGlobalIndexArrayBasedSignal,
714
- useSelectPksByGlobalIndexSetBased,
715
- useSelectPksByGlobalIndexSetBasedSignal,
716
- useSelectPksByIndexKeyArrayBased,
717
- useSelectPksByIndexKeyArrayBasedSignal,
718
- useSelectPksByIndexKeySetBased,
719
- useSelectPksByIndexKeySetBasedSignal,
720
- useSelectPksByIndexKeysArrayBased,
721
- useSelectPksByIndexKeysSetBased,
722
- useSelectValueByObjectKey,
723
- useSelectValuesByObjectKeys
336
+ OIMDBContext,
337
+ OIMDBProvider,
338
+ useOIMBucketSlots,
339
+ useOIMBucketSlotsByKey,
340
+ useOIMComputedCollection,
341
+ useOIMComputedValue,
342
+ useOIMComputedValueByKey,
343
+ useOIMDB,
344
+ useOIMEntity,
345
+ useOIMField,
346
+ useOIMSingleBucketSlots,
347
+ useOIMSingleComputedValue,
348
+ useOIMSingleEntity,
349
+ useOIMSlotsByKeys,
350
+ useOIMValue
724
351
  };