@oimdb/react 1.2.1 → 1.3.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
@@ -33,7 +33,9 @@ __export(index_exports, {
33
33
  useSelectPksByIndexKeyArrayBased: () => useSelectPksByIndexKeyArrayBased,
34
34
  useSelectPksByIndexKeySetBased: () => useSelectPksByIndexKeySetBased,
35
35
  useSelectPksByIndexKeysArrayBased: () => useSelectPksByIndexKeysArrayBased,
36
- useSelectPksByIndexKeysSetBased: () => useSelectPksByIndexKeysSetBased
36
+ useSelectPksByIndexKeysSetBased: () => useSelectPksByIndexKeysSetBased,
37
+ useSelectValueByObjectKey: () => useSelectValueByObjectKey,
38
+ useSelectValuesByObjectKeys: () => useSelectValuesByObjectKeys
37
39
  });
38
40
  module.exports = __toCommonJS(index_exports);
39
41
 
@@ -47,31 +49,24 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
47
49
  if (!keysAreEqual) {
48
50
  prevKeysRef.current = keys;
49
51
  }
52
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
50
53
  const snapshotValueRef = (0, import_react.useRef)();
51
54
  const subscribe = (0, import_react.useMemo)(() => {
52
- snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
55
+ snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
53
56
  return (onStoreChange) => {
54
- const prevKeys = prevKeysRef.current;
55
- if (!prevKeys) {
56
- return () => {
57
- };
58
- }
59
57
  const updateSnapshot = () => {
60
- snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
58
+ snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
61
59
  onStoreChange();
62
60
  };
63
- reactiveIndex.updateEventEmitter.subscribeOnKeys(
64
- prevKeys,
61
+ const unsubscribe = reactiveIndex.subscribeOnKeys(
62
+ stableKeys,
65
63
  updateSnapshot
66
64
  );
67
65
  return () => {
68
- reactiveIndex.updateEventEmitter.unsubscribeFromKeys(
69
- prevKeys,
70
- updateSnapshot
71
- );
66
+ unsubscribe();
72
67
  };
73
68
  };
74
- }, [prevKeysRef.current, reactiveIndex.updateEventEmitter]);
69
+ }, [reactiveIndex, stableKeys]);
75
70
  const getSnapshot = (0, import_react.useMemo)(() => {
76
71
  return () => {
77
72
  return snapshotValueRef.current;
@@ -88,15 +83,9 @@ var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
88
83
  }, [key, reactiveIndex]);
89
84
  const subscribe = (0, import_react.useMemo)(() => {
90
85
  return (onStoreChange) => {
91
- reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
92
- return () => {
93
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
94
- key,
95
- onStoreChange
96
- );
97
- };
86
+ return reactiveIndex.subscribeOnKey(key, onStoreChange);
98
87
  };
99
- }, [key, reactiveIndex.updateEventEmitter]);
88
+ }, [key, reactiveIndex]);
100
89
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
101
90
  return snapshot;
102
91
  };
@@ -106,31 +95,24 @@ var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
106
95
  if (!keysAreEqual) {
107
96
  prevKeysRef.current = keys;
108
97
  }
98
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
109
99
  const snapshotValueRef = (0, import_react.useRef)();
110
100
  const subscribe = (0, import_react.useMemo)(() => {
111
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
101
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
112
102
  return (onStoreChange) => {
113
- const prevKeys = prevKeysRef.current;
114
- if (!prevKeys) {
115
- return () => {
116
- };
117
- }
118
103
  const updateSnapshot = () => {
119
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
104
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
120
105
  onStoreChange();
121
106
  };
122
- reactiveIndex.updateEventEmitter.subscribeOnKeys(
123
- prevKeys,
107
+ const unsubscribe = reactiveIndex.subscribeOnKeys(
108
+ stableKeys,
124
109
  updateSnapshot
125
110
  );
126
111
  return () => {
127
- reactiveIndex.updateEventEmitter.unsubscribeFromKeys(
128
- prevKeys,
129
- updateSnapshot
130
- );
112
+ unsubscribe();
131
113
  };
132
114
  };
133
- }, [prevKeysRef.current, reactiveIndex.updateEventEmitter]);
115
+ }, [reactiveIndex, stableKeys]);
134
116
  const getSnapshot = (0, import_react.useMemo)(() => {
135
117
  return () => {
136
118
  return snapshotValueRef.current;
@@ -158,18 +140,15 @@ var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
158
140
  snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
159
141
  onStoreChange();
160
142
  };
161
- reactiveIndex.updateEventEmitter.subscribeOnKey(
143
+ const unsubscribe = reactiveIndex.subscribeOnKey(
162
144
  prevKey,
163
145
  updateSnapshot
164
146
  );
165
147
  return () => {
166
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
167
- prevKey,
168
- updateSnapshot
169
- );
148
+ unsubscribe();
170
149
  };
171
150
  };
172
- }, [key, reactiveIndex.updateEventEmitter]);
151
+ }, [key, reactiveIndex]);
173
152
  const getSnapshot = (0, import_react.useMemo)(() => {
174
153
  return () => {
175
154
  return snapshotValueRef.current;
@@ -185,18 +164,56 @@ var useSelectEntityByPk = (reactiveCollection, pk) => {
185
164
  );
186
165
  const subscribe = (0, import_react.useMemo)(() => {
187
166
  return (onStoreChange) => {
188
- reactiveCollection.updateEventEmitter.subscribeOnKey(
189
- pk,
190
- onStoreChange
167
+ return reactiveCollection.subscribeOnKey(pk, onStoreChange);
168
+ };
169
+ }, [pk, reactiveCollection]);
170
+ const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
171
+ return snapshot;
172
+ };
173
+ var useSelectValueByObjectKey = (reactiveObject, key) => {
174
+ const getSnapshot = (0, import_react.useMemo)(
175
+ () => () => reactiveObject.get(key),
176
+ [key, reactiveObject]
177
+ );
178
+ const subscribe = (0, import_react.useMemo)(() => {
179
+ return (onStoreChange) => {
180
+ return reactiveObject.subscribeOnKey(key, onStoreChange);
181
+ };
182
+ }, [key, reactiveObject]);
183
+ const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
184
+ return snapshot;
185
+ };
186
+ var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
187
+ const prevKeysRef = (0, import_react.useRef)();
188
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
189
+ if (!keysAreEqual) {
190
+ prevKeysRef.current = keys;
191
+ }
192
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
193
+ const snapshotRef = (0, import_react.useRef)();
194
+ const subscribe = (0, import_react.useMemo)(() => {
195
+ snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
196
+ return (onStoreChange) => {
197
+ const updateSnapshot = () => {
198
+ snapshotRef.current = stableKeys.map(
199
+ (key) => reactiveObject.get(key)
200
+ );
201
+ onStoreChange();
202
+ };
203
+ const unsubscribe = reactiveObject.subscribeOnKeys(
204
+ stableKeys,
205
+ updateSnapshot
191
206
  );
192
207
  return () => {
193
- reactiveCollection.updateEventEmitter.unsubscribeFromKey(
194
- pk,
195
- onStoreChange
196
- );
208
+ unsubscribe();
197
209
  };
198
210
  };
199
- }, [pk, reactiveCollection.updateEventEmitter]);
211
+ }, [reactiveObject, stableKeys]);
212
+ const getSnapshot = (0, import_react.useMemo)(() => {
213
+ return () => {
214
+ return snapshotRef.current;
215
+ };
216
+ }, []);
200
217
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
201
218
  return snapshot;
202
219
  };
@@ -214,33 +231,28 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
214
231
  if (!pksAreEqual) {
215
232
  prevPksRef.current = pks;
216
233
  }
234
+ const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
217
235
  const snapshotRef = (0, import_react.useRef)();
218
236
  const subscribe = (0, import_react.useMemo)(() => {
219
- snapshotRef.current = pks.map((pk) => reactiveCollection.getOneByPk(pk));
237
+ snapshotRef.current = stablePks.map(
238
+ (pk) => reactiveCollection.getOneByPk(pk)
239
+ );
220
240
  return (onStoreChange) => {
221
- const prevPks = prevPksRef.current;
222
- if (!prevPks) {
223
- return () => {
224
- };
225
- }
226
241
  const updateSnapshot = () => {
227
- snapshotRef.current = prevPks.map(
242
+ snapshotRef.current = stablePks.map(
228
243
  (pk) => reactiveCollection.getOneByPk(pk)
229
244
  );
230
245
  onStoreChange();
231
246
  };
232
- reactiveCollection.updateEventEmitter.subscribeOnKeys(
233
- prevPks,
247
+ const unsubscribe = reactiveCollection.subscribeOnKeys(
248
+ stablePks,
234
249
  updateSnapshot
235
250
  );
236
251
  return () => {
237
- reactiveCollection.updateEventEmitter.unsubscribeFromKeys(
238
- prevPks,
239
- updateSnapshot
240
- );
252
+ unsubscribe();
241
253
  };
242
254
  };
243
- }, [prevPksRef.current, reactiveCollection.updateEventEmitter]);
255
+ }, [reactiveCollection, stablePks]);
244
256
  const getSnapshot = (0, import_react.useMemo)(() => {
245
257
  return () => {
246
258
  return snapshotRef.current;
@@ -315,5 +327,7 @@ function useOIMCollectionsContext(context) {
315
327
  useSelectPksByIndexKeyArrayBased,
316
328
  useSelectPksByIndexKeySetBased,
317
329
  useSelectPksByIndexKeysArrayBased,
318
- useSelectPksByIndexKeysSetBased
330
+ useSelectPksByIndexKeysSetBased,
331
+ useSelectValueByObjectKey,
332
+ useSelectValuesByObjectKeys
319
333
  });
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMRICollection } from '@oimdb/core';
1
+ import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject, OIMRICollection } from '@oimdb/core';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import * as React from 'react';
4
4
  import { ReactNode } from 'react';
@@ -8,6 +8,8 @@ declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends
8
8
  declare const useSelectPksByIndexKeysArrayBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
9
9
  declare const useSelectPksByIndexKeyArrayBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => TPk[] | undefined;
10
10
  declare const useSelectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
11
+ declare const useSelectValueByObjectKey: <TKey extends string, TValue>(reactiveObject: OIMReactiveObject<TKey, TValue>, key: TKey) => TValue | undefined;
12
+ declare const useSelectValuesByObjectKeys: <TKey extends string, TValue>(reactiveObject: OIMReactiveObject<TKey, TValue>, keys: readonly TKey[]) => readonly (TValue | undefined)[] | undefined;
11
13
  declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly (TEntity | undefined)[] | undefined;
12
14
  declare const useSelectEntitiesByIndexKeySetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
13
15
  declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
@@ -26,4 +28,4 @@ interface OIMRICollectionsProviderProps<T extends CollectionsDictionary = Collec
26
28
  declare function OIMRICollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMRICollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
27
29
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
28
30
 
29
- export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased };
31
+ export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMRICollection } from '@oimdb/core';
1
+ import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject, OIMRICollection } from '@oimdb/core';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
  import * as React from 'react';
4
4
  import { ReactNode } from 'react';
@@ -8,6 +8,8 @@ declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends
8
8
  declare const useSelectPksByIndexKeysArrayBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
9
9
  declare const useSelectPksByIndexKeyArrayBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => TPk[] | undefined;
10
10
  declare const useSelectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
11
+ declare const useSelectValueByObjectKey: <TKey extends string, TValue>(reactiveObject: OIMReactiveObject<TKey, TValue>, key: TKey) => TValue | undefined;
12
+ declare const useSelectValuesByObjectKeys: <TKey extends string, TValue>(reactiveObject: OIMReactiveObject<TKey, TValue>, keys: readonly TKey[]) => readonly (TValue | undefined)[] | undefined;
11
13
  declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly (TEntity | undefined)[] | undefined;
12
14
  declare const useSelectEntitiesByIndexKeySetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
13
15
  declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
@@ -26,4 +28,4 @@ interface OIMRICollectionsProviderProps<T extends CollectionsDictionary = Collec
26
28
  declare function OIMRICollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMRICollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
27
29
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
28
30
 
29
- export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased };
31
+ export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.js CHANGED
@@ -8,31 +8,24 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
8
8
  if (!keysAreEqual) {
9
9
  prevKeysRef.current = keys;
10
10
  }
11
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
11
12
  const snapshotValueRef = useRef();
12
13
  const subscribe = useMemo(() => {
13
- snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
14
+ snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
14
15
  return (onStoreChange) => {
15
- const prevKeys = prevKeysRef.current;
16
- if (!prevKeys) {
17
- return () => {
18
- };
19
- }
20
16
  const updateSnapshot = () => {
21
- snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
17
+ snapshotValueRef.current = stableKeys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
22
18
  onStoreChange();
23
19
  };
24
- reactiveIndex.updateEventEmitter.subscribeOnKeys(
25
- prevKeys,
20
+ const unsubscribe = reactiveIndex.subscribeOnKeys(
21
+ stableKeys,
26
22
  updateSnapshot
27
23
  );
28
24
  return () => {
29
- reactiveIndex.updateEventEmitter.unsubscribeFromKeys(
30
- prevKeys,
31
- updateSnapshot
32
- );
25
+ unsubscribe();
33
26
  };
34
27
  };
35
- }, [prevKeysRef.current, reactiveIndex.updateEventEmitter]);
28
+ }, [reactiveIndex, stableKeys]);
36
29
  const getSnapshot = useMemo(() => {
37
30
  return () => {
38
31
  return snapshotValueRef.current;
@@ -49,15 +42,9 @@ var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
49
42
  }, [key, reactiveIndex]);
50
43
  const subscribe = useMemo(() => {
51
44
  return (onStoreChange) => {
52
- reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
53
- return () => {
54
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
55
- key,
56
- onStoreChange
57
- );
58
- };
45
+ return reactiveIndex.subscribeOnKey(key, onStoreChange);
59
46
  };
60
- }, [key, reactiveIndex.updateEventEmitter]);
47
+ }, [key, reactiveIndex]);
61
48
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
62
49
  return snapshot;
63
50
  };
@@ -67,31 +54,24 @@ var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
67
54
  if (!keysAreEqual) {
68
55
  prevKeysRef.current = keys;
69
56
  }
57
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
70
58
  const snapshotValueRef = useRef();
71
59
  const subscribe = useMemo(() => {
72
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
60
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
73
61
  return (onStoreChange) => {
74
- const prevKeys = prevKeysRef.current;
75
- if (!prevKeys) {
76
- return () => {
77
- };
78
- }
79
62
  const updateSnapshot = () => {
80
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
63
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
81
64
  onStoreChange();
82
65
  };
83
- reactiveIndex.updateEventEmitter.subscribeOnKeys(
84
- prevKeys,
66
+ const unsubscribe = reactiveIndex.subscribeOnKeys(
67
+ stableKeys,
85
68
  updateSnapshot
86
69
  );
87
70
  return () => {
88
- reactiveIndex.updateEventEmitter.unsubscribeFromKeys(
89
- prevKeys,
90
- updateSnapshot
91
- );
71
+ unsubscribe();
92
72
  };
93
73
  };
94
- }, [prevKeysRef.current, reactiveIndex.updateEventEmitter]);
74
+ }, [reactiveIndex, stableKeys]);
95
75
  const getSnapshot = useMemo(() => {
96
76
  return () => {
97
77
  return snapshotValueRef.current;
@@ -119,18 +99,15 @@ var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
119
99
  snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
120
100
  onStoreChange();
121
101
  };
122
- reactiveIndex.updateEventEmitter.subscribeOnKey(
102
+ const unsubscribe = reactiveIndex.subscribeOnKey(
123
103
  prevKey,
124
104
  updateSnapshot
125
105
  );
126
106
  return () => {
127
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
128
- prevKey,
129
- updateSnapshot
130
- );
107
+ unsubscribe();
131
108
  };
132
109
  };
133
- }, [key, reactiveIndex.updateEventEmitter]);
110
+ }, [key, reactiveIndex]);
134
111
  const getSnapshot = useMemo(() => {
135
112
  return () => {
136
113
  return snapshotValueRef.current;
@@ -146,18 +123,56 @@ var useSelectEntityByPk = (reactiveCollection, pk) => {
146
123
  );
147
124
  const subscribe = useMemo(() => {
148
125
  return (onStoreChange) => {
149
- reactiveCollection.updateEventEmitter.subscribeOnKey(
150
- pk,
151
- onStoreChange
126
+ return reactiveCollection.subscribeOnKey(pk, onStoreChange);
127
+ };
128
+ }, [pk, reactiveCollection]);
129
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
130
+ return snapshot;
131
+ };
132
+ var useSelectValueByObjectKey = (reactiveObject, key) => {
133
+ const getSnapshot = useMemo(
134
+ () => () => reactiveObject.get(key),
135
+ [key, reactiveObject]
136
+ );
137
+ const subscribe = useMemo(() => {
138
+ return (onStoreChange) => {
139
+ return reactiveObject.subscribeOnKey(key, onStoreChange);
140
+ };
141
+ }, [key, reactiveObject]);
142
+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
143
+ return snapshot;
144
+ };
145
+ var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
146
+ const prevKeysRef = useRef();
147
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
148
+ if (!keysAreEqual) {
149
+ prevKeysRef.current = keys;
150
+ }
151
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
152
+ const snapshotRef = useRef();
153
+ const subscribe = useMemo(() => {
154
+ snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
155
+ return (onStoreChange) => {
156
+ const updateSnapshot = () => {
157
+ snapshotRef.current = stableKeys.map(
158
+ (key) => reactiveObject.get(key)
159
+ );
160
+ onStoreChange();
161
+ };
162
+ const unsubscribe = reactiveObject.subscribeOnKeys(
163
+ stableKeys,
164
+ updateSnapshot
152
165
  );
153
166
  return () => {
154
- reactiveCollection.updateEventEmitter.unsubscribeFromKey(
155
- pk,
156
- onStoreChange
157
- );
167
+ unsubscribe();
158
168
  };
159
169
  };
160
- }, [pk, reactiveCollection.updateEventEmitter]);
170
+ }, [reactiveObject, stableKeys]);
171
+ const getSnapshot = useMemo(() => {
172
+ return () => {
173
+ return snapshotRef.current;
174
+ };
175
+ }, []);
161
176
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
162
177
  return snapshot;
163
178
  };
@@ -175,33 +190,28 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
175
190
  if (!pksAreEqual) {
176
191
  prevPksRef.current = pks;
177
192
  }
193
+ const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
178
194
  const snapshotRef = useRef();
179
195
  const subscribe = useMemo(() => {
180
- snapshotRef.current = pks.map((pk) => reactiveCollection.getOneByPk(pk));
196
+ snapshotRef.current = stablePks.map(
197
+ (pk) => reactiveCollection.getOneByPk(pk)
198
+ );
181
199
  return (onStoreChange) => {
182
- const prevPks = prevPksRef.current;
183
- if (!prevPks) {
184
- return () => {
185
- };
186
- }
187
200
  const updateSnapshot = () => {
188
- snapshotRef.current = prevPks.map(
201
+ snapshotRef.current = stablePks.map(
189
202
  (pk) => reactiveCollection.getOneByPk(pk)
190
203
  );
191
204
  onStoreChange();
192
205
  };
193
- reactiveCollection.updateEventEmitter.subscribeOnKeys(
194
- prevPks,
206
+ const unsubscribe = reactiveCollection.subscribeOnKeys(
207
+ stablePks,
195
208
  updateSnapshot
196
209
  );
197
210
  return () => {
198
- reactiveCollection.updateEventEmitter.unsubscribeFromKeys(
199
- prevPks,
200
- updateSnapshot
201
- );
211
+ unsubscribe();
202
212
  };
203
213
  };
204
- }, [prevPksRef.current, reactiveCollection.updateEventEmitter]);
214
+ }, [reactiveCollection, stablePks]);
205
215
  const getSnapshot = useMemo(() => {
206
216
  return () => {
207
217
  return snapshotRef.current;
@@ -275,5 +285,7 @@ export {
275
285
  useSelectPksByIndexKeyArrayBased,
276
286
  useSelectPksByIndexKeySetBased,
277
287
  useSelectPksByIndexKeysArrayBased,
278
- useSelectPksByIndexKeysSetBased
288
+ useSelectPksByIndexKeysSetBased,
289
+ useSelectValueByObjectKey,
290
+ useSelectValuesByObjectKeys
279
291
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "React integration for OIMDB - Hooks for selection and subscription with external storage",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -23,7 +23,7 @@
23
23
  }
24
24
  },
25
25
  "scripts": {
26
- "build": "tsup src/index.ts --dts --format esm,cjs",
26
+ "build": "tsup --config tsup.config.ts --tsconfig tsconfig.build.json",
27
27
  "test": "jest",
28
28
  "type-check": "tsc --noEmit",
29
29
  "clean": "rimraf dist",
@@ -33,8 +33,8 @@
33
33
  "dist"
34
34
  ],
35
35
  "peerDependencies": {
36
- "@oimdb/core": "^1.3.1",
37
- "react": "^18.0.0"
36
+ "@oimdb/core": "^1.4.1",
37
+ "react": "^18.0.0 || ^19.0.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@testing-library/react": "^14.3.1",