@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/README.md +267 -127
- package/dist/index.cjs +343 -466
- package/dist/index.d.cts +56 -34
- package/dist/index.d.ts +56 -34
- package/dist/index.js +328 -447
- package/llms.txt +4156 -0
- package/package.json +46 -36
package/dist/index.js
CHANGED
|
@@ -1,470 +1,351 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
// src/OIMDBContext.ts
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
var OIMDBContext = createContext(void 0);
|
|
4
4
|
|
|
5
|
-
// src/
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
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/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
}, [
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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
|
-
|
|
339
|
-
unsubscribeFromIndex();
|
|
340
|
-
unsubscribeFromCollection();
|
|
341
|
-
};
|
|
125
|
+
},
|
|
126
|
+
getSlot: () => subscribedSlot ?? slotCollectionService.getSlotByKey(storeSlot, settledKey)
|
|
342
127
|
};
|
|
343
|
-
}, [
|
|
344
|
-
|
|
345
|
-
return
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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
|
-
}, [
|
|
379
|
-
|
|
380
|
-
return
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
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
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
-
}, [
|
|
423
|
-
|
|
424
|
-
return
|
|
425
|
-
}
|
|
221
|
+
}, [db, entityCollectionService, slot, name]);
|
|
222
|
+
useOIMSnapshotVersion(store.snapshotVersion, store.subscribe);
|
|
223
|
+
return slot.entity?.[name];
|
|
224
|
+
}
|
|
426
225
|
|
|
427
|
-
// src/
|
|
428
|
-
import {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
{
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
|
|
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
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
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
|
-
|
|
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
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
};
|