@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.cjs CHANGED
@@ -20,496 +20,373 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- OIMCollectionsContext: () => OIMCollectionsContext,
24
- OIMCollectionsProvider: () => OIMCollectionsProvider,
25
- createOIMCollectionsContext: () => createOIMCollectionsContext,
26
- useOIMCollectionsContext: () => useOIMCollectionsContext,
27
- useSelectEntitiesByIndexKeyArrayBased: () => useSelectEntitiesByIndexKeyArrayBased,
28
- useSelectEntitiesByIndexKeySetBased: () => useSelectEntitiesByIndexKeySetBased,
29
- useSelectEntitiesByIndexKeysArrayBased: () => useSelectEntitiesByIndexKeysArrayBased,
30
- useSelectEntitiesByIndexKeysSetBased: () => useSelectEntitiesByIndexKeysSetBased,
31
- useSelectEntitiesByPks: () => useSelectEntitiesByPks,
32
- useSelectEntityByPk: () => useSelectEntityByPk,
33
- useSelectEntityByPkSignal: () => useSelectEntityByPkSignal,
34
- useSelectPksByIndexKeyArrayBased: () => useSelectPksByIndexKeyArrayBased,
35
- useSelectPksByIndexKeyArrayBasedSignal: () => useSelectPksByIndexKeyArrayBasedSignal,
36
- useSelectPksByIndexKeySetBased: () => useSelectPksByIndexKeySetBased,
37
- useSelectPksByIndexKeySetBasedSignal: () => useSelectPksByIndexKeySetBasedSignal,
38
- useSelectPksByIndexKeysArrayBased: () => useSelectPksByIndexKeysArrayBased,
39
- useSelectPksByIndexKeysSetBased: () => useSelectPksByIndexKeysSetBased,
40
- useSelectValueByObjectKey: () => useSelectValueByObjectKey,
41
- useSelectValuesByObjectKeys: () => useSelectValuesByObjectKeys
23
+ OIMDBContext: () => OIMDBContext,
24
+ OIMDBProvider: () => OIMDBProvider,
25
+ useOIMBucketSlots: () => useOIMBucketSlots,
26
+ useOIMBucketSlotsByKey: () => useOIMBucketSlotsByKey,
27
+ useOIMComputedCollection: () => useOIMComputedCollection,
28
+ useOIMComputedValue: () => useOIMComputedValue,
29
+ useOIMComputedValueByKey: () => useOIMComputedValueByKey,
30
+ useOIMDB: () => useOIMDB,
31
+ useOIMEntity: () => useOIMEntity,
32
+ useOIMField: () => useOIMField,
33
+ useOIMSingleBucketSlots: () => useOIMSingleBucketSlots,
34
+ useOIMSingleComputedValue: () => useOIMSingleComputedValue,
35
+ useOIMSingleEntity: () => useOIMSingleEntity,
36
+ useOIMSlotsByKeys: () => useOIMSlotsByKeys,
37
+ useOIMValue: () => useOIMValue
42
38
  });
43
39
  module.exports = __toCommonJS(index_exports);
44
40
 
45
- // src/hooks/index.ts
46
- var import_react2 = require("react");
47
- var import_react3 = require("react");
48
-
49
- // src/hooks/signal.ts
41
+ // src/OIMDBContext.ts
50
42
  var import_react = require("react");
51
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? import_react.useLayoutEffect : import_react.useEffect;
52
- function useForceUpdate() {
53
- const [, force] = (0, import_react.useReducer)((c) => c + 1 | 0, 0);
54
- return force;
43
+ var OIMDBContext = (0, import_react.createContext)(void 0);
44
+
45
+ // src/OIMDBProvider.tsx
46
+ var import_jsx_runtime = require("react/jsx-runtime");
47
+ function OIMDBProvider({ db, children }) {
48
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(OIMDBContext.Provider, { value: db, children });
55
49
  }
56
- var useSelectEntityByPkSignal = (reactiveCollection, pk) => {
57
- const force = useForceUpdate();
58
- useIsomorphicLayoutEffect(
59
- () => reactiveCollection.subscribeOnKey(pk, force),
60
- [reactiveCollection, pk]
61
- );
62
- return reactiveCollection.getOneByPk(pk);
63
- };
64
- var useSelectPksByIndexKeyArrayBasedSignal = (reactiveIndex, key) => {
65
- const force = useForceUpdate();
66
- useIsomorphicLayoutEffect(
67
- () => reactiveIndex.subscribeOnKey(key, force),
68
- [reactiveIndex, key]
69
- );
70
- return reactiveIndex.getPksByKey(key);
71
- };
72
- var useSelectPksByIndexKeySetBasedSignal = (reactiveIndex, key) => {
73
- const force = useForceUpdate();
74
- useIsomorphicLayoutEffect(
75
- () => reactiveIndex.subscribeOnKey(key, force),
76
- [reactiveIndex, key]
77
- );
78
- return reactiveIndex.getPksByKey(key);
79
- };
80
50
 
81
- // src/hooks/index.ts
82
- var EMPTY_ARRAY = [];
83
- var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
84
- const prevKeysRef = (0, import_react2.useRef)();
85
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
86
- if (!keysAreEqual) {
87
- prevKeysRef.current = keys;
51
+ // src/useOIMDB.ts
52
+ var import_react2 = require("react");
53
+ function useOIMDB() {
54
+ const db = (0, import_react2.useContext)(OIMDBContext);
55
+ if (db === void 0) {
56
+ throw new Error(
57
+ "useOIMDB: no <OIMDBProvider db={...}> above this component. The hooks that build a computed for the component take the database from it."
58
+ );
88
59
  }
89
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
90
- const snapshotValueRef = (0, import_react2.useRef)();
91
- const subscribe = (0, import_react2.useMemo)(() => {
92
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
93
- return (onStoreChange) => {
94
- const updateSnapshot = () => {
95
- snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
96
- onStoreChange();
97
- };
98
- const unsubscribe = reactiveIndex.subscribeOnKeys(
99
- stableKeys,
100
- updateSnapshot
101
- );
102
- return () => {
103
- unsubscribe();
104
- };
105
- };
106
- }, [reactiveIndex, stableKeys]);
107
- const getSnapshot = (0, import_react2.useMemo)(() => {
108
- return () => {
109
- return snapshotValueRef.current;
110
- };
111
- }, []);
112
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
113
- return snapshot;
114
- };
115
- var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
116
- const snapshotValueRef = (0, import_react2.useRef)();
117
- const subscribe = (0, import_react2.useMemo)(() => {
118
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
119
- return (onStoreChange) => {
120
- const updateSnapshot = () => {
121
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
122
- onStoreChange();
123
- };
124
- return reactiveIndex.subscribeOnKey(key, updateSnapshot);
125
- };
126
- }, [key, reactiveIndex]);
127
- const getSnapshot = (0, import_react2.useMemo)(() => {
128
- return () => {
129
- return snapshotValueRef.current;
130
- };
131
- }, []);
132
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
133
- return snapshot;
134
- };
135
- var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
136
- const prevKeysRef = (0, import_react2.useRef)();
137
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
138
- if (!keysAreEqual) {
139
- prevKeysRef.current = keys;
60
+ return db;
61
+ }
62
+
63
+ // src/useOIMValue.ts
64
+ var import_react4 = require("react");
65
+ var import_core2 = require("@oimdb/core");
66
+
67
+ // src/OIMSnapshotVersion.ts
68
+ var import_core = require("@oimdb/core");
69
+ var OIMSnapshotVersion = class {
70
+ constructor() {
71
+ this.version = 0;
72
+ this.getVersion = () => this.version;
140
73
  }
141
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
142
- const snapshotValueRef = (0, import_react2.useRef)();
143
- const subscribe = (0, import_react2.useMemo)(() => {
144
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
145
- return (onStoreChange) => {
146
- const updateSnapshot = () => {
147
- snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
148
- onStoreChange();
149
- };
150
- const unsubscribe = reactiveIndex.subscribeOnKeys(
151
- stableKeys,
152
- updateSnapshot
153
- );
154
- return () => {
155
- unsubscribe();
156
- };
157
- };
158
- }, [reactiveIndex, stableKeys]);
159
- const getSnapshot = (0, import_react2.useMemo)(() => {
160
- return () => {
161
- return snapshotValueRef.current;
162
- };
163
- }, []);
164
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
165
- return snapshot;
166
- };
167
- var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
168
- const prevKeyRef = (0, import_react2.useRef)();
169
- const keyAreEqual = prevKeyRef.current === key;
170
- if (!keyAreEqual) {
171
- prevKeyRef.current = key;
74
+ subscribeEffect(channelMap, sourceSlots, sourceComputeds, onStoreChange) {
75
+ this.version++;
76
+ return import_core.OIMEffectService.INSTANCE.subscribeEffect(channelMap, sourceSlots, sourceComputeds, () => {
77
+ this.version++;
78
+ onStoreChange();
79
+ });
172
80
  }
173
- const snapshotValueRef = (0, import_react2.useRef)();
174
- const subscribe = (0, import_react2.useMemo)(() => {
175
- snapshotValueRef.current = reactiveIndex.getPksByKey(key);
176
- return (onStoreChange) => {
177
- const prevKey = prevKeyRef.current;
178
- if (!prevKey) {
179
- return () => {
180
- };
181
- }
182
- const updateSnapshot = () => {
183
- snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
184
- onStoreChange();
185
- };
186
- const unsubscribe = reactiveIndex.subscribeOnKey(
187
- prevKey,
188
- updateSnapshot
189
- );
190
- return () => {
191
- unsubscribe();
192
- };
193
- };
194
- }, [key, reactiveIndex]);
195
- const getSnapshot = (0, import_react2.useMemo)(() => {
196
- return () => {
197
- return snapshotValueRef.current;
198
- };
199
- }, []);
200
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
201
- return snapshot;
202
81
  };
203
- var useSelectEntityByPk = (reactiveCollection, pk) => {
204
- const getSnapshot = (0, import_react2.useMemo)(
205
- () => () => reactiveCollection.getOneByPk(pk),
206
- [pk, reactiveCollection]
207
- );
208
- const subscribe = (0, import_react2.useMemo)(() => {
209
- return (onStoreChange) => {
210
- return reactiveCollection.subscribeOnKey(pk, onStoreChange);
82
+
83
+ // src/useOIMSnapshotVersion.ts
84
+ var import_react3 = require("react");
85
+ function useOIMSnapshotVersion(snapshotVersion, subscribe) {
86
+ return (0, import_react3.useSyncExternalStore)(subscribe, snapshotVersion.getVersion, snapshotVersion.getVersion);
87
+ }
88
+
89
+ // src/useOIMValue.ts
90
+ function useOIMValue(binding) {
91
+ const db = useOIMDB();
92
+ const sourceComputeds = binding.sourceComputeds ?? import_core2.OIM_FROZEN_EMPTY_SLOTS;
93
+ const store = (0, import_react4.useMemo)(() => {
94
+ const snapshotVersion = new OIMSnapshotVersion();
95
+ return {
96
+ snapshotVersion,
97
+ subscribe: (onStoreChange) => snapshotVersion.subscribeEffect(
98
+ db,
99
+ binding.sourceSlots ?? import_core2.OIM_FROZEN_EMPTY_SLOTS,
100
+ sourceComputeds,
101
+ onStoreChange
102
+ )
211
103
  };
212
- }, [pk, reactiveCollection]);
213
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
214
- return snapshot;
215
- };
216
- var useSelectValueByObjectKey = (reactiveObject, key) => {
217
- const getSnapshot = (0, import_react2.useMemo)(
218
- () => () => reactiveObject.get(key),
219
- [key, reactiveObject]
104
+ }, [db, binding, sourceComputeds]);
105
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
106
+ (0, import_core2.oimRefreshSourceComputeds)(sourceComputeds);
107
+ return binding.derive();
108
+ }
109
+
110
+ // src/useOIMComputedValue.ts
111
+ var import_react5 = require("react");
112
+ function useOIMComputedValue(computedSlot) {
113
+ const binding = (0, import_react5.useMemo)(() => ({
114
+ sourceComputeds: [computedSlot],
115
+ derive: () => computedSlot.value
116
+ }), [computedSlot]);
117
+ return useOIMValue(binding);
118
+ }
119
+
120
+ // src/useOIMSlotByKey.ts
121
+ var import_react6 = require("react");
122
+ var import_core3 = require("@oimdb/core");
123
+
124
+ // src/oimAssertOneChannel.ts
125
+ function oimAssertOneChannel(channelMap, slot, door) {
126
+ const storeSlot = slot.storeSlot;
127
+ if (storeSlot === void 0 || storeSlot.channelSlot === channelMap.defaultChannelSlot) return;
128
+ throw new Error(
129
+ `${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.`
220
130
  );
221
- const subscribe = (0, import_react2.useMemo)(() => {
222
- return (onStoreChange) => {
223
- return reactiveObject.subscribeOnKey(key, onStoreChange);
224
- };
225
- }, [key, reactiveObject]);
226
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
227
- return snapshot;
228
- };
229
- var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
230
- const prevKeysRef = (0, import_react2.useRef)();
231
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
232
- if (!keysAreEqual) {
233
- prevKeysRef.current = keys;
234
- }
235
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
236
- const snapshotRef = (0, import_react2.useRef)();
237
- const subscribe = (0, import_react2.useMemo)(() => {
238
- snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
239
- return (onStoreChange) => {
240
- const updateSnapshot = () => {
241
- snapshotRef.current = stableKeys.map(
242
- (key) => reactiveObject.get(key)
243
- );
244
- onStoreChange();
245
- };
246
- const unsubscribe = reactiveObject.subscribeOnKeys(
247
- stableKeys,
248
- updateSnapshot
249
- );
250
- return () => {
251
- unsubscribe();
252
- };
253
- };
254
- }, [reactiveObject, stableKeys]);
255
- const getSnapshot = (0, import_react2.useMemo)(() => {
256
- return () => {
257
- return snapshotRef.current;
258
- };
259
- }, []);
260
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
261
- return snapshot;
262
- };
263
- function arraysEqual(a, b) {
264
- if (a === b) return true;
265
- if (a.length !== b.length) return false;
266
- for (let i = 0; i < a.length; i++) {
267
- if (a[i] !== b[i]) return false;
268
- }
269
- return true;
270
131
  }
271
- var useSelectEntitiesByPks = (reactiveCollection, pks) => {
272
- const prevPksRef = (0, import_react2.useRef)();
273
- const pksAreEqual = arraysEqual(prevPksRef.current || EMPTY_ARRAY, pks);
274
- if (!pksAreEqual) {
275
- prevPksRef.current = pks;
276
- }
277
- const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
278
- const snapshotRef = (0, import_react2.useRef)();
279
- const subscribe = (0, import_react2.useMemo)(() => {
280
- snapshotRef.current = stablePks.map(
281
- (pk) => reactiveCollection.getOneByPk(pk)
282
- );
283
- return (onStoreChange) => {
284
- const updateSnapshot = () => {
285
- snapshotRef.current = stablePks.map(
286
- (pk) => reactiveCollection.getOneByPk(pk)
287
- );
288
- onStoreChange();
289
- };
290
- const unsubscribe = reactiveCollection.subscribeOnKeys(
291
- stablePks,
292
- updateSnapshot
293
- );
294
- return () => {
295
- unsubscribe();
296
- };
297
- };
298
- }, [reactiveCollection, stablePks]);
299
- const getSnapshot = (0, import_react2.useMemo)(() => {
300
- return () => {
301
- return snapshotRef.current;
302
- };
303
- }, []);
304
- const snapshot = (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
305
- return snapshot;
306
- };
307
- var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
308
- const snapshotRef = (0, import_react2.useRef)();
309
- const subscribe = (0, import_react2.useMemo)(() => {
310
- const readSnapshot = () => Array.from(
311
- reactiveIndex.getPksByKey(key),
312
- (pk) => reactiveCollection.getOneByPk(pk)
313
- );
314
- const readPks = () => Array.from(reactiveIndex.getPksByKey(key));
315
- snapshotRef.current = readSnapshot();
316
- return (onStoreChange) => {
317
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
318
- snapshotRef.current = readSnapshot();
319
- onStoreChange();
320
- });
321
- const resubscribeCollection = () => {
322
- unsubscribeFromCollection();
323
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
324
- readPks(),
325
- () => {
326
- snapshotRef.current = readSnapshot();
327
- onStoreChange();
328
- }
329
- );
330
- };
331
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
332
- resubscribeCollection();
333
- snapshotRef.current = readSnapshot();
334
- onStoreChange();
335
- });
336
- return () => {
337
- unsubscribeFromIndex();
338
- unsubscribeFromCollection();
339
- };
340
- };
341
- }, [key, reactiveCollection, reactiveIndex]);
342
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
343
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
344
- };
345
- var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
346
- const prevKeysRef = (0, import_react2.useRef)();
347
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
348
- if (!keysAreEqual) {
349
- prevKeysRef.current = keys;
132
+
133
+ // src/useOIMSlotByKey.ts
134
+ var sourcesOfRootSlot = (rootSlot) => [[rootSlot], import_core3.OIM_FROZEN_EMPTY_SLOTS];
135
+ function useOIMSlotByKey(hookName, slotCollectionService, storeSlot, key, sourcesOfSlot = sourcesOfRootSlot) {
136
+ const db = useOIMDB();
137
+ const lastSeenKeyRef = (0, import_react6.useRef)(key);
138
+ const settledKeyRef = (0, import_react6.useRef)(key);
139
+ if (key !== lastSeenKeyRef.current) {
140
+ lastSeenKeyRef.current = key;
141
+ if (slotCollectionService.getSlotByKey(storeSlot, key) !== slotCollectionService.getSlotByKey(storeSlot, settledKeyRef.current)) {
142
+ settledKeyRef.current = key;
143
+ }
350
144
  }
351
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
352
- const snapshotRef = (0, import_react2.useRef)();
353
- const subscribe = (0, import_react2.useMemo)(() => {
354
- const readPks = () => stableKeys.flatMap(
355
- (key) => Array.from(reactiveIndex.getPksByKey(key))
356
- );
357
- const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
358
- snapshotRef.current = readSnapshot();
359
- return (onStoreChange) => {
360
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
361
- snapshotRef.current = readSnapshot();
362
- onStoreChange();
363
- });
364
- const resubscribeCollection = () => {
365
- unsubscribeFromCollection();
366
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
367
- readPks(),
368
- () => {
369
- snapshotRef.current = readSnapshot();
370
- onStoreChange();
371
- }
372
- );
373
- };
374
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
375
- stableKeys,
376
- () => {
377
- resubscribeCollection();
378
- snapshotRef.current = readSnapshot();
379
- onStoreChange();
145
+ const settledKey = settledKeyRef.current;
146
+ const store = (0, import_react6.useMemo)(() => {
147
+ const snapshotVersion = new OIMSnapshotVersion();
148
+ let subscribedSlot = void 0;
149
+ return {
150
+ snapshotVersion,
151
+ subscribe: (onStoreChange) => {
152
+ const holdingSlot = slotCollectionService.holdSlotByKey(storeSlot, settledKey);
153
+ try {
154
+ oimAssertOneChannel(db, holdingSlot, hookName);
155
+ const [sourceSlots, sourceComputeds] = sourcesOfSlot(holdingSlot);
156
+ const disposeEffect = snapshotVersion.subscribeEffect(db, sourceSlots, sourceComputeds, onStoreChange);
157
+ subscribedSlot = holdingSlot;
158
+ return () => {
159
+ subscribedSlot = void 0;
160
+ disposeEffect();
161
+ };
162
+ } finally {
163
+ holdingSlot.owner.unholdSlot(holdingSlot);
380
164
  }
381
- );
382
- return () => {
383
- unsubscribeFromIndex();
384
- unsubscribeFromCollection();
385
- };
165
+ },
166
+ getSlot: () => subscribedSlot ?? slotCollectionService.getSlotByKey(storeSlot, settledKey)
386
167
  };
387
- }, [reactiveCollection, reactiveIndex, stableKeys]);
388
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
389
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
390
- };
391
- var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
392
- const snapshotRef = (0, import_react2.useRef)();
393
- const subscribe = (0, import_react2.useMemo)(() => {
394
- const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
395
- const readPks = () => reactiveIndex.getPksByKey(key);
396
- snapshotRef.current = readSnapshot();
397
- return (onStoreChange) => {
398
- let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
399
- snapshotRef.current = readSnapshot();
400
- onStoreChange();
401
- });
402
- const resubscribeCollection = () => {
403
- unsubscribeFromCollection();
404
- unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
405
- readPks(),
406
- () => {
407
- snapshotRef.current = readSnapshot();
408
- onStoreChange();
409
- }
410
- );
411
- };
412
- const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
413
- resubscribeCollection();
414
- snapshotRef.current = readSnapshot();
415
- onStoreChange();
416
- });
417
- return () => {
418
- unsubscribeFromIndex();
419
- unsubscribeFromCollection();
420
- };
168
+ }, [hookName, db, slotCollectionService, storeSlot, settledKey, sourcesOfSlot]);
169
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
170
+ return store.getSlot();
171
+ }
172
+
173
+ // src/useOIMEntity.ts
174
+ function useOIMEntity(entityCollectionService, entityStoreSlot, key) {
175
+ return useOIMSlotByKey("useOIMEntity", entityCollectionService, entityStoreSlot, key).entity;
176
+ }
177
+
178
+ // src/useOIMSingleSlot.ts
179
+ var import_react7 = require("react");
180
+ function useOIMSingleSlot(hookName, singleSlotService, singleSlotStoreSlot) {
181
+ const db = useOIMDB();
182
+ const store = (0, import_react7.useMemo)(() => {
183
+ const snapshotVersion = new OIMSnapshotVersion();
184
+ let subscribedSlot = void 0;
185
+ return {
186
+ snapshotVersion,
187
+ subscribe: (onStoreChange) => {
188
+ const holdingSlot = singleSlotService.holdSingleSlot(singleSlotStoreSlot);
189
+ try {
190
+ oimAssertOneChannel(db, holdingSlot, hookName);
191
+ const disposeEffect = snapshotVersion.subscribeEffect(db, [holdingSlot], [], onStoreChange);
192
+ subscribedSlot = holdingSlot;
193
+ return () => {
194
+ subscribedSlot = void 0;
195
+ disposeEffect();
196
+ };
197
+ } finally {
198
+ holdingSlot.owner.unholdSlot(holdingSlot);
199
+ }
200
+ },
201
+ getSlot: () => subscribedSlot ?? singleSlotService.getSingleSlot(singleSlotStoreSlot)
421
202
  };
422
- }, [key, reactiveCollection, reactiveIndex]);
423
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
424
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
425
- };
426
- var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
427
- const prevKeysRef = (0, import_react2.useRef)();
428
- const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
429
- if (!keysAreEqual) {
430
- prevKeysRef.current = keys;
203
+ }, [hookName, db, singleSlotService, singleSlotStoreSlot]);
204
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
205
+ return store.getSlot();
206
+ }
207
+
208
+ // src/useOIMSingleEntity.ts
209
+ function useOIMSingleEntity(singleSlotService, singleSlotStoreSlot) {
210
+ return useOIMSingleSlot("useOIMSingleEntity", singleSlotService, singleSlotStoreSlot).entity;
211
+ }
212
+
213
+ // src/useOIMSingleBucketSlots.ts
214
+ function useOIMSingleBucketSlots(singleSlotService, singleSlotStoreSlot) {
215
+ const bucketSlot = useOIMSingleSlot("useOIMSingleBucketSlots", singleSlotService, singleSlotStoreSlot);
216
+ return Array.from(singleSlotService.slotContentService.getBucketMemberSlots(bucketSlot));
217
+ }
218
+
219
+ // src/useOIMField.ts
220
+ var import_react8 = require("react");
221
+
222
+ // src/holdWhileSubscribed.ts
223
+ var import_core4 = require("@oimdb/core");
224
+ function holdWhileSubscribed(slots, subscribe) {
225
+ (0, import_core4.oimHoldSlots)(slots);
226
+ const giveEveryHoldBack = () => {
227
+ (0, import_core4.oimUnholdSlotsIfAlive)(slots);
228
+ };
229
+ let disposer;
230
+ try {
231
+ disposer = subscribe();
232
+ } catch (failure) {
233
+ giveEveryHoldBack();
234
+ throw failure;
431
235
  }
432
- const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
433
- const snapshotRef = (0, import_react2.useRef)();
434
- const subscribe = (0, import_react2.useMemo)(() => {
435
- const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
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
- };
236
+ return () => {
237
+ try {
238
+ disposer();
239
+ } finally {
240
+ giveEveryHoldBack();
241
+ }
242
+ };
243
+ }
244
+
245
+ // src/useOIMField.ts
246
+ function useOIMField(entityCollectionService, slot, name) {
247
+ const db = useOIMDB();
248
+ const store = (0, import_react8.useMemo)(() => {
249
+ oimAssertOneChannel(db, slot, "useOIMField");
250
+ const holds = [slot];
251
+ const snapshotVersion = new OIMSnapshotVersion();
252
+ return {
253
+ snapshotVersion,
254
+ subscribe: (onStoreChange) => holdWhileSubscribed(holds, () => snapshotVersion.subscribeEffect(
255
+ db,
256
+ [entityCollectionService.getColumn(slot, name)],
257
+ [],
258
+ onStoreChange
259
+ ))
465
260
  };
466
- }, [reactiveCollection, reactiveIndex, stableKeys]);
467
- const getSnapshot = (0, import_react2.useMemo)(() => () => snapshotRef.current, []);
468
- return (0, import_react3.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
469
- };
261
+ }, [db, entityCollectionService, slot, name]);
262
+ useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
263
+ return slot.entity?.[name];
264
+ }
470
265
 
471
- // src/context/index.tsx
472
- var import_react4 = require("react");
473
- var import_jsx_runtime = require("react/jsx-runtime");
474
- var OIMCollectionsContext = (0, import_react4.createContext)(
475
- {}
476
- );
477
- function createOIMCollectionsContext() {
478
- return (0, import_react4.createContext)({});
266
+ // src/useOIMBucketSlots.ts
267
+ var import_react9 = require("react");
268
+ function useOIMBucketSlots(bucketCollectionService, bucketSlot) {
269
+ const db = useOIMDB();
270
+ const binding = (0, import_react9.useMemo)(() => {
271
+ oimAssertOneChannel(db, bucketSlot, "useOIMBucketSlots");
272
+ return { sourceSlots: [bucketSlot], derive: () => bucketSlot };
273
+ }, [db, bucketSlot]);
274
+ const { changedAtRevision, changedAtEpoch } = useOIMValue(binding);
275
+ return (0, import_react9.useMemo)(
276
+ () => Array.from(bucketCollectionService.slotContentService.getBucketMemberSlots(bucketSlot)),
277
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the bucket slot's change stamp moves exactly when its members do
278
+ [bucketCollectionService, bucketSlot, changedAtRevision, changedAtEpoch]
279
+ );
479
280
  }
480
- function OIMCollectionsProvider({ collections, children, context }) {
481
- const ContextToUse = context || OIMCollectionsContext;
482
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ContextToUse.Provider, { value: collections, children });
281
+
282
+ // src/useOIMBucketSlotsByKey.ts
283
+ function useOIMBucketSlotsByKey(bucketCollectionService, bucketStoreSlot, key) {
284
+ const bucketSlot = useOIMSlotByKey("useOIMBucketSlotsByKey", bucketCollectionService, bucketStoreSlot, key);
285
+ return Array.from(bucketCollectionService.slotContentService.getBucketMemberSlots(bucketSlot));
483
286
  }
484
- function useOIMCollectionsContext(context) {
485
- const contextToUse = context || OIMCollectionsContext;
486
- const collections = (0, import_react4.useContext)(contextToUse);
487
- if (!collections || Object.keys(collections).length === 0) {
488
- throw new Error(
489
- "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
490
- );
287
+
288
+ // src/useOIMSlotsByKeys.ts
289
+ var import_react10 = require("react");
290
+ var import_core5 = require("@oimdb/core");
291
+ function useOIMSlotsByKeys(slotCollectionService, storeSlot, keys) {
292
+ const settledKeys = (0, import_react10.useRef)(keys);
293
+ const previousKeys = settledKeys.current;
294
+ if (!(0, import_core5.oimSameKeySequence)(previousKeys, keys)) settledKeys.current = keys;
295
+ const keysToHold = settledKeys.current;
296
+ const [, resolveAgain] = (0, import_react10.useState)(0);
297
+ const resolvedSlots = (0, import_react10.useRef)(void 0);
298
+ const resolvedKeys = (0, import_react10.useRef)(keysToHold);
299
+ const resolvedSlotCollectionService = (0, import_react10.useRef)(slotCollectionService);
300
+ const resolvedStoreSlot = (0, import_react10.useRef)(storeSlot);
301
+ if (resolvedSlots.current === void 0 || resolvedKeys.current !== keysToHold || resolvedSlotCollectionService.current !== slotCollectionService || resolvedStoreSlot.current !== storeSlot) {
302
+ const slots2 = [];
303
+ for (let i = 0; i < keysToHold.length; i++) {
304
+ slots2.push(slotCollectionService.getSlotByKey(storeSlot, keysToHold[i]));
305
+ }
306
+ resolvedSlots.current = slots2;
307
+ resolvedKeys.current = keysToHold;
308
+ resolvedSlotCollectionService.current = slotCollectionService;
309
+ resolvedStoreSlot.current = storeSlot;
491
310
  }
492
- return collections;
311
+ const slots = resolvedSlots.current;
312
+ (0, import_react10.useEffect)(() => {
313
+ const renderedSlots = resolvedSlots.current;
314
+ const heldSlots = [];
315
+ let someKeyNamesAnotherIdentityNow = false;
316
+ for (let i = 0; i < keysToHold.length; i++) {
317
+ const slot = slotCollectionService.holdSlotByKey(storeSlot, keysToHold[i]);
318
+ heldSlots.push(slot);
319
+ if (slot !== renderedSlots?.[i]) someKeyNamesAnotherIdentityNow = true;
320
+ }
321
+ if (someKeyNamesAnotherIdentityNow) {
322
+ resolvedSlots.current = void 0;
323
+ resolveAgain((version) => version + 1);
324
+ }
325
+ return () => {
326
+ for (let i = 0; i < heldSlots.length; i++) {
327
+ const heldSlot = heldSlots[i];
328
+ heldSlot.owner.unholdSlot(heldSlot);
329
+ }
330
+ };
331
+ }, [slotCollectionService, storeSlot, keysToHold]);
332
+ return slots;
333
+ }
334
+
335
+ // src/useOIMComputedValueByKey.ts
336
+ var import_core6 = require("@oimdb/core");
337
+ var sourcesOfComputedSlot = (computedSlot) => [import_core6.OIM_FROZEN_EMPTY_SLOTS, [computedSlot]];
338
+ function useOIMComputedValueByKey(computedCollectionService, computedStoreSlot, key) {
339
+ const computedSlot = useOIMSlotByKey(
340
+ "useOIMComputedValueByKey",
341
+ computedCollectionService,
342
+ computedStoreSlot,
343
+ key,
344
+ sourcesOfComputedSlot
345
+ );
346
+ return computedSlot.computedValueService.getValueForSlot(computedSlot);
347
+ }
348
+
349
+ // src/useOIMSingleComputedValue.ts
350
+ var import_react11 = require("react");
351
+ function useOIMSingleComputedValue(computedService, derive, createSources, dependencyList) {
352
+ const db = useOIMDB();
353
+ const computedStoreSlot = (0, import_react11.useMemo)(
354
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
355
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
356
+ [db, computedService, ...dependencyList]
357
+ );
358
+ return useOIMComputedValueByKey(
359
+ computedService.computedCollectionService,
360
+ computedStoreSlot,
361
+ void 0
362
+ );
363
+ }
364
+
365
+ // src/useOIMComputedCollection.ts
366
+ var import_react12 = require("react");
367
+ function useOIMComputedCollection(computedService, derive, createSources, dependencyList) {
368
+ const db = useOIMDB();
369
+ return (0, import_react12.useMemo)(
370
+ () => computedService.createComputedStoreSlot(db, derive, createSources),
371
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller's list is checked at the call site
372
+ [db, computedService, ...dependencyList]
373
+ );
493
374
  }
494
375
  // Annotate the CommonJS export names for ESM import in node:
495
376
  0 && (module.exports = {
496
- OIMCollectionsContext,
497
- OIMCollectionsProvider,
498
- createOIMCollectionsContext,
499
- useOIMCollectionsContext,
500
- useSelectEntitiesByIndexKeyArrayBased,
501
- useSelectEntitiesByIndexKeySetBased,
502
- useSelectEntitiesByIndexKeysArrayBased,
503
- useSelectEntitiesByIndexKeysSetBased,
504
- useSelectEntitiesByPks,
505
- useSelectEntityByPk,
506
- useSelectEntityByPkSignal,
507
- useSelectPksByIndexKeyArrayBased,
508
- useSelectPksByIndexKeyArrayBasedSignal,
509
- useSelectPksByIndexKeySetBased,
510
- useSelectPksByIndexKeySetBasedSignal,
511
- useSelectPksByIndexKeysArrayBased,
512
- useSelectPksByIndexKeysSetBased,
513
- useSelectValueByObjectKey,
514
- useSelectValuesByObjectKeys
377
+ OIMDBContext,
378
+ OIMDBProvider,
379
+ useOIMBucketSlots,
380
+ useOIMBucketSlotsByKey,
381
+ useOIMComputedCollection,
382
+ useOIMComputedValue,
383
+ useOIMComputedValueByKey,
384
+ useOIMDB,
385
+ useOIMEntity,
386
+ useOIMField,
387
+ useOIMSingleBucketSlots,
388
+ useOIMSingleComputedValue,
389
+ useOIMSingleEntity,
390
+ useOIMSlotsByKeys,
391
+ useOIMValue
515
392
  });