@oimdb/react 1.3.0 → 2.0.1

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,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- OIMRICollectionsContext: () => OIMRICollectionsContext,
24
- OIMRICollectionsProvider: () => OIMRICollectionsProvider,
23
+ OIMCollectionsContext: () => OIMCollectionsContext,
24
+ OIMCollectionsProvider: () => OIMCollectionsProvider,
25
25
  createOIMCollectionsContext: () => createOIMCollectionsContext,
26
26
  useOIMCollectionsContext: () => useOIMCollectionsContext,
27
27
  useSelectEntitiesByIndexKeyArrayBased: () => useSelectEntitiesByIndexKeyArrayBased,
@@ -76,16 +76,22 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
76
76
  return snapshot;
77
77
  };
78
78
  var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
79
- const getSnapshot = (0, import_react.useMemo)(() => {
80
- return () => {
81
- return reactiveIndex.getPksByKey(key);
82
- };
83
- }, [key, reactiveIndex]);
79
+ const snapshotValueRef = (0, import_react.useRef)();
84
80
  const subscribe = (0, import_react.useMemo)(() => {
81
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
85
82
  return (onStoreChange) => {
86
- return reactiveIndex.subscribeOnKey(key, onStoreChange);
83
+ const updateSnapshot = () => {
84
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
85
+ onStoreChange();
86
+ };
87
+ return reactiveIndex.subscribeOnKey(key, updateSnapshot);
87
88
  };
88
89
  }, [key, reactiveIndex]);
90
+ const getSnapshot = (0, import_react.useMemo)(() => {
91
+ return () => {
92
+ return snapshotValueRef.current;
93
+ };
94
+ }, []);
89
95
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
90
96
  return snapshot;
91
97
  };
@@ -262,60 +268,196 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
262
268
  return snapshot;
263
269
  };
264
270
  var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
265
- const pks = useSelectPksByIndexKeySetBased(reactiveIndex, key);
266
- const pksArray = (0, import_react.useMemo)(
267
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
268
- [pks]
269
- );
270
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
271
+ const snapshotRef = (0, import_react.useRef)();
272
+ const subscribe = (0, import_react.useMemo)(() => {
273
+ const readSnapshot = () => Array.from(
274
+ reactiveIndex.getPksByKey(key),
275
+ (pk) => reactiveCollection.getOneByPk(pk)
276
+ );
277
+ const readPks = () => Array.from(reactiveIndex.getPksByKey(key));
278
+ snapshotRef.current = readSnapshot();
279
+ return (onStoreChange) => {
280
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
281
+ snapshotRef.current = readSnapshot();
282
+ onStoreChange();
283
+ });
284
+ const resubscribeCollection = () => {
285
+ unsubscribeFromCollection();
286
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
287
+ readPks(),
288
+ () => {
289
+ snapshotRef.current = readSnapshot();
290
+ onStoreChange();
291
+ }
292
+ );
293
+ };
294
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKey(key, () => {
295
+ resubscribeCollection();
296
+ snapshotRef.current = readSnapshot();
297
+ onStoreChange();
298
+ });
299
+ return () => {
300
+ unsubscribeFromIndex();
301
+ unsubscribeFromCollection();
302
+ };
303
+ };
304
+ }, [key, reactiveCollection, reactiveIndex]);
305
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
306
+ return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
271
307
  };
272
308
  var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
273
- const pks = useSelectPksByIndexKeysSetBased(reactiveIndex, keys);
274
- const pksArray = (0, import_react.useMemo)(
275
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
276
- [pks]
277
- );
278
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
309
+ const prevKeysRef = (0, import_react.useRef)();
310
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
311
+ if (!keysAreEqual) {
312
+ prevKeysRef.current = keys;
313
+ }
314
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
315
+ const snapshotRef = (0, import_react.useRef)();
316
+ const subscribe = (0, import_react.useMemo)(() => {
317
+ const readPks = () => stableKeys.flatMap(
318
+ (key) => Array.from(reactiveIndex.getPksByKey(key))
319
+ );
320
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
321
+ snapshotRef.current = readSnapshot();
322
+ return (onStoreChange) => {
323
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
324
+ snapshotRef.current = readSnapshot();
325
+ onStoreChange();
326
+ });
327
+ const resubscribeCollection = () => {
328
+ unsubscribeFromCollection();
329
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
330
+ readPks(),
331
+ () => {
332
+ snapshotRef.current = readSnapshot();
333
+ onStoreChange();
334
+ }
335
+ );
336
+ };
337
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
338
+ stableKeys,
339
+ () => {
340
+ resubscribeCollection();
341
+ snapshotRef.current = readSnapshot();
342
+ onStoreChange();
343
+ }
344
+ );
345
+ return () => {
346
+ unsubscribeFromIndex();
347
+ unsubscribeFromCollection();
348
+ };
349
+ };
350
+ }, [reactiveCollection, reactiveIndex, stableKeys]);
351
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
352
+ return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
279
353
  };
280
354
  var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
281
- const pks = useSelectPksByIndexKeyArrayBased(reactiveIndex, key);
282
- return useSelectEntitiesByPks(
283
- reactiveCollection,
284
- pks || EMPTY_ARRAY
285
- );
355
+ const snapshotRef = (0, import_react.useRef)();
356
+ const subscribe = (0, import_react.useMemo)(() => {
357
+ const readSnapshot = () => reactiveIndex.getPksByKey(key).map((pk) => reactiveCollection.getOneByPk(pk));
358
+ const readPks = () => reactiveIndex.getPksByKey(key);
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.subscribeOnKey(key, () => {
376
+ resubscribeCollection();
377
+ snapshotRef.current = readSnapshot();
378
+ onStoreChange();
379
+ });
380
+ return () => {
381
+ unsubscribeFromIndex();
382
+ unsubscribeFromCollection();
383
+ };
384
+ };
385
+ }, [key, reactiveCollection, reactiveIndex]);
386
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
387
+ return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
286
388
  };
287
389
  var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
288
- const pks = useSelectPksByIndexKeysArrayBased(reactiveIndex, keys) || EMPTY_ARRAY;
289
- return useSelectEntitiesByPks(reactiveCollection, pks);
390
+ const prevKeysRef = (0, import_react.useRef)();
391
+ const keysAreEqual = arraysEqual(prevKeysRef.current || EMPTY_ARRAY, keys);
392
+ if (!keysAreEqual) {
393
+ prevKeysRef.current = keys;
394
+ }
395
+ const stableKeys = keysAreEqual ? prevKeysRef.current ?? keys : keys;
396
+ const snapshotRef = (0, import_react.useRef)();
397
+ const subscribe = (0, import_react.useMemo)(() => {
398
+ const readPks = () => stableKeys.flatMap((key) => reactiveIndex.getPksByKey(key));
399
+ const readSnapshot = () => readPks().map((pk) => reactiveCollection.getOneByPk(pk));
400
+ snapshotRef.current = readSnapshot();
401
+ return (onStoreChange) => {
402
+ let unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(readPks(), () => {
403
+ snapshotRef.current = readSnapshot();
404
+ onStoreChange();
405
+ });
406
+ const resubscribeCollection = () => {
407
+ unsubscribeFromCollection();
408
+ unsubscribeFromCollection = reactiveCollection.subscribeOnKeys(
409
+ readPks(),
410
+ () => {
411
+ snapshotRef.current = readSnapshot();
412
+ onStoreChange();
413
+ }
414
+ );
415
+ };
416
+ const unsubscribeFromIndex = reactiveIndex.subscribeOnKeys(
417
+ stableKeys,
418
+ () => {
419
+ resubscribeCollection();
420
+ snapshotRef.current = readSnapshot();
421
+ onStoreChange();
422
+ }
423
+ );
424
+ return () => {
425
+ unsubscribeFromIndex();
426
+ unsubscribeFromCollection();
427
+ };
428
+ };
429
+ }, [reactiveCollection, reactiveIndex, stableKeys]);
430
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotRef.current, []);
431
+ return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
290
432
  };
291
433
 
292
434
  // src/context/index.tsx
293
435
  var import_react3 = require("react");
294
436
  var import_jsx_runtime = require("react/jsx-runtime");
295
- var OIMRICollectionsContext = (0, import_react3.createContext)(
437
+ var OIMCollectionsContext = (0, import_react3.createContext)(
296
438
  {}
297
439
  );
298
440
  function createOIMCollectionsContext() {
299
441
  return (0, import_react3.createContext)({});
300
442
  }
301
- function OIMRICollectionsProvider({ collections, children, context }) {
302
- const ContextToUse = context || OIMRICollectionsContext;
443
+ function OIMCollectionsProvider({ collections, children, context }) {
444
+ const ContextToUse = context || OIMCollectionsContext;
303
445
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ContextToUse.Provider, { value: collections, children });
304
446
  }
305
447
  function useOIMCollectionsContext(context) {
306
- const contextToUse = context || OIMRICollectionsContext;
448
+ const contextToUse = context || OIMCollectionsContext;
307
449
  const collections = (0, import_react3.useContext)(contextToUse);
308
450
  if (!collections || Object.keys(collections).length === 0) {
309
451
  throw new Error(
310
- "useOIMCollectionsContext must be used within an OIMRICollectionsProvider"
452
+ "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
311
453
  );
312
454
  }
313
455
  return collections;
314
456
  }
315
457
  // Annotate the CommonJS export names for ESM import in node:
316
458
  0 && (module.exports = {
317
- OIMRICollectionsContext,
318
- OIMRICollectionsProvider,
459
+ OIMCollectionsContext,
460
+ OIMCollectionsProvider,
319
461
  createOIMCollectionsContext,
320
462
  useOIMCollectionsContext,
321
463
  useSelectEntitiesByIndexKeyArrayBased,
package/dist/index.d.cts CHANGED
@@ -1,10 +1,10 @@
1
- import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject, OIMRICollection } from '@oimdb/core';
1
+ import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject } 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';
5
5
 
6
6
  declare const useSelectPksByIndexKeysSetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
7
- declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk>;
7
+ declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk> | undefined;
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;
@@ -16,16 +16,16 @@ declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk
16
16
  declare const useSelectEntitiesByIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
17
17
  declare const useSelectEntitiesByIndexKeysArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
18
18
 
19
- type CollectionsDictionary = Record<string, OIMRICollection<any, any, any, any>>;
19
+ type CollectionsDictionary = Record<string, OIMReactiveCollection<any, any>>;
20
20
  type OIMContextValue<T extends CollectionsDictionary = CollectionsDictionary> = T;
21
- declare const OIMRICollectionsContext: React.Context<CollectionsDictionary>;
21
+ declare const OIMCollectionsContext: React.Context<CollectionsDictionary>;
22
22
  declare function createOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(): React.Context<OIMContextValue<T>>;
23
- interface OIMRICollectionsProviderProps<T extends CollectionsDictionary = CollectionsDictionary> {
23
+ interface OIMCollectionsProviderProps<T extends CollectionsDictionary = CollectionsDictionary> {
24
24
  collections: T;
25
25
  children: ReactNode;
26
26
  context?: React.Context<OIMContextValue<T>>;
27
27
  }
28
- declare function OIMRICollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMRICollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
28
+ declare function OIMCollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMCollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
29
29
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
30
30
 
31
- export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
31
+ export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject, OIMRICollection } from '@oimdb/core';
1
+ import { TOIMPk, OIMIndexSetBased, OIMReactiveIndexSetBased, OIMIndexArrayBased, OIMReactiveIndexArrayBased, OIMReactiveCollection, OIMReactiveObject } 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';
5
5
 
6
6
  declare const useSelectPksByIndexKeysSetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => TPk[] | undefined;
7
- declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk>;
7
+ declare const useSelectPksByIndexKeySetBased: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>>(reactiveIndex: OIMReactiveIndexSetBased<TKey, TPk, TIndex>, key: TKey) => Set<TPk> | undefined;
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;
@@ -16,16 +16,16 @@ declare const useSelectEntitiesByIndexKeysSetBased: <TEntity extends object, TPk
16
16
  declare const useSelectEntitiesByIndexKeyArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, key: TKey) => readonly (TEntity | undefined)[] | undefined;
17
17
  declare const useSelectEntitiesByIndexKeysArrayBased: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndexArrayBased<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly (TEntity | undefined)[] | undefined;
18
18
 
19
- type CollectionsDictionary = Record<string, OIMRICollection<any, any, any, any>>;
19
+ type CollectionsDictionary = Record<string, OIMReactiveCollection<any, any>>;
20
20
  type OIMContextValue<T extends CollectionsDictionary = CollectionsDictionary> = T;
21
- declare const OIMRICollectionsContext: React.Context<CollectionsDictionary>;
21
+ declare const OIMCollectionsContext: React.Context<CollectionsDictionary>;
22
22
  declare function createOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(): React.Context<OIMContextValue<T>>;
23
- interface OIMRICollectionsProviderProps<T extends CollectionsDictionary = CollectionsDictionary> {
23
+ interface OIMCollectionsProviderProps<T extends CollectionsDictionary = CollectionsDictionary> {
24
24
  collections: T;
25
25
  children: ReactNode;
26
26
  context?: React.Context<OIMContextValue<T>>;
27
27
  }
28
- declare function OIMRICollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMRICollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
28
+ declare function OIMCollectionsProvider<T extends CollectionsDictionary = CollectionsDictionary>({ collections, children, context }: OIMCollectionsProviderProps<T>): react_jsx_runtime.JSX.Element;
29
29
  declare function useOIMCollectionsContext<T extends CollectionsDictionary = CollectionsDictionary>(context?: React.Context<OIMContextValue<T>>): T;
30
30
 
31
- export { type CollectionsDictionary, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
31
+ export { type CollectionsDictionary, OIMCollectionsContext, OIMCollectionsProvider, type OIMCollectionsProviderProps, type OIMContextValue, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKeyArrayBased, useSelectEntitiesByIndexKeySetBased, useSelectEntitiesByIndexKeysArrayBased, useSelectEntitiesByIndexKeysSetBased, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKeyArrayBased, useSelectPksByIndexKeySetBased, useSelectPksByIndexKeysArrayBased, useSelectPksByIndexKeysSetBased, useSelectValueByObjectKey, useSelectValuesByObjectKeys };
package/dist/index.js CHANGED
@@ -35,16 +35,22 @@ var useSelectPksByIndexKeysSetBased = (reactiveIndex, keys) => {
35
35
  return snapshot;
36
36
  };
37
37
  var useSelectPksByIndexKeySetBased = (reactiveIndex, key) => {
38
- const getSnapshot = useMemo(() => {
39
- return () => {
40
- return reactiveIndex.getPksByKey(key);
41
- };
42
- }, [key, reactiveIndex]);
38
+ const snapshotValueRef = useRef();
43
39
  const subscribe = useMemo(() => {
40
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
44
41
  return (onStoreChange) => {
45
- return reactiveIndex.subscribeOnKey(key, onStoreChange);
42
+ const updateSnapshot = () => {
43
+ snapshotValueRef.current = reactiveIndex.getPksByKey(key);
44
+ onStoreChange();
45
+ };
46
+ return reactiveIndex.subscribeOnKey(key, updateSnapshot);
46
47
  };
47
48
  }, [key, reactiveIndex]);
49
+ const getSnapshot = useMemo(() => {
50
+ return () => {
51
+ return snapshotValueRef.current;
52
+ };
53
+ }, []);
48
54
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
49
55
  return snapshot;
50
56
  };
@@ -221,59 +227,195 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
221
227
  return snapshot;
222
228
  };
223
229
  var useSelectEntitiesByIndexKeySetBased = (reactiveCollection, reactiveIndex, key) => {
224
- const pks = useSelectPksByIndexKeySetBased(reactiveIndex, key);
225
- const pksArray = useMemo(
226
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
227
- [pks]
228
- );
229
- 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);
230
266
  };
231
267
  var useSelectEntitiesByIndexKeysSetBased = (reactiveCollection, reactiveIndex, keys) => {
232
- const pks = useSelectPksByIndexKeysSetBased(reactiveIndex, keys);
233
- const pksArray = useMemo(
234
- () => pks ? Array.from(pks) : EMPTY_ARRAY,
235
- [pks]
236
- );
237
- 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);
238
312
  };
239
313
  var useSelectEntitiesByIndexKeyArrayBased = (reactiveCollection, reactiveIndex, key) => {
240
- const pks = useSelectPksByIndexKeyArrayBased(reactiveIndex, key);
241
- return useSelectEntitiesByPks(
242
- reactiveCollection,
243
- pks || EMPTY_ARRAY
244
- );
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);
245
347
  };
246
348
  var useSelectEntitiesByIndexKeysArrayBased = (reactiveCollection, reactiveIndex, keys) => {
247
- const pks = useSelectPksByIndexKeysArrayBased(reactiveIndex, keys) || EMPTY_ARRAY;
248
- 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);
249
391
  };
250
392
 
251
393
  // src/context/index.tsx
252
394
  import { createContext, useContext } from "react";
253
395
  import { jsx } from "react/jsx-runtime";
254
- var OIMRICollectionsContext = createContext(
396
+ var OIMCollectionsContext = createContext(
255
397
  {}
256
398
  );
257
399
  function createOIMCollectionsContext() {
258
400
  return createContext({});
259
401
  }
260
- function OIMRICollectionsProvider({ collections, children, context }) {
261
- const ContextToUse = context || OIMRICollectionsContext;
402
+ function OIMCollectionsProvider({ collections, children, context }) {
403
+ const ContextToUse = context || OIMCollectionsContext;
262
404
  return /* @__PURE__ */ jsx(ContextToUse.Provider, { value: collections, children });
263
405
  }
264
406
  function useOIMCollectionsContext(context) {
265
- const contextToUse = context || OIMRICollectionsContext;
407
+ const contextToUse = context || OIMCollectionsContext;
266
408
  const collections = useContext(contextToUse);
267
409
  if (!collections || Object.keys(collections).length === 0) {
268
410
  throw new Error(
269
- "useOIMCollectionsContext must be used within an OIMRICollectionsProvider"
411
+ "useOIMCollectionsContext must be used within an OIMCollectionsProvider"
270
412
  );
271
413
  }
272
414
  return collections;
273
415
  }
274
416
  export {
275
- OIMRICollectionsContext,
276
- OIMRICollectionsProvider,
417
+ OIMCollectionsContext,
418
+ OIMCollectionsProvider,
277
419
  createOIMCollectionsContext,
278
420
  useOIMCollectionsContext,
279
421
  useSelectEntitiesByIndexKeyArrayBased,
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "1.3.0",
3
+ "version": "2.0.1",
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"
@@ -33,7 +36,7 @@
33
36
  "dist"
34
37
  ],
35
38
  "peerDependencies": {
36
- "@oimdb/core": "^1.4.1",
39
+ "@oimdb/core": "^2.0.0",
37
40
  "react": "^18.0.0 || ^19.0.0"
38
41
  },
39
42
  "devDependencies": {