@oimdb/react 1.0.1 → 1.0.3

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
@@ -36,73 +36,84 @@ module.exports = __toCommonJS(index_exports);
36
36
  // src/hooks/index.ts
37
37
  var import_react = require("react");
38
38
  var import_react2 = require("react");
39
+ var EMPTY_ARRAY = [];
39
40
  var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
41
+ const snapshotValueRef = (0, import_react.useRef)(null);
40
42
  const subscribe = (0, import_react.useMemo)(() => {
41
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(
42
- keys,
43
- onStoreChange
44
- );
43
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, () => {
44
+ snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
45
+ onStoreChange();
46
+ });
45
47
  }, [keys, reactiveIndex.updateEventEmitter]);
46
- const getSnapshot = (0, import_react.useMemo)(
47
- () => () => keys.map((key) => reactiveIndex.getPksByKey(key)),
48
- [keys, reactiveIndex]
49
- );
48
+ const getSnapshot = (0, import_react.useMemo)(() => {
49
+ return () => {
50
+ return snapshotValueRef.current;
51
+ };
52
+ }, []);
50
53
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
51
54
  return snapshot;
52
55
  };
53
56
  var useSelectPksByIndexKey = (reactiveIndex, key) => {
57
+ const snapshotValueRef = (0, import_react.useRef)(null);
54
58
  const subscribe = (0, import_react.useMemo)(() => {
55
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
59
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, () => {
60
+ snapshotValueRef.current = Array.from(
61
+ reactiveIndex.getPksByKey(key)
62
+ );
63
+ onStoreChange();
64
+ });
56
65
  }, [key, reactiveIndex.updateEventEmitter]);
57
- const getSnapshot = (0, import_react.useMemo)(
58
- () => () => reactiveIndex.getPksByKey(key),
59
- [key, reactiveIndex]
60
- );
66
+ const getSnapshot = (0, import_react.useMemo)(() => {
67
+ return () => {
68
+ return snapshotValueRef.current;
69
+ };
70
+ }, []);
61
71
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
62
72
  return snapshot;
63
73
  };
64
74
  var selectEntityByPk = (reactiveCollection, pk) => {
75
+ const snapshotValueRef = (0, import_react.useRef)(void 0);
65
76
  const subscribe = (0, import_react.useMemo)(() => {
66
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(
67
- pk,
68
- onStoreChange
69
- );
77
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(pk, () => {
78
+ snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
79
+ onStoreChange();
80
+ });
70
81
  }, [pk, reactiveCollection.updateEventEmitter]);
71
- const getSnapshot = (0, import_react.useMemo)(
72
- () => () => reactiveCollection.getOneByPk(pk),
73
- [pk, reactiveCollection]
74
- );
82
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotValueRef.current, []);
75
83
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
76
84
  return snapshot;
77
85
  };
78
86
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
87
+ const snapshotValueRef = (0, import_react.useRef)(null);
79
88
  const subscribe = (0, import_react.useMemo)(() => {
80
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(
81
- pks,
82
- onStoreChange
83
- );
89
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, () => {
90
+ snapshotValueRef.current = pks.map(
91
+ (pk) => reactiveCollection.getOneByPk(pk)
92
+ );
93
+ onStoreChange();
94
+ });
84
95
  }, [pks, reactiveCollection.updateEventEmitter]);
85
- const getSnapshot = (0, import_react.useMemo)(
86
- () => () => pks.map((pk) => reactiveCollection.getOneByPk(pk)),
87
- [pks, reactiveCollection]
88
- );
96
+ const getSnapshot = (0, import_react.useMemo)(() => {
97
+ return () => {
98
+ return snapshotValueRef.current;
99
+ };
100
+ }, []);
89
101
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
90
102
  return snapshot;
91
103
  };
92
104
  var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
93
105
  const pks = useSelectPksByIndexKey(reactiveIndex, key);
94
- return useSelectEntitiesByPks(reactiveCollection, pks);
106
+ return useSelectEntitiesByPks(
107
+ reactiveCollection,
108
+ pks || EMPTY_ARRAY
109
+ );
95
110
  };
96
111
  var unique = (array) => {
97
112
  return Array.from(new Set(array));
98
113
  };
99
114
  var selectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
100
- const pks = useSelectPksByIndexKeys(reactiveIndex, keys);
101
- const pksArray = (0, import_react.useMemo)(
102
- () => unique(Array.from(pks.values()).flat()),
103
- [pks]
104
- );
105
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
115
+ const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
116
+ return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
106
117
  };
107
118
 
108
119
  // src/context/index.tsx
package/dist/index.d.cts CHANGED
@@ -3,12 +3,12 @@ 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
- declare const useSelectPksByIndexKeys: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => (readonly TPk[])[];
7
- declare const useSelectPksByIndexKey: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TPk[];
6
+ declare const useSelectPksByIndexKeys: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly TPk[] | null;
7
+ declare const useSelectPksByIndexKey: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TPk[] | null;
8
8
  declare const selectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
9
- declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => (TEntity | undefined)[];
10
- declare const selectEntitiesByIndexKey: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => (TEntity | undefined)[];
11
- declare const selectEntitiesByIndexKeys: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => (TEntity | undefined)[];
9
+ declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly TEntity[] | null;
10
+ declare const selectEntitiesByIndexKey: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TEntity[] | null;
11
+ declare const selectEntitiesByIndexKeys: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly TEntity[] | null;
12
12
 
13
13
  type AnyOIMRICollection = OIMRICollection<object, TOIMPk, string, TOIMPk, any, any>;
14
14
  type CollectionsDictionary = Record<string, AnyOIMRICollection>;
package/dist/index.d.ts CHANGED
@@ -3,12 +3,12 @@ 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
- declare const useSelectPksByIndexKeys: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => (readonly TPk[])[];
7
- declare const useSelectPksByIndexKey: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TPk[];
6
+ declare const useSelectPksByIndexKeys: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly TPk[] | null;
7
+ declare const useSelectPksByIndexKey: <TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TPk[] | null;
8
8
  declare const selectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
9
- declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => (TEntity | undefined)[];
10
- declare const selectEntitiesByIndexKey: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => (TEntity | undefined)[];
11
- declare const selectEntitiesByIndexKeys: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => (TEntity | undefined)[];
9
+ declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly TEntity[] | null;
10
+ declare const selectEntitiesByIndexKey: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, key: TKey) => readonly TEntity[] | null;
11
+ declare const selectEntitiesByIndexKeys: <TEntity extends object, TPk extends TOIMPk, TKey extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, reactiveIndex: OIMReactiveIndex<TKey, TPk, TIndex>, keys: readonly TKey[]) => readonly TEntity[] | null;
12
12
 
13
13
  type AnyOIMRICollection = OIMRICollection<object, TOIMPk, string, TOIMPk, any, any>;
14
14
  type CollectionsDictionary = Record<string, AnyOIMRICollection>;
package/dist/index.js CHANGED
@@ -1,73 +1,84 @@
1
1
  // src/hooks/index.ts
2
- import { useMemo } from "react";
2
+ import { useMemo, useRef } from "react";
3
3
  import { useSyncExternalStore } from "react";
4
+ var EMPTY_ARRAY = [];
4
5
  var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
6
+ const snapshotValueRef = useRef(null);
5
7
  const subscribe = useMemo(() => {
6
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(
7
- keys,
8
- onStoreChange
9
- );
8
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, () => {
9
+ snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
10
+ onStoreChange();
11
+ });
10
12
  }, [keys, reactiveIndex.updateEventEmitter]);
11
- const getSnapshot = useMemo(
12
- () => () => keys.map((key) => reactiveIndex.getPksByKey(key)),
13
- [keys, reactiveIndex]
14
- );
13
+ const getSnapshot = useMemo(() => {
14
+ return () => {
15
+ return snapshotValueRef.current;
16
+ };
17
+ }, []);
15
18
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
16
19
  return snapshot;
17
20
  };
18
21
  var useSelectPksByIndexKey = (reactiveIndex, key) => {
22
+ const snapshotValueRef = useRef(null);
19
23
  const subscribe = useMemo(() => {
20
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
24
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, () => {
25
+ snapshotValueRef.current = Array.from(
26
+ reactiveIndex.getPksByKey(key)
27
+ );
28
+ onStoreChange();
29
+ });
21
30
  }, [key, reactiveIndex.updateEventEmitter]);
22
- const getSnapshot = useMemo(
23
- () => () => reactiveIndex.getPksByKey(key),
24
- [key, reactiveIndex]
25
- );
31
+ const getSnapshot = useMemo(() => {
32
+ return () => {
33
+ return snapshotValueRef.current;
34
+ };
35
+ }, []);
26
36
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
27
37
  return snapshot;
28
38
  };
29
39
  var selectEntityByPk = (reactiveCollection, pk) => {
40
+ const snapshotValueRef = useRef(void 0);
30
41
  const subscribe = useMemo(() => {
31
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(
32
- pk,
33
- onStoreChange
34
- );
42
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(pk, () => {
43
+ snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
44
+ onStoreChange();
45
+ });
35
46
  }, [pk, reactiveCollection.updateEventEmitter]);
36
- const getSnapshot = useMemo(
37
- () => () => reactiveCollection.getOneByPk(pk),
38
- [pk, reactiveCollection]
39
- );
47
+ const getSnapshot = useMemo(() => () => snapshotValueRef.current, []);
40
48
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
41
49
  return snapshot;
42
50
  };
43
51
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
52
+ const snapshotValueRef = useRef(null);
44
53
  const subscribe = useMemo(() => {
45
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(
46
- pks,
47
- onStoreChange
48
- );
54
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, () => {
55
+ snapshotValueRef.current = pks.map(
56
+ (pk) => reactiveCollection.getOneByPk(pk)
57
+ );
58
+ onStoreChange();
59
+ });
49
60
  }, [pks, reactiveCollection.updateEventEmitter]);
50
- const getSnapshot = useMemo(
51
- () => () => pks.map((pk) => reactiveCollection.getOneByPk(pk)),
52
- [pks, reactiveCollection]
53
- );
61
+ const getSnapshot = useMemo(() => {
62
+ return () => {
63
+ return snapshotValueRef.current;
64
+ };
65
+ }, []);
54
66
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
55
67
  return snapshot;
56
68
  };
57
69
  var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
58
70
  const pks = useSelectPksByIndexKey(reactiveIndex, key);
59
- return useSelectEntitiesByPks(reactiveCollection, pks);
71
+ return useSelectEntitiesByPks(
72
+ reactiveCollection,
73
+ pks || EMPTY_ARRAY
74
+ );
60
75
  };
61
76
  var unique = (array) => {
62
77
  return Array.from(new Set(array));
63
78
  };
64
79
  var selectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
65
- const pks = useSelectPksByIndexKeys(reactiveIndex, keys);
66
- const pksArray = useMemo(
67
- () => unique(Array.from(pks.values()).flat()),
68
- [pks]
69
- );
70
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
80
+ const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
81
+ return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
71
82
  };
72
83
 
73
84
  // src/context/index.tsx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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",