@oimdb/react 2.2.0 → 3.1.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,355 @@
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
- };
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 });
9
+ }
111
10
 
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;
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
+ );
19
+ }
20
+ return db;
118
21
  }
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
22
 
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;
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;
33
+ }
34
+ subscribeEffect(channelMap, onStoreChange, sourceSlots, sourceComputeds) {
35
+ this.version++;
36
+ return OIMEffectService.INSTANCE.subscribeEffect(channelMap, () => {
37
+ this.version++;
38
+ onStoreChange();
39
+ }, sourceSlots, sourceComputeds);
167
40
  }
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
41
  };
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;
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
+ onStoreChange,
60
+ binding.sourceSlots ?? OIM_FROZEN_EMPTY_SLOTS,
61
+ sourceComputeds
62
+ )
209
63
  };
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;
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
+ if (slot.storeSlot.channelSlot === channelMap.defaultChannelSlot) return;
87
+ throw new Error(
88
+ `${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.`
89
+ );
90
+ }
91
+
92
+ // src/oimSubscribeHeldSlot.ts
93
+ function oimSubscribeHeldSlot(channelMap, hookName, snapshotVersion, holdingSlot, sourcesOfSlot, onStoreChange) {
94
+ try {
95
+ oimAssertOneChannel(channelMap, holdingSlot, hookName);
96
+ const [sourceSlots, sourceComputeds] = sourcesOfSlot(holdingSlot);
97
+ return snapshotVersion.subscribeEffect(channelMap, onStoreChange, sourceSlots, sourceComputeds);
98
+ } finally {
99
+ holdingSlot.owner.unholdSlot(holdingSlot);
219
100
  }
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;
101
+ }
102
+
103
+ // src/useOIMSlotByKey.ts
104
+ var sourcesOfRootSlot = (rootSlot) => [[rootSlot], OIM_FROZEN_EMPTY_SLOTS2];
105
+ function useOIMSlotByKey(hookName, slotCollectionService, storeSlot, key, sourcesOfSlot = sourcesOfRootSlot) {
106
+ const db = useOIMDB();
107
+ const lastSeenKeyRef = useRef(key);
108
+ const settledKeyRef = useRef(key);
109
+ if (key !== lastSeenKeyRef.current) {
110
+ lastSeenKeyRef.current = key;
111
+ if (slotCollectionService.getSlotByKey(storeSlot, key) !== slotCollectionService.getSlotByKey(storeSlot, settledKeyRef.current)) {
112
+ settledKeyRef.current = key;
113
+ }
251
114
  }
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) {
115
+ const settledKey = settledKeyRef.current;
116
+ const store = useMemo3(() => {
117
+ const snapshotVersion = new OIMSnapshotVersion();
118
+ let subscribedSlot = void 0;
119
+ return {
120
+ snapshotVersion,
121
+ subscribe: (onStoreChange) => {
122
+ const holdingSlot = slotCollectionService.holdSlotByKey(storeSlot, settledKey);
123
+ const effectDisposer = oimSubscribeHeldSlot(db, hookName, snapshotVersion, holdingSlot, sourcesOfSlot, onStoreChange);
124
+ subscribedSlot = holdingSlot;
258
125
  return () => {
126
+ subscribedSlot = void 0;
127
+ effectDisposer();
259
128
  };
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
- };
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);
129
+ },
130
+ getSlot: () => subscribedSlot ?? slotCollectionService.getSlotByKey(storeSlot, settledKey)
290
131
  };
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]
299
- );
300
- const subscribe = useMemo2(() => {
301
- return (onStoreChange) => {
302
- return reactiveObject.subscribeOnKey(key, onStoreChange);
132
+ }, [hookName, db, slotCollectionService, storeSlot, settledKey, sourcesOfSlot]);
133
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
134
+ return store.getSlot();
135
+ }
136
+
137
+ // src/useOIMEntity.ts
138
+ function useOIMEntity(entityCollectionService, entityStoreSlot, key) {
139
+ return useOIMSlotByKey("useOIMEntity", entityCollectionService, entityStoreSlot, key).entity;
140
+ }
141
+
142
+ // src/useOIMSingleSlot.ts
143
+ import { useMemo as useMemo4 } from "react";
144
+ import { OIM_FROZEN_EMPTY_SLOTS as OIM_FROZEN_EMPTY_SLOTS3 } from "@oimdb/core";
145
+ var sourcesOfRootSlot2 = (rootSlot) => [[rootSlot], OIM_FROZEN_EMPTY_SLOTS3];
146
+ function useOIMSingleSlot(hookName, singleSlotService, singleSlotStoreSlot, sourcesOfSlot = sourcesOfRootSlot2) {
147
+ const db = useOIMDB();
148
+ const store = useMemo4(() => {
149
+ const snapshotVersion = new OIMSnapshotVersion();
150
+ let subscribedSlot = void 0;
151
+ return {
152
+ snapshotVersion,
153
+ subscribe: (onStoreChange) => {
154
+ const holdingSlot = singleSlotService.holdSingleSlot(singleSlotStoreSlot);
155
+ const effectDisposer = oimSubscribeHeldSlot(db, hookName, snapshotVersion, holdingSlot, sourcesOfSlot, onStoreChange);
156
+ subscribedSlot = holdingSlot;
157
+ return () => {
158
+ subscribedSlot = void 0;
159
+ effectDisposer();
160
+ };
161
+ },
162
+ getSlot: () => subscribedSlot ?? singleSlotService.getSingleSlot(singleSlotStoreSlot)
303
163
  };
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;
164
+ }, [hookName, db, singleSlotService, singleSlotStoreSlot, sourcesOfSlot]);
165
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
166
+ return store.getSlot();
167
+ }
168
+
169
+ // src/useOIMSingleEntity.ts
170
+ function useOIMSingleEntity(singleSlotService, singleSlotStoreSlot) {
171
+ return useOIMSingleSlot("useOIMSingleEntity", singleSlotService, singleSlotStoreSlot).entity;
172
+ }
173
+
174
+ // src/useOIMSingleBucketSlots.ts
175
+ function useOIMSingleBucketSlots(singleSlotService, singleSlotStoreSlot) {
176
+ const bucketSlot = useOIMSingleSlot("useOIMSingleBucketSlots", singleSlotService, singleSlotStoreSlot);
177
+ return Array.from(singleSlotService.slotContentService.getBucketMemberSlots(bucketSlot));
178
+ }
179
+
180
+ // src/useOIMField.ts
181
+ import { useMemo as useMemo5 } from "react";
182
+
183
+ // src/holdWhileSubscribed.ts
184
+ import { oimHoldSlots, oimUnholdSlotsIfAlive } from "@oimdb/core";
185
+ function holdWhileSubscribed(slots, subscribe) {
186
+ oimHoldSlots(slots);
187
+ const giveEveryHoldBack = () => {
188
+ oimUnholdSlotsIfAlive(slots);
189
+ };
190
+ let disposer;
191
+ try {
192
+ disposer = subscribe();
193
+ } catch (failure) {
194
+ giveEveryHoldBack();
195
+ throw failure;
313
196
  }
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;
197
+ return () => {
198
+ try {
199
+ disposer();
200
+ } finally {
201
+ giveEveryHoldBack();
202
+ }
203
+ };
204
+ }
205
+
206
+ // src/useOIMField.ts
207
+ function useOIMField(entityCollectionService, slot, name) {
208
+ const db = useOIMDB();
209
+ const store = useMemo5(() => {
210
+ oimAssertOneChannel(db, slot, "useOIMField");
211
+ const holds = [slot];
212
+ const snapshotVersion = new OIMSnapshotVersion();
213
+ return {
214
+ snapshotVersion,
215
+ subscribe: (onStoreChange) => holdWhileSubscribed(holds, () => snapshotVersion.subscribeEffect(
216
+ db,
217
+ onStoreChange,
218
+ [entityCollectionService.columnSlotService.getColumn(slot, name)]
219
+ ))
337
220
  };
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;
221
+ }, [db, entityCollectionService, slot, name]);
222
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
223
+ return slot.entity?.[name];
224
+ }
225
+
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
+ );
240
+ }
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));
349
246
  }
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;
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;
355
270
  }
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(() => {
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
+ }
379
285
  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;
429
- }
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();
459
- }
460
- );
461
- return () => {
462
- unsubscribeFromIndex();
463
- unsubscribeFromCollection();
464
- };
465
- };
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();
538
- }
539
- );
540
- return () => {
541
- unsubscribeFromIndex();
542
- unsubscribeFromCollection();
543
- };
286
+ for (let i = 0; i < heldSlots.length; i++) {
287
+ const heldSlot = heldSlots[i];
288
+ heldSlot.owner.unholdSlot(heldSlot);
289
+ }
544
290
  };
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;
554
- }
555
- return equal ? prevRef.current : key;
291
+ }, [slotCollectionService, storeSlot, keysToHold]);
292
+ return slots;
556
293
  }
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);
669
- };
670
294
 
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({});
295
+ // src/useOIMComputedValueByKey.ts
296
+ import { OIM_FROZEN_EMPTY_SLOTS as OIM_FROZEN_EMPTY_SLOTS4 } from "@oimdb/core";
297
+ var sourcesOfComputedSlot = (computedSlot) => [OIM_FROZEN_EMPTY_SLOTS4, [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);
679
307
  }
680
- function OIMCollectionsProvider({ collections, children, context }) {
681
- const ContextToUse = context || OIMCollectionsContext;
682
- return /* @__PURE__ */ jsx(ContextToUse.Provider, { value: collections, children });
308
+
309
+ // src/useOIMSingleComputedValue.ts
310
+ import { useMemo as useMemo7 } from "react";
311
+ import { OIM_FROZEN_EMPTY_SLOTS as OIM_FROZEN_EMPTY_SLOTS5 } from "@oimdb/core";
312
+ var sourcesOfComputedSlot2 = (computedSlot) => [OIM_FROZEN_EMPTY_SLOTS5, [computedSlot]];
313
+ function useOIMSingleComputedValue(computedService, derive, createSources, dependencyList) {
314
+ const db = useOIMDB();
315
+ const computedStoreSlot = useMemo7(
316
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
317
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
318
+ [db, computedService, ...dependencyList]
319
+ );
320
+ const computedSlot = useOIMSingleSlot(
321
+ "useOIMSingleComputedValue",
322
+ computedService.computedSingleSlotService,
323
+ computedStoreSlot,
324
+ sourcesOfComputedSlot2
325
+ );
326
+ return computedSlot.computedValueService.getValueForSlot(computedSlot);
683
327
  }
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
- );
691
- }
692
- return collections;
328
+
329
+ // src/useOIMComputedCollection.ts
330
+ import { useMemo as useMemo8 } from "react";
331
+ function useOIMComputedCollection(computedService, derive, createSources, dependencyList) {
332
+ const db = useOIMDB();
333
+ return useMemo8(
334
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
335
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
336
+ [db, computedService, ...dependencyList]
337
+ );
693
338
  }
694
339
  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
340
+ OIMDBContext,
341
+ OIMDBProvider,
342
+ useOIMBucketSlots,
343
+ useOIMBucketSlotsByKey,
344
+ useOIMComputedCollection,
345
+ useOIMComputedValue,
346
+ useOIMComputedValueByKey,
347
+ useOIMDB,
348
+ useOIMEntity,
349
+ useOIMField,
350
+ useOIMSingleBucketSlots,
351
+ useOIMSingleComputedValue,
352
+ useOIMSingleEntity,
353
+ useOIMSlotsByKeys,
354
+ useOIMValue
724
355
  };