@oimdb/react 2.1.2 → 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,470 +1,351 @@
1
- // src/hooks/index.ts
2
- import { useMemo, useRef } from "react";
3
- import { useSyncExternalStore } from "react";
1
+ // src/OIMDBContext.ts
2
+ import { createContext } from "react";
3
+ var OIMDBContext = createContext(void 0);
4
4
 
5
- // src/hooks/signal.ts
6
- import { useEffect, useLayoutEffect, useReducer } from "react";
7
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
8
- function useForceUpdate() {
9
- const [, force] = useReducer((c) => c + 1 | 0, 0);
10
- 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 });
11
9
  }
12
- var useSelectEntityByPkSignal = (reactiveCollection, pk) => {
13
- const force = useForceUpdate();
14
- useIsomorphicLayoutEffect(
15
- () => reactiveCollection.subscribeOnKey(pk, force),
16
- [reactiveCollection, pk]
17
- );
18
- return reactiveCollection.getOneByPk(pk);
19
- };
20
- var useSelectPksByIndexKeyArrayBasedSignal = (reactiveIndex, key) => {
21
- const force = useForceUpdate();
22
- useIsomorphicLayoutEffect(
23
- () => reactiveIndex.subscribeOnKey(key, force),
24
- [reactiveIndex, key]
25
- );
26
- return reactiveIndex.getPksByKey(key);
27
- };
28
- var useSelectPksByIndexKeySetBasedSignal = (reactiveIndex, key) => {
29
- const force = useForceUpdate();
30
- useIsomorphicLayoutEffect(
31
- () => reactiveIndex.subscribeOnKey(key, force),
32
- [reactiveIndex, key]
33
- );
34
- return reactiveIndex.getPksByKey(key);
35
- };
36
10
 
37
- // src/hooks/index.ts
38
- var EMPTY_ARRAY = [];
39
- var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
40
- const prevKeysRef = useRef();
41
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
42
- if (!keysAreEqual) {
43
- 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
+ );
44
19
  }
45
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
46
- const snapshotValueRef = useRef();
47
- const subscribe = useMemo(() => {
48
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
49
- return (onStoreChange) => {
50
- const updateSnapshot = () => {
51
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
52
- onStoreChange();
53
- };
54
- const unsubscribe = reactiveIndex.subscribeOnKeys(
55
- stableKeys,
56
- updateSnapshot
57
- );
58
- return () => {
59
- unsubscribe();
60
- };
61
- };
62
- }, [reactiveIndex, stableKeys]);
63
- const getSnapshot = useMemo(() => {
64
- return () => {
65
- return snapshotValueRef.current;
66
- };
67
- }, []);
68
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
69
- return snapshot;
70
- };
71
- var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
72
- const snapshotValueRef = useRef();
73
- const subscribe = useMemo(() => {
74
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
75
- return (onStoreChange) => {
76
- const updateSnapshot = () => {
77
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
78
- onStoreChange();
79
- };
80
- return reactiveIndex.subscribeOnKey(key, updateSnapshot);
81
- };
82
- }, [key, reactiveIndex]);
83
- const getSnapshot = useMemo(() => {
84
- return () => {
85
- return snapshotValueRef.current;
86
- };
87
- }, []);
88
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
89
- return snapshot;
90
- };
91
- var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
92
- const prevKeysRef = useRef();
93
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
94
- if (!keysAreEqual) {
95
- 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;
96
33
  }
97
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
98
- const snapshotValueRef = useRef();
99
- const subscribe = useMemo(() => {
100
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
101
- return (onStoreChange) => {
102
- const updateSnapshot = () => {
103
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
104
- onStoreChange();
105
- };
106
- const unsubscribe = reactiveIndex.subscribeOnKeys(
107
- stableKeys,
108
- updateSnapshot
109
- );
110
- return () => {
111
- unsubscribe();
112
- };
113
- };
114
- }, [reactiveIndex, stableKeys]);
115
- const getSnapshot = useMemo(() => {
116
- return () => {
117
- return snapshotValueRef.current;
118
- };
119
- }, []);
120
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
121
- return snapshot;
122
- };
123
- var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
124
- const prevKeyRef = useRef();
125
- const keyAreEqual = prevKeyRef.current === key;
126
- if (!keyAreEqual) {
127
- 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
+ });
128
40
  }
129
- const snapshotValueRef = useRef();
130
- const subscribe = useMemo(() => {
131
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
132
- return (onStoreChange) => {
133
- const prevKey = prevKeyRef.current;
134
- if (!prevKey) {
135
- return () => {
136
- };
137
- }
138
- const updateSnapshot = () => {
139
- snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
140
- onStoreChange();
141
- };
142
- const unsubscribe = reactiveIndex.subscribeOnKey(
143
- prevKey,
144
- updateSnapshot
145
- );
146
- return () => {
147
- unsubscribe();
148
- };
149
- };
150
- }, [key, reactiveIndex]);
151
- const getSnapshot = useMemo(() => {
152
- return () => {
153
- return snapshotValueRef.current;
154
- };
155
- }, []);
156
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
157
- return snapshot;
158
41
  };
159
- var useSelectEntityByPk = (reactiveCollection, pk) => {
160
- const getSnapshot = useMemo(
161
- () => () => reactiveCollection.getOneByPk(pk),
162
- [pk, reactiveCollection]
163
- );
164
- const subscribe = useMemo(() => {
165
- return (onStoreChange) => {
166
- 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
+ )
167
63
  };
168
- }, [pk, reactiveCollection]);
169
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
170
- return snapshot;
171
- };
172
- var useSelectValueByObjectKey = (reactiveObject, key) => {
173
- const getSnapshot = useMemo(
174
- () => () => reactiveObject.get(key),
175
- [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.`
176
90
  );
177
- const subscribe = useMemo(() => {
178
- return (onStoreChange) => {
179
- return reactiveObject.subscribeOnKey(key, onStoreChange);
180
- };
181
- }, [key, reactiveObject]);
182
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
183
- return snapshot;
184
- };
185
- var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
186
- const prevKeysRef = useRef();
187
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
188
- if (!keysAreEqual) {
189
- prevKeysRef.current = keys;
190
- }
191
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
192
- const snapshotRef = useRef();
193
- const subscribe = useMemo(() => {
194
- snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
195
- return (onStoreChange) => {
196
- const updateSnapshot = () => {
197
- snapshotRef.current = stableKeys.map(
198
- (key) => reactiveObject.get(key)
199
- );
200
- onStoreChange();
201
- };
202
- const unsubscribe = reactiveObject.subscribeOnKeys(
203
- stableKeys,
204
- updateSnapshot
205
- );
206
- return () => {
207
- unsubscribe();
208
- };
209
- };
210
- }, [reactiveObject, stableKeys]);
211
- const getSnapshot = useMemo(() => {
212
- return () => {
213
- return snapshotRef.current;
214
- };
215
- }, []);
216
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
217
- return snapshot;
218
- };
219
- function arraysEqual(a, b) {
220
- if (a === b) return true;
221
- if (a.length !== b.length) return false;
222
- for (let i = 0; i < a.length; i++) {
223
- if (a[i] !== b[i]) return false;
224
- }
225
- return true;
226
91
  }
227
- var useSelectEntitiesByPks = (reactiveCollection, pks) => {
228
- const prevPksRef = useRef();
229
- const pksAreEqual = arraysEqual(prevPksRef.current || EMPTY_ARRAY, pks);
230
- if (!pksAreEqual) {
231
- prevPksRef.current = pks;
232
- }
233
- const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
234
- const snapshotRef = useRef();
235
- const subscribe = useMemo(() => {
236
- snapshotRef.current = stablePks.map(
237
- (pk) => reactiveCollection.getOneByPk(pk)
238
- );
239
- return (onStoreChange) => {
240
- const updateSnapshot = () => {
241
- snapshotRef.current = stablePks.map(
242
- (pk) => reactiveCollection.getOneByPk(pk)
243
- );
244
- onStoreChange();
245
- };
246
- const unsubscribe = reactiveCollection.subscribeOnKeys(
247
- stablePks,
248
- updateSnapshot
249
- );
250
- return () => {
251
- unsubscribe();
252
- };
253
- };
254
- }, [reactiveCollection, stablePks]);
255
- const getSnapshot = useMemo(() => {
256
- return () => {
257
- return snapshotRef.current;
258
- };
259
- }, []);
260
- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
261
- return snapshot;
262
- };
263
- var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
264
- const snapshotRef = useRef();
265
- const subscribe = useMemo(() => {
266
- const readSnapshot = () => Array.from(
267
- reactiveIndex.getPksByKey(key),
268
- (pk) => reactiveCollection.getOneByPk(pk)
269
- );
270
- const readPks = () => Array.from(reactiveIndex.getPksByKey(key));
271
- snapshotRef.current = readSnapshot();
272
- return (onStoreChange) => {
273
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
274
- snapshotRef.current = readSnapshot();
275
- onStoreChange();
276
- });
277
- const resubscribeCollection = () => {
278
- unsubscribeFromCollection();
279
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
280
- readPks(),
281
- () => {
282
- snapshotRef.current = readSnapshot();
283
- onStoreChange();
284
- }
285
- );
286
- };
287
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
288
- resubscribeCollection();
289
- snapshotRef.current = readSnapshot();
290
- onStoreChange();
291
- });
292
- return () => {
293
- unsubscribeFromIndex();
294
- unsubscribeFromCollection();
295
- };
296
- };
297
- }, [key, reactiveCollection, reactiveIndex]);
298
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
299
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
300
- };
301
- var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
302
- const prevKeysRef = useRef();
303
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
304
- if (!keysAreEqual) {
305
- 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
+ }
306
104
  }
307
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
308
- const snapshotRef = useRef();
309
- const subscribe = useMemo(() => {
310
- const readPks = () => stableKeys.flatMap(
311
- (key) => Array.from(reactiveIndex.getPksByKey(key))
312
- );
313
- const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
314
- snapshotRef.current = readSnapshot();
315
- return (onStoreChange) => {
316
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
317
- snapshotRef.current = readSnapshot();
318
- onStoreChange();
319
- });
320
- const resubscribeCollection = () => {
321
- unsubscribeFromCollection();
322
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
323
- readPks(),
324
- () => {
325
- snapshotRef.current = readSnapshot();
326
- onStoreChange();
327
- }
328
- );
329
- };
330
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
331
- stableKeys,
332
- () => {
333
- resubscribeCollection();
334
- snapshotRef.current = readSnapshot();
335
- 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);
336
124
  }
337
- );
338
- return () => {
339
- unsubscribeFromIndex();
340
- unsubscribeFromCollection();
341
- };
125
+ },
126
+ getSlot: () => subscribedSlot ?? slotCollectionService.getSlotByKey(storeSlot, settledKey)
342
127
  };
343
- }, [reactiveCollection, reactiveIndex, stableKeys]);
344
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
345
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
346
- };
347
- var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
348
- const snapshotRef = useRef();
349
- const subscribe = useMemo(() => {
350
- const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
351
- const readPks = () => reactiveIndex.getPksByKey(key);
352
- snapshotRef.current = readSnapshot();
353
- return (onStoreChange) => {
354
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
355
- snapshotRef.current = readSnapshot();
356
- onStoreChange();
357
- });
358
- const resubscribeCollection = () => {
359
- unsubscribeFromCollection();
360
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
361
- readPks(),
362
- () => {
363
- snapshotRef.current = readSnapshot();
364
- onStoreChange();
365
- }
366
- );
367
- };
368
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
369
- resubscribeCollection();
370
- snapshotRef.current = readSnapshot();
371
- onStoreChange();
372
- });
373
- return () => {
374
- unsubscribeFromIndex();
375
- unsubscribeFromCollection();
376
- };
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);
159
+ }
160
+ },
161
+ getSlot: () => subscribedSlot ?? singleSlotService.getSingleSlot(singleSlotStoreSlot)
377
162
  };
378
- }, [key, reactiveCollection, reactiveIndex]);
379
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
380
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
381
- };
382
- var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
383
- const prevKeysRef = useRef();
384
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
385
- if (!keysAreEqual) {
386
- prevKeysRef.current = keys;
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;
387
195
  }
388
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
389
- const snapshotRef = useRef();
390
- const subscribe = useMemo(() => {
391
- const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
392
- const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
393
- snapshotRef.current = readSnapshot();
394
- return (onStoreChange) => {
395
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
396
- snapshotRef.current = readSnapshot();
397
- onStoreChange();
398
- });
399
- const resubscribeCollection = () => {
400
- unsubscribeFromCollection();
401
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
402
- readPks(),
403
- () => {
404
- snapshotRef.current = readSnapshot();
405
- onStoreChange();
406
- }
407
- );
408
- };
409
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
410
- stableKeys,
411
- () => {
412
- resubscribeCollection();
413
- snapshotRef.current = readSnapshot();
414
- onStoreChange();
415
- }
416
- );
417
- return () => {
418
- unsubscribeFromIndex();
419
- unsubscribeFromCollection();
420
- };
196
+ return () => {
197
+ try {
198
+ disposer();
199
+ } finally {
200
+ giveEveryHoldBack();
201
+ }
202
+ };
203
+ }
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
+ ))
421
220
  };
422
- }, [reactiveCollection, reactiveIndex, stableKeys]);
423
- const getSnapshot = useMemo(() => () => snapshotRef.current, []);
424
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
425
- };
221
+ }, [db, entityCollectionService, slot, name]);
222
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
223
+ return slot.entity?.[name];
224
+ }
426
225
 
427
- // src/context/index.tsx
428
- import { createContext, useContext } from "react";
429
- import { jsx } from "react/jsx-runtime";
430
- var OIMCollectionsContext = createContext(
431
- {}
432
- );
433
- function createOIMCollectionsContext() {
434
- 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
+ );
435
240
  }
436
- function OIMCollectionsProvider({ collections, children, context }) {
437
- const ContextToUse = context || OIMCollectionsContext;
438
- 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));
439
246
  }
440
- function useOIMCollectionsContext(context) {
441
- const contextToUse = context || OIMCollectionsContext;
442
- const collections = useContext(contextToUse);
443
- if (!collections || Object.keys(collections).length === 0) {
444
- throw new Error(
445
- "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
446
- );
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;
447
270
  }
448
- 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
+ );
449
334
  }
450
335
  export {
451
- OIMCollectionsContext,
452
- OIMCollectionsProvider,
453
- createOIMCollectionsContext,
454
- useOIMCollectionsContext,
455
- useSelectEntitiesByIndexKeyArrayBased,
456
- useSelectEntitiesByIndexKeySetBased,
457
- useSelectEntitiesByIndexKeysArrayBased,
458
- useSelectEntitiesByIndexKeysSetBased,
459
- useSelectEntitiesByPks,
460
- useSelectEntityByPk,
461
- useSelectEntityByPkSignal,
462
- useSelectPksByIndexKeyArrayBased,
463
- useSelectPksByIndexKeyArrayBasedSignal,
464
- useSelectPksByIndexKeySetBased,
465
- useSelectPksByIndexKeySetBasedSignal,
466
- useSelectPksByIndexKeysArrayBased,
467
- useSelectPksByIndexKeysSetBased,
468
- useSelectValueByObjectKey,
469
- 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
470
351
  };