@oimdb/react 1.0.2 → 1.0.5

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,95 +36,92 @@ 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)(
42
+ keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat()
43
+ );
40
44
  const subscribe = (0, import_react.useMemo)(() => {
41
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(
42
- keys,
43
- onStoreChange
44
- );
45
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, () => {
46
+ snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
47
+ onStoreChange();
48
+ });
45
49
  }, [keys, reactiveIndex.updateEventEmitter]);
46
50
  const getSnapshot = (0, import_react.useMemo)(() => {
47
- let cachedResult = null;
48
- let cachedKeys = null;
49
51
  return () => {
50
- if (cachedKeys !== keys || !cachedResult) {
51
- cachedResult = keys.map((key) => reactiveIndex.getPksByKey(key));
52
- cachedKeys = keys;
53
- }
54
- return cachedResult;
52
+ return snapshotValueRef.current;
55
53
  };
56
- }, [keys, reactiveIndex]);
54
+ }, []);
57
55
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
58
56
  return snapshot;
59
57
  };
60
58
  var useSelectPksByIndexKey = (reactiveIndex, key) => {
59
+ const snapshotValueRef = (0, import_react.useRef)(
60
+ Array.from(reactiveIndex.getPksByKey(key))
61
+ );
61
62
  const subscribe = (0, import_react.useMemo)(() => {
62
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
63
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, () => {
64
+ snapshotValueRef.current = Array.from(
65
+ reactiveIndex.getPksByKey(key)
66
+ );
67
+ onStoreChange();
68
+ });
63
69
  }, [key, reactiveIndex.updateEventEmitter]);
64
- const getSnapshot = (0, import_react.useMemo)(
65
- () => () => reactiveIndex.getPksByKey(key),
66
- [key, reactiveIndex]
67
- );
70
+ const getSnapshot = (0, import_react.useMemo)(() => {
71
+ return () => {
72
+ return snapshotValueRef.current;
73
+ };
74
+ }, []);
68
75
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
69
76
  return snapshot;
70
77
  };
71
78
  var selectEntityByPk = (reactiveCollection, pk) => {
79
+ const snapshotValueRef = (0, import_react.useRef)(
80
+ reactiveCollection.getOneByPk(pk)
81
+ );
72
82
  const subscribe = (0, import_react.useMemo)(() => {
73
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(
74
- pk,
75
- onStoreChange
76
- );
83
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(pk, () => {
84
+ snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
85
+ onStoreChange();
86
+ });
77
87
  }, [pk, reactiveCollection.updateEventEmitter]);
78
- const getSnapshot = (0, import_react.useMemo)(
79
- () => () => reactiveCollection.getOneByPk(pk),
80
- [pk, reactiveCollection]
81
- );
88
+ const getSnapshot = (0, import_react.useMemo)(() => () => snapshotValueRef.current, []);
82
89
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
83
90
  return snapshot;
84
91
  };
85
92
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
93
+ const snapshotValueRef = (0, import_react.useRef)(
94
+ pks.map((pk) => reactiveCollection.getOneByPk(pk))
95
+ );
86
96
  const subscribe = (0, import_react.useMemo)(() => {
87
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(
88
- pks,
89
- onStoreChange
90
- );
97
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, () => {
98
+ snapshotValueRef.current = pks.map(
99
+ (pk) => reactiveCollection.getOneByPk(pk)
100
+ );
101
+ onStoreChange();
102
+ });
91
103
  }, [pks, reactiveCollection.updateEventEmitter]);
92
104
  const getSnapshot = (0, import_react.useMemo)(() => {
93
- let cachedResult = null;
94
- let cachedPks = null;
95
105
  return () => {
96
- if (cachedPks !== pks || !cachedResult) {
97
- cachedResult = pks.map((pk) => reactiveCollection.getOneByPk(pk));
98
- cachedPks = pks;
99
- }
100
- return cachedResult;
106
+ return snapshotValueRef.current;
101
107
  };
102
- }, [pks, reactiveCollection]);
108
+ }, []);
103
109
  const snapshot = (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
104
110
  return snapshot;
105
111
  };
106
112
  var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
107
113
  const pks = useSelectPksByIndexKey(reactiveIndex, key);
108
- return useSelectEntitiesByPks(reactiveCollection, pks);
114
+ return useSelectEntitiesByPks(
115
+ reactiveCollection,
116
+ pks || EMPTY_ARRAY
117
+ );
109
118
  };
110
119
  var unique = (array) => {
111
120
  return Array.from(new Set(array));
112
121
  };
113
122
  var selectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
114
- const pks = useSelectPksByIndexKeys(reactiveIndex, keys);
115
- const pksArray = (0, import_react.useMemo)(() => {
116
- let cachedResult = null;
117
- let cachedPks = null;
118
- const getCurrentPksArray = () => {
119
- if (cachedPks !== pks || !cachedResult) {
120
- cachedResult = unique(Array.from(pks.values()).flat());
121
- cachedPks = pks;
122
- }
123
- return cachedResult;
124
- };
125
- return getCurrentPksArray();
126
- }, [pks]);
127
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
123
+ const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
124
+ return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
128
125
  };
129
126
 
130
127
  // 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 (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 (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,95 +1,92 @@
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(
7
+ keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat()
8
+ );
5
9
  const subscribe = useMemo(() => {
6
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(
7
- keys,
8
- onStoreChange
9
- );
10
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, () => {
11
+ snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
12
+ onStoreChange();
13
+ });
10
14
  }, [keys, reactiveIndex.updateEventEmitter]);
11
15
  const getSnapshot = useMemo(() => {
12
- let cachedResult = null;
13
- let cachedKeys = null;
14
16
  return () => {
15
- if (cachedKeys !== keys || !cachedResult) {
16
- cachedResult = keys.map((key) => reactiveIndex.getPksByKey(key));
17
- cachedKeys = keys;
18
- }
19
- return cachedResult;
17
+ return snapshotValueRef.current;
20
18
  };
21
- }, [keys, reactiveIndex]);
19
+ }, []);
22
20
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
23
21
  return snapshot;
24
22
  };
25
23
  var useSelectPksByIndexKey = (reactiveIndex, key) => {
24
+ const snapshotValueRef = useRef(
25
+ Array.from(reactiveIndex.getPksByKey(key))
26
+ );
26
27
  const subscribe = useMemo(() => {
27
- return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, onStoreChange);
28
+ return (onStoreChange) => reactiveIndex.updateEventEmitter.subscribeOnKey(key, () => {
29
+ snapshotValueRef.current = Array.from(
30
+ reactiveIndex.getPksByKey(key)
31
+ );
32
+ onStoreChange();
33
+ });
28
34
  }, [key, reactiveIndex.updateEventEmitter]);
29
- const getSnapshot = useMemo(
30
- () => () => reactiveIndex.getPksByKey(key),
31
- [key, reactiveIndex]
32
- );
35
+ const getSnapshot = useMemo(() => {
36
+ return () => {
37
+ return snapshotValueRef.current;
38
+ };
39
+ }, []);
33
40
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
34
41
  return snapshot;
35
42
  };
36
43
  var selectEntityByPk = (reactiveCollection, pk) => {
44
+ const snapshotValueRef = useRef(
45
+ reactiveCollection.getOneByPk(pk)
46
+ );
37
47
  const subscribe = useMemo(() => {
38
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(
39
- pk,
40
- onStoreChange
41
- );
48
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKey(pk, () => {
49
+ snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
50
+ onStoreChange();
51
+ });
42
52
  }, [pk, reactiveCollection.updateEventEmitter]);
43
- const getSnapshot = useMemo(
44
- () => () => reactiveCollection.getOneByPk(pk),
45
- [pk, reactiveCollection]
46
- );
53
+ const getSnapshot = useMemo(() => () => snapshotValueRef.current, []);
47
54
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
48
55
  return snapshot;
49
56
  };
50
57
  var useSelectEntitiesByPks = (reactiveCollection, pks) => {
58
+ const snapshotValueRef = useRef(
59
+ pks.map((pk) => reactiveCollection.getOneByPk(pk))
60
+ );
51
61
  const subscribe = useMemo(() => {
52
- return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(
53
- pks,
54
- onStoreChange
55
- );
62
+ return (onStoreChange) => reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, () => {
63
+ snapshotValueRef.current = pks.map(
64
+ (pk) => reactiveCollection.getOneByPk(pk)
65
+ );
66
+ onStoreChange();
67
+ });
56
68
  }, [pks, reactiveCollection.updateEventEmitter]);
57
69
  const getSnapshot = useMemo(() => {
58
- let cachedResult = null;
59
- let cachedPks = null;
60
70
  return () => {
61
- if (cachedPks !== pks || !cachedResult) {
62
- cachedResult = pks.map((pk) => reactiveCollection.getOneByPk(pk));
63
- cachedPks = pks;
64
- }
65
- return cachedResult;
71
+ return snapshotValueRef.current;
66
72
  };
67
- }, [pks, reactiveCollection]);
73
+ }, []);
68
74
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
69
75
  return snapshot;
70
76
  };
71
77
  var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
72
78
  const pks = useSelectPksByIndexKey(reactiveIndex, key);
73
- return useSelectEntitiesByPks(reactiveCollection, pks);
79
+ return useSelectEntitiesByPks(
80
+ reactiveCollection,
81
+ pks || EMPTY_ARRAY
82
+ );
74
83
  };
75
84
  var unique = (array) => {
76
85
  return Array.from(new Set(array));
77
86
  };
78
87
  var selectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
79
- const pks = useSelectPksByIndexKeys(reactiveIndex, keys);
80
- const pksArray = useMemo(() => {
81
- let cachedResult = null;
82
- let cachedPks = null;
83
- const getCurrentPksArray = () => {
84
- if (cachedPks !== pks || !cachedResult) {
85
- cachedResult = unique(Array.from(pks.values()).flat());
86
- cachedPks = pks;
87
- }
88
- return cachedResult;
89
- };
90
- return getCurrentPksArray();
91
- }, [pks]);
92
- return useSelectEntitiesByPks(reactiveCollection, pksArray);
88
+ const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
89
+ return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
93
90
  };
94
91
 
95
92
  // src/context/index.tsx
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oimdb/react",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
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",