@oimdb/react 1.2.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -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;
@@ -42,22 +35,22 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
42
35
  return snapshot;
43
36
  };
44
37
  var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
45
- const getSnapshot = useMemo(() => {
46
- return () => {
47
- return reactiveIndex.getPksByKey(key);
48
- };
49
- }, [key, reactiveIndex]);
38
+ const snapshotValueRef = useRef();
50
39
  const subscribe = useMemo(() => {
40
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
51
41
  return (onStoreChange) => {
52
- reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
53
- return () => {
54
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
55
- key,
56
- onStoreChange
57
- );
42
+ const updateSnapshot = () => {
43
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
44
+ onStoreChange();
58
45
  };
46
+ return reactiveIndex.subscribeOnKey(key, updateSnapshot);
47
+ };
48
+ }, [key, reactiveIndex]);
49
+ const getSnapshot = useMemo(() => {
50
+ return () => {
51
+ return snapshotValueRef.current;
59
52
  };
60
- }, [key, reactiveIndex.updateEventEmitter]);
53
+ }, []);
61
54
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
62
55
  return snapshot;
63
56
  };
@@ -67,31 +60,24 @@ var useSelectPksByIndexKeysArrayBased = (reactiveIndex, keys) => {
67
60
  if (!keysAreEqual) {
68
61
  prevKeysRef.current = keys;
69
62
  }
63
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
70
64
  const snapshotValueRef = useRef();
71
65
  const subscribe = useMemo(() => {
72
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
66
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
73
67
  return (onStoreChange) => {
74
- const prevKeys = prevKeysRef.current;
75
- if (!prevKeys) {
76
- return () => {
77
- };
78
- }
79
68
  const updateSnapshot = () => {
80
- snapshotValueRef.current = keys.map((key) => reactiveIndex.getPksByKey(key)).flat();
69
+ snapshotValueRef.current = stableKeys.map((key) => reactiveIndex.getPksByKey(key)).flat();
81
70
  onStoreChange();
82
71
  };
83
- reactiveIndex.updateEventEmitter.subscribeOnKeys(
84
- prevKeys,
72
+ const unsubscribe = reactiveIndex.subscribeOnKeys(
73
+ stableKeys,
85
74
  updateSnapshot
86
75
  );
87
76
  return () => {
88
- reactiveIndex.updateEventEmitter.unsubscribeFromKeys(
89
- prevKeys,
90
- updateSnapshot
91
- );
77
+ unsubscribe();
92
78
  };
93
79
  };
94
- }, [prevKeysRef.current, reactiveIndex.updateEventEmitter]);
80
+ }, [reactiveIndex, stableKeys]);
95
81
  const getSnapshot = useMemo(() => {
96
82
  return () => {
97
83
  return snapshotValueRef.current;
@@ -119,18 +105,15 @@ var useSelectPksByIndexKeyArrayBased = (reactiveIndex, key) => {
119
105
  snapshotValueRef.current = reactiveIndex.getPksByKey(prevKey);
120
106
  onStoreChange();
121
107
  };
122
- reactiveIndex.updateEventEmitter.subscribeOnKey(
108
+ const unsubscribe = reactiveIndex.subscribeOnKey(
123
109
  prevKey,
124
110
  updateSnapshot
125
111
  );
126
112
  return () => {
127
- reactiveIndex.updateEventEmitter.unsubscribeFromKey(
128
- prevKey,
129
- updateSnapshot
130
- );
113
+ unsubscribe();
131
114
  };
132
115
  };
133
- }, [key, reactiveIndex.updateEventEmitter]);
116
+ }, [key, reactiveIndex]);
134
117
  const getSnapshot = useMemo(() => {
135
118
  return () => {
136
119
  return snapshotValueRef.current;
@@ -146,18 +129,9 @@ var useSelectEntityByPk = (reactiveCollection, pk) => {
146
129
  );
147
130
  const subscribe = useMemo(() => {
148
131
  return (onStoreChange) => {
149
- reactiveCollection.updateEventEmitter.subscribeOnKey(
150
- pk,
151
- onStoreChange
152
- );
153
- return () => {
154
- reactiveCollection.updateEventEmitter.unsubscribeFromKey(
155
- pk,
156
- onStoreChange
157
- );
158
- };
132
+ return reactiveCollection.subscribeOnKey(pk, onStoreChange);
159
133
  };
160
- }, [pk, reactiveCollection.updateEventEmitter]);
134
+ }, [pk, reactiveCollection]);
161
135
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
162
136
  return snapshot;
163
137
  };
@@ -168,18 +142,9 @@ var useSelectValueByObjectKey = (reactiveObject, key) => {
168
142
  );
169
143
  const subscribe = useMemo(() => {
170
144
  return (onStoreChange) => {
171
- reactiveObject.updateEventEmitter.subscribeOnKey(
172
- key,
173
- onStoreChange
174
- );
175
- return () => {
176
- reactiveObject.updateEventEmitter.unsubscribeFromKey(
177
- key,
178
- onStoreChange
179
- );
180
- };
145
+ return reactiveObject.subscribeOnKey(key, onStoreChange);
181
146
  };
182
- }, [key, reactiveObject.updateEventEmitter]);
147
+ }, [key, reactiveObject]);
183
148
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
184
149
  return snapshot;
185
150
  };
@@ -189,33 +154,26 @@ var useSelectValuesByObjectKeys = (reactiveObject, keys) => {
189
154
  if (!keysAreEqual) {
190
155
  prevKeysRef.current = keys;
191
156
  }
157
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
192
158
  const snapshotRef = useRef();
193
159
  const subscribe = useMemo(() => {
194
- snapshotRef.current = keys.map((key) => reactiveObject.get(key));
160
+ snapshotRef.current = stableKeys.map((key) => reactiveObject.get(key));
195
161
  return (onStoreChange) => {
196
- const prevKeys = prevKeysRef.current;
197
- if (!prevKeys) {
198
- return () => {
199
- };
200
- }
201
162
  const updateSnapshot = () => {
202
- snapshotRef.current = prevKeys.map(
163
+ snapshotRef.current = stableKeys.map(
203
164
  (key) => reactiveObject.get(key)
204
165
  );
205
166
  onStoreChange();
206
167
  };
207
- reactiveObject.updateEventEmitter.subscribeOnKeys(
208
- prevKeys,
168
+ const unsubscribe = reactiveObject.subscribeOnKeys(
169
+ stableKeys,
209
170
  updateSnapshot
210
171
  );
211
172
  return () => {
212
- reactiveObject.updateEventEmitter.unsubscribeFromKeys(
213
- prevKeys,
214
- updateSnapshot
215
- );
173
+ unsubscribe();
216
174
  };
217
175
  };
218
- }, [prevKeysRef.current, reactiveObject.updateEventEmitter]);
176
+ }, [reactiveObject, stableKeys]);
219
177
  const getSnapshot = useMemo(() => {
220
178
  return () => {
221
179
  return snapshotRef.current;
@@ -238,33 +196,28 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
238
196
  if (!pksAreEqual) {
239
197
  prevPksRef.current = pks;
240
198
  }
199
+ const stablePks = pksAreEqual ? prevPksRef.current ?? pks : pks;
241
200
  const snapshotRef = useRef();
242
201
  const subscribe = useMemo(() => {
243
- snapshotRef.current = pks.map((pk) => reactiveCollection.getOneByPk(pk));
202
+ snapshotRef.current = stablePks.map(
203
+ (pk) => reactiveCollection.getOneByPk(pk)
204
+ );
244
205
  return (onStoreChange) => {
245
- const prevPks = prevPksRef.current;
246
- if (!prevPks) {
247
- return () => {
248
- };
249
- }
250
206
  const updateSnapshot = () => {
251
- snapshotRef.current = prevPks.map(
207
+ snapshotRef.current = stablePks.map(
252
208
  (pk) => reactiveCollection.getOneByPk(pk)
253
209
  );
254
210
  onStoreChange();
255
211
  };
256
- reactiveCollection.updateEventEmitter.subscribeOnKeys(
257
- prevPks,
212
+ const unsubscribe = reactiveCollection.subscribeOnKeys(
213
+ stablePks,
258
214
  updateSnapshot
259
215
  );
260
216
  return () => {
261
- reactiveCollection.updateEventEmitter.unsubscribeFromKeys(
262
- prevPks,
263
- updateSnapshot
264
- );
217
+ unsubscribe();
265
218
  };
266
219
  };
267
- }, [prevPksRef.current, reactiveCollection.updateEventEmitter]);
220
+ }, [reactiveCollection, stablePks]);
268
221
  const getSnapshot = useMemo(() => {
269
222
  return () => {
270
223
  return snapshotRef.current;
@@ -274,59 +227,195 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
274
227
  return snapshot;
275
228
  };
276
229
  var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
277
- const pks = useSelectPksByIndexKeySetBased(reactiveIndex, key);
278
- const pksArray = useMemo(
279
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
280
- [pks]
281
- );
282
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
230
+ const snapshotRef = useRef();
231
+ const subscribe = useMemo(() => {
232
+ const readSnapshot = () => Array.from(
233
+ reactiveIndex.getPksByKey(key),
234
+ (pk) => reactiveCollection.getOneByPk(pk)
235
+ );
236
+ const readPks = () => Array.from(reactiveIndex.getPksByKey(key));
237
+ snapshotRef.current = readSnapshot();
238
+ return (onStoreChange) => {
239
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
240
+ snapshotRef.current = readSnapshot();
241
+ onStoreChange();
242
+ });
243
+ const resubscribeCollection = () => {
244
+ unsubscribeFromCollection();
245
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
246
+ readPks(),
247
+ () => {
248
+ snapshotRef.current = readSnapshot();
249
+ onStoreChange();
250
+ }
251
+ );
252
+ };
253
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
254
+ resubscribeCollection();
255
+ snapshotRef.current = readSnapshot();
256
+ onStoreChange();
257
+ });
258
+ return () => {
259
+ unsubscribeFromIndex();
260
+ unsubscribeFromCollection();
261
+ };
262
+ };
263
+ }, [key, reactiveCollection, reactiveIndex]);
264
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
265
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
283
266
  };
284
267
  var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
285
- const pks = useSelectPksByIndexKeysSetBased(reactiveIndex, keys);
286
- const pksArray = useMemo(
287
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
288
- [pks]
289
- );
290
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
268
+ const prevKeysRef = useRef();
269
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
270
+ if (!keysAreEqual) {
271
+ prevKeysRef.current = keys;
272
+ }
273
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
274
+ const snapshotRef = useRef();
275
+ const subscribe = useMemo(() => {
276
+ const readPks = () => stableKeys.flatMap(
277
+ (key) => Array.from(reactiveIndex.getPksByKey(key))
278
+ );
279
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
280
+ snapshotRef.current = readSnapshot();
281
+ return (onStoreChange) => {
282
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
283
+ snapshotRef.current = readSnapshot();
284
+ onStoreChange();
285
+ });
286
+ const resubscribeCollection = () => {
287
+ unsubscribeFromCollection();
288
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
289
+ readPks(),
290
+ () => {
291
+ snapshotRef.current = readSnapshot();
292
+ onStoreChange();
293
+ }
294
+ );
295
+ };
296
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
297
+ stableKeys,
298
+ () => {
299
+ resubscribeCollection();
300
+ snapshotRef.current = readSnapshot();
301
+ onStoreChange();
302
+ }
303
+ );
304
+ return () => {
305
+ unsubscribeFromIndex();
306
+ unsubscribeFromCollection();
307
+ };
308
+ };
309
+ }, [reactiveCollection, reactiveIndex, stableKeys]);
310
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
311
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
291
312
  };
292
313
  var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
293
- const pks = useSelectPksByIndexKeyArrayBased(reactiveIndex, key);
294
- return useSelectEntitiesByPks(
295
- reactiveCollection,
296
- pks || EMPTY_ARRAY
297
- );
314
+ const snapshotRef = useRef();
315
+ const subscribe = useMemo(() => {
316
+ const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
317
+ const readPks = () => reactiveIndex.getPksByKey(key);
318
+ snapshotRef.current = readSnapshot();
319
+ return (onStoreChange) => {
320
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
321
+ snapshotRef.current = readSnapshot();
322
+ onStoreChange();
323
+ });
324
+ const resubscribeCollection = () => {
325
+ unsubscribeFromCollection();
326
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
327
+ readPks(),
328
+ () => {
329
+ snapshotRef.current = readSnapshot();
330
+ onStoreChange();
331
+ }
332
+ );
333
+ };
334
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
335
+ resubscribeCollection();
336
+ snapshotRef.current = readSnapshot();
337
+ onStoreChange();
338
+ });
339
+ return () => {
340
+ unsubscribeFromIndex();
341
+ unsubscribeFromCollection();
342
+ };
343
+ };
344
+ }, [key, reactiveCollection, reactiveIndex]);
345
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
346
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
298
347
  };
299
348
  var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
300
- const pks = useSelectPksByIndexKeysArrayBased(reactiveIndex, keys) || EMPTY_ARRAY;
301
- return useSelectEntitiesByPks(reactiveCollection, pks);
349
+ const prevKeysRef = useRef();
350
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
351
+ if (!keysAreEqual) {
352
+ prevKeysRef.current = keys;
353
+ }
354
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
355
+ const snapshotRef = useRef();
356
+ const subscribe = useMemo(() => {
357
+ const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
358
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
359
+ snapshotRef.current = readSnapshot();
360
+ return (onStoreChange) => {
361
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
362
+ snapshotRef.current = readSnapshot();
363
+ onStoreChange();
364
+ });
365
+ const resubscribeCollection = () => {
366
+ unsubscribeFromCollection();
367
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
368
+ readPks(),
369
+ () => {
370
+ snapshotRef.current = readSnapshot();
371
+ onStoreChange();
372
+ }
373
+ );
374
+ };
375
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
376
+ stableKeys,
377
+ () => {
378
+ resubscribeCollection();
379
+ snapshotRef.current = readSnapshot();
380
+ onStoreChange();
381
+ }
382
+ );
383
+ return () => {
384
+ unsubscribeFromIndex();
385
+ unsubscribeFromCollection();
386
+ };
387
+ };
388
+ }, [reactiveCollection, reactiveIndex, stableKeys]);
389
+ const getSnapshot = useMemo(() => () => snapshotRef.current, []);
390
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
302
391
  };
303
392
 
304
393
  // src/context/index.tsx
305
394
  import { createContext, useContext } from "react";
306
395
  import { jsx } from "react/jsx-runtime";
307
- var OIMRICollectionsContext = createContext(
396
+ var OIMCollectionsContext = createContext(
308
397
  {}
309
398
  );
310
399
  function createOIMCollectionsContext() {
311
400
  return createContext({});
312
401
  }
313
- function OIMRICollectionsProvider({ collections, children, context }) {
314
- const ContextToUse = context || OIMRICollectionsContext;
402
+ function OIMCollectionsProvider({ collections, children, context }) {
403
+ const ContextToUse = context || OIMCollectionsContext;
315
404
  return /* @__PURE__ */ jsx(ContextToUse.Provider, { value: collections, children });
316
405
  }
317
406
  function useOIMCollectionsContext(context) {
318
- const contextToUse = context || OIMRICollectionsContext;
407
+ const contextToUse = context || OIMCollectionsContext;
319
408
  const collections = useContext(contextToUse);
320
409
  if (!collections || Object.keys(collections).length === 0) {
321
410
  throw new Error(
322
- "useOIMCollectionsContext must be used within an OIMRICollectionsProvider"
411
+ "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
323
412
  );
324
413
  }
325
414
  return collections;
326
415
  }
327
416
  export {
328
- OIMRICollectionsContext,
329
- OIMRICollectionsProvider,
417
+ OIMCollectionsContext,
418
+ OIMCollectionsProvider,
330
419
  createOIMCollectionsContext,
331
420
  useOIMCollectionsContext,
332
421
  useSelectEntitiesByIndexKeyArrayBased,
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "1.2.2",
3
+ "version": "2.0.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",
7
7
  "module": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
9
  "sideEffects": false,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
10
13
  "repository": {
11
14
  "type": "git",
12
15
  "url": "git+https://github.com/abaikov/oimdb.git"
@@ -23,7 +26,7 @@
23
26
  }
24
27
  },
25
28
  "scripts": {
26
- "build": "tsup src/index.ts --dts --format esm,cjs",
29
+ "build": "tsup --config tsup.config.ts --tsconfig tsconfig.build.json",
27
30
  "test": "jest",
28
31
  "type-check": "tsc --noEmit",
29
32
  "clean": "rimraf dist",
@@ -33,8 +36,8 @@
33
36
  "dist"
34
37
  ],
35
38
  "peerDependencies": {
36
- "@oimdb/core": "^1.4.0",
37
- "react": "^18.0.0"
39
+ "@oimdb/core": "^2.0.0",
40
+ "react": "^18.0.0 || ^19.0.0"
38
41
  },
39
42
  "devDependencies": {
40
43
  "@testing-library/react": "^14.3.1",