@oimdb/react 1.0.7 → 1.1.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/README.md +17 -17
- package/dist/index.cjs +41 -9
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +38 -6
- package/package.json +2 -5
package/README.md
CHANGED
|
@@ -29,8 +29,8 @@ npm install @oimdb/react @oimdb/core
|
|
|
29
29
|
import { OIMEventQueue, OIMRICollection, OIMReactiveIndexManual } from '@oimdb/core';
|
|
30
30
|
import {
|
|
31
31
|
useSelectEntitiesByPks,
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
useSelectEntitiesByIndexKey,
|
|
33
|
+
useSelectEntityByPk
|
|
34
34
|
} from '@oimdb/react';
|
|
35
35
|
|
|
36
36
|
// Create event queue and reactive collections
|
|
@@ -46,7 +46,7 @@ const usersCollection = new OIMRICollection(queue, {
|
|
|
46
46
|
|
|
47
47
|
```typescript
|
|
48
48
|
function UserProfile({ userId }: { userId: string }) {
|
|
49
|
-
const user =
|
|
49
|
+
const user = useSelectEntityByPk(usersCollection, userId);
|
|
50
50
|
|
|
51
51
|
if (!user) return <div>Loading...</div>;
|
|
52
52
|
|
|
@@ -81,7 +81,7 @@ function UserList({ userIds }: { userIds: string[] }) {
|
|
|
81
81
|
|
|
82
82
|
```typescript
|
|
83
83
|
function TeamMembers({ teamId }: { teamId: string }) {
|
|
84
|
-
const teamUsers =
|
|
84
|
+
const teamUsers = useSelectEntitiesByIndexKey(
|
|
85
85
|
usersCollection,
|
|
86
86
|
usersCollection.indexes.byTeam,
|
|
87
87
|
teamId
|
|
@@ -165,7 +165,7 @@ function UserDashboard() {
|
|
|
165
165
|
|
|
166
166
|
// Use collections with hooks
|
|
167
167
|
const allUsers = useSelectEntitiesByPks(users, []);
|
|
168
|
-
const teamMembers =
|
|
168
|
+
const teamMembers = useSelectEntitiesByIndexKey(
|
|
169
169
|
users,
|
|
170
170
|
users.indexes.byTeam,
|
|
171
171
|
'team1'
|
|
@@ -205,7 +205,7 @@ function UserComponent() {
|
|
|
205
205
|
|
|
206
206
|
## API Reference
|
|
207
207
|
|
|
208
|
-
### `
|
|
208
|
+
### `useSelectEntityByPk(reactiveCollection, pk)`
|
|
209
209
|
|
|
210
210
|
Subscribes to a single entity from a reactive collection.
|
|
211
211
|
|
|
@@ -227,7 +227,7 @@ Subscribes to multiple entities from a reactive collection.
|
|
|
227
227
|
**Returns:**
|
|
228
228
|
- `(TEntity | undefined)[]` - Array of entities (undefined for missing entities)
|
|
229
229
|
|
|
230
|
-
### `
|
|
230
|
+
### `useSelectEntitiesByIndexKey(reactiveCollection, reactiveIndex, key)`
|
|
231
231
|
|
|
232
232
|
Subscribes to entities indexed by a specific key.
|
|
233
233
|
|
|
@@ -239,7 +239,7 @@ Subscribes to entities indexed by a specific key.
|
|
|
239
239
|
**Returns:**
|
|
240
240
|
- `(TEntity | undefined)[]` - Array of entities for the given index key
|
|
241
241
|
|
|
242
|
-
### `
|
|
242
|
+
### `useSelectEntitiesByIndexKeys(reactiveCollection, reactiveIndex, keys)`
|
|
243
243
|
|
|
244
244
|
Subscribes to entities indexed by multiple keys.
|
|
245
245
|
|
|
@@ -326,8 +326,8 @@ The hooks work directly with OIMDB reactive objects:
|
|
|
326
326
|
|
|
327
327
|
```typescript
|
|
328
328
|
// Use reactive collections and indexes directly
|
|
329
|
-
const user =
|
|
330
|
-
const posts =
|
|
329
|
+
const user = useSelectEntityByPk(reactiveCollection, 'user123');
|
|
330
|
+
const posts = useSelectEntitiesByIndexKey(reactiveCollection, reactiveIndex, 'tech');
|
|
331
331
|
```
|
|
332
332
|
|
|
333
333
|
### Event Subscription
|
|
@@ -354,7 +354,7 @@ Hooks automatically subscribe to OIMDB reactive events using `useSyncExternalSto
|
|
|
354
354
|
```typescript
|
|
355
355
|
import React from 'react';
|
|
356
356
|
import { OIMEventQueue, OIMRICollection, OIMReactiveIndexManual } from '@oimdb/core';
|
|
357
|
-
import {
|
|
357
|
+
import { useSelectEntityByPk, useSelectEntitiesByPks, useSelectEntitiesByIndexKey } from '@oimdb/react';
|
|
358
358
|
|
|
359
359
|
interface User {
|
|
360
360
|
id: string;
|
|
@@ -378,7 +378,7 @@ const usersCollection = createUserCollection();
|
|
|
378
378
|
|
|
379
379
|
// Component
|
|
380
380
|
function UserProfile({ userId }: { userId: string }) {
|
|
381
|
-
const user =
|
|
381
|
+
const user = useSelectEntityByPk(usersCollection, userId);
|
|
382
382
|
|
|
383
383
|
if (!user) return <div>Loading...</div>;
|
|
384
384
|
|
|
@@ -386,7 +386,7 @@ function UserProfile({ userId }: { userId: string }) {
|
|
|
386
386
|
}
|
|
387
387
|
|
|
388
388
|
function TeamDashboard({ teamId }: { teamId: string }) {
|
|
389
|
-
const teamMembers =
|
|
389
|
+
const teamMembers = useSelectEntitiesByIndexKey(
|
|
390
390
|
usersCollection,
|
|
391
391
|
usersCollection.indexes.byTeam,
|
|
392
392
|
teamId
|
|
@@ -445,9 +445,9 @@ const users = useEntities(userStorage, userIds);
|
|
|
445
445
|
const posts = useIndex(postStorage, categoryIndex, 'tech');
|
|
446
446
|
|
|
447
447
|
// v1.x - Reactive collections
|
|
448
|
-
const user =
|
|
448
|
+
const user = useSelectEntityByPk(reactiveCollection, 'user123');
|
|
449
449
|
const users = useSelectEntitiesByPks(reactiveCollection, userIds);
|
|
450
|
-
const posts =
|
|
450
|
+
const posts = useSelectEntitiesByIndexKey(reactiveCollection, reactiveIndex, 'tech');
|
|
451
451
|
```
|
|
452
452
|
|
|
453
453
|
### Collection Creation
|
|
@@ -464,7 +464,7 @@ const usersCollection = new OIMRICollection(queue, {
|
|
|
464
464
|
collectionOpts: { selectPk: (user: User) => user.id },
|
|
465
465
|
indexes: {},
|
|
466
466
|
});
|
|
467
|
-
const user =
|
|
467
|
+
const user = useSelectEntityByPk(usersCollection, userId);
|
|
468
468
|
```
|
|
469
469
|
|
|
470
470
|
### Context API
|
|
@@ -481,7 +481,7 @@ const collections = { users: usersCollection };
|
|
|
481
481
|
|
|
482
482
|
### Key Changes
|
|
483
483
|
|
|
484
|
-
- **Hook naming**: More explicit names like `
|
|
484
|
+
- **Hook naming**: More explicit names like `useSelectEntityByPk` vs `useEntity`
|
|
485
485
|
- **Parameters**: Direct reactive collection objects instead of storage abstractions
|
|
486
486
|
- **Context**: New context API for centralized collection management
|
|
487
487
|
- **Type safety**: Enhanced TypeScript support with better inference
|
package/dist/index.cjs
CHANGED
|
@@ -23,11 +23,11 @@ __export(index_exports, {
|
|
|
23
23
|
OIMRICollectionsContext: () => OIMRICollectionsContext,
|
|
24
24
|
OIMRICollectionsProvider: () => OIMRICollectionsProvider,
|
|
25
25
|
createOIMCollectionsContext: () => createOIMCollectionsContext,
|
|
26
|
-
selectEntitiesByIndexKey: () => selectEntitiesByIndexKey,
|
|
27
|
-
selectEntitiesByIndexKeys: () => selectEntitiesByIndexKeys,
|
|
28
|
-
selectEntityByPk: () => selectEntityByPk,
|
|
29
26
|
useOIMCollectionsContext: () => useOIMCollectionsContext,
|
|
27
|
+
useSelectEntitiesByIndexKey: () => useSelectEntitiesByIndexKey,
|
|
28
|
+
useSelectEntitiesByIndexKeys: () => useSelectEntitiesByIndexKeys,
|
|
30
29
|
useSelectEntitiesByPks: () => useSelectEntitiesByPks,
|
|
30
|
+
useSelectEntityByPk: () => useSelectEntityByPk,
|
|
31
31
|
useSelectPksByIndexKey: () => useSelectPksByIndexKey,
|
|
32
32
|
useSelectPksByIndexKeys: () => useSelectPksByIndexKeys
|
|
33
33
|
});
|
|
@@ -38,6 +38,7 @@ var import_react = require("react");
|
|
|
38
38
|
var import_react2 = require("react");
|
|
39
39
|
var EMPTY_ARRAY = [];
|
|
40
40
|
var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
41
|
+
const isInitialized = (0, import_react.useRef)(false);
|
|
41
42
|
const snapshotValueRef = (0, import_react.useRef)(
|
|
42
43
|
keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat()
|
|
43
44
|
);
|
|
@@ -56,6 +57,12 @@ var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
|
56
57
|
osc.current();
|
|
57
58
|
};
|
|
58
59
|
reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, list);
|
|
60
|
+
if (!isInitialized.current) {
|
|
61
|
+
isInitialized.current = true;
|
|
62
|
+
} else {
|
|
63
|
+
snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
|
|
64
|
+
osc.current();
|
|
65
|
+
}
|
|
59
66
|
return () => {
|
|
60
67
|
reactiveIndex.updateEventEmitter.unsubscribeFromKeys(keys, list);
|
|
61
68
|
};
|
|
@@ -73,6 +80,7 @@ var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
|
73
80
|
return snapshot;
|
|
74
81
|
};
|
|
75
82
|
var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
83
|
+
const isInitialized = (0, import_react.useRef)(false);
|
|
76
84
|
const snapshotValueRef = (0, import_react.useRef)(
|
|
77
85
|
Array.from(reactiveIndex.getPksByKey(key))
|
|
78
86
|
);
|
|
@@ -93,6 +101,14 @@ var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
|
93
101
|
osc.current();
|
|
94
102
|
};
|
|
95
103
|
reactiveIndex.updateEventEmitter.subscribeOnKey(key, list);
|
|
104
|
+
if (!isInitialized.current) {
|
|
105
|
+
isInitialized.current = true;
|
|
106
|
+
} else {
|
|
107
|
+
snapshotValueRef.current = Array.from(
|
|
108
|
+
reactiveIndex.getPksByKey(key)
|
|
109
|
+
);
|
|
110
|
+
osc.current();
|
|
111
|
+
}
|
|
96
112
|
return () => {
|
|
97
113
|
reactiveIndex.updateEventEmitter.unsubscribeFromKey(key, list);
|
|
98
114
|
};
|
|
@@ -109,7 +125,8 @@ var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
|
109
125
|
);
|
|
110
126
|
return snapshot;
|
|
111
127
|
};
|
|
112
|
-
var
|
|
128
|
+
var useSelectEntityByPk = (reactiveCollection, pk) => {
|
|
129
|
+
const isInitialized = (0, import_react.useRef)(false);
|
|
113
130
|
const snapshotValueRef = (0, import_react.useRef)(
|
|
114
131
|
reactiveCollection.getOneByPk(pk)
|
|
115
132
|
);
|
|
@@ -128,6 +145,12 @@ var selectEntityByPk = (reactiveCollection, pk) => {
|
|
|
128
145
|
osc.current();
|
|
129
146
|
};
|
|
130
147
|
reactiveCollection.updateEventEmitter.subscribeOnKey(pk, list);
|
|
148
|
+
if (!isInitialized.current) {
|
|
149
|
+
isInitialized.current = true;
|
|
150
|
+
} else {
|
|
151
|
+
snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
|
|
152
|
+
osc.current();
|
|
153
|
+
}
|
|
131
154
|
return () => {
|
|
132
155
|
reactiveCollection.updateEventEmitter.unsubscribeFromKey(pk, list);
|
|
133
156
|
};
|
|
@@ -141,6 +164,7 @@ var selectEntityByPk = (reactiveCollection, pk) => {
|
|
|
141
164
|
return snapshot;
|
|
142
165
|
};
|
|
143
166
|
var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
167
|
+
const isInitialized = (0, import_react.useRef)(false);
|
|
144
168
|
const snapshotValueRef = (0, import_react.useRef)(
|
|
145
169
|
pks.map((pk) => reactiveCollection.getOneByPk(pk))
|
|
146
170
|
);
|
|
@@ -161,6 +185,14 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
|
161
185
|
osc.current();
|
|
162
186
|
};
|
|
163
187
|
reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, list);
|
|
188
|
+
if (!isInitialized.current) {
|
|
189
|
+
isInitialized.current = true;
|
|
190
|
+
} else {
|
|
191
|
+
snapshotValueRef.current = pks.map(
|
|
192
|
+
(pk) => reactiveCollection.getOneByPk(pk)
|
|
193
|
+
);
|
|
194
|
+
osc.current();
|
|
195
|
+
}
|
|
164
196
|
return () => {
|
|
165
197
|
reactiveCollection.updateEventEmitter.unsubscribeFromKeys(
|
|
166
198
|
pks,
|
|
@@ -180,7 +212,7 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
|
180
212
|
);
|
|
181
213
|
return snapshot;
|
|
182
214
|
};
|
|
183
|
-
var
|
|
215
|
+
var useSelectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
|
|
184
216
|
const pks = useSelectPksByIndexKey(reactiveIndex, key);
|
|
185
217
|
return useSelectEntitiesByPks(
|
|
186
218
|
reactiveCollection,
|
|
@@ -190,7 +222,7 @@ var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
|
|
|
190
222
|
var unique = (array) => {
|
|
191
223
|
return Array.from(new Set(array));
|
|
192
224
|
};
|
|
193
|
-
var
|
|
225
|
+
var useSelectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
|
|
194
226
|
const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
|
|
195
227
|
return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
|
|
196
228
|
};
|
|
@@ -225,11 +257,11 @@ function useOIMCollectionsContext(context) {
|
|
|
225
257
|
OIMRICollectionsContext,
|
|
226
258
|
OIMRICollectionsProvider,
|
|
227
259
|
createOIMCollectionsContext,
|
|
228
|
-
selectEntitiesByIndexKey,
|
|
229
|
-
selectEntitiesByIndexKeys,
|
|
230
|
-
selectEntityByPk,
|
|
231
260
|
useOIMCollectionsContext,
|
|
261
|
+
useSelectEntitiesByIndexKey,
|
|
262
|
+
useSelectEntitiesByIndexKeys,
|
|
232
263
|
useSelectEntitiesByPks,
|
|
264
|
+
useSelectEntityByPk,
|
|
233
265
|
useSelectPksByIndexKey,
|
|
234
266
|
useSelectPksByIndexKeys
|
|
235
267
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -5,10 +5,10 @@ import { ReactNode } from 'react';
|
|
|
5
5
|
|
|
6
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
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
|
-
declare const
|
|
8
|
+
declare const useSelectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
|
|
9
9
|
declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly TEntity[] | null;
|
|
10
|
-
declare const
|
|
11
|
-
declare const
|
|
10
|
+
declare const useSelectEntitiesByIndexKey: <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 useSelectEntitiesByIndexKeys: <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>;
|
|
@@ -42,4 +42,4 @@ type StrictCollectionsDictionary<T> = {
|
|
|
42
42
|
[K in keyof T]: T[K] extends OIMRICollection<infer E, infer P, infer IN, infer IK, infer I, infer RI> ? OIMRICollection<E, P, IN, IK, I, RI> : never;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export { type AnyOIMRICollection, type CollectionsDictionary, type CreateCollectionsDictionary, type ExtractCollectionSignature, type ExtractEntityType, type ExtractIndexKeyType, type ExtractIndexNameType, type ExtractPkType, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, type StrictCollectionsDictionary, type TypedCollectionsDictionary, createOIMCollectionsContext,
|
|
45
|
+
export { type AnyOIMRICollection, type CollectionsDictionary, type CreateCollectionsDictionary, type ExtractCollectionSignature, type ExtractEntityType, type ExtractIndexKeyType, type ExtractIndexNameType, type ExtractPkType, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, type StrictCollectionsDictionary, type TypedCollectionsDictionary, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKey, useSelectEntitiesByIndexKeys, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKey, useSelectPksByIndexKeys };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,10 +5,10 @@ import { ReactNode } from 'react';
|
|
|
5
5
|
|
|
6
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
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
|
-
declare const
|
|
8
|
+
declare const useSelectEntityByPk: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pk: TPk) => TEntity | undefined;
|
|
9
9
|
declare const useSelectEntitiesByPks: <TEntity extends object, TPk extends TOIMPk>(reactiveCollection: OIMReactiveCollection<TEntity, TPk>, pks: readonly TPk[]) => readonly TEntity[] | null;
|
|
10
|
-
declare const
|
|
11
|
-
declare const
|
|
10
|
+
declare const useSelectEntitiesByIndexKey: <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 useSelectEntitiesByIndexKeys: <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>;
|
|
@@ -42,4 +42,4 @@ type StrictCollectionsDictionary<T> = {
|
|
|
42
42
|
[K in keyof T]: T[K] extends OIMRICollection<infer E, infer P, infer IN, infer IK, infer I, infer RI> ? OIMRICollection<E, P, IN, IK, I, RI> : never;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export { type AnyOIMRICollection, type CollectionsDictionary, type CreateCollectionsDictionary, type ExtractCollectionSignature, type ExtractEntityType, type ExtractIndexKeyType, type ExtractIndexNameType, type ExtractPkType, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, type StrictCollectionsDictionary, type TypedCollectionsDictionary, createOIMCollectionsContext,
|
|
45
|
+
export { type AnyOIMRICollection, type CollectionsDictionary, type CreateCollectionsDictionary, type ExtractCollectionSignature, type ExtractEntityType, type ExtractIndexKeyType, type ExtractIndexNameType, type ExtractPkType, type OIMContextValue, OIMRICollectionsContext, OIMRICollectionsProvider, type OIMRICollectionsProviderProps, type StrictCollectionsDictionary, type TypedCollectionsDictionary, createOIMCollectionsContext, useOIMCollectionsContext, useSelectEntitiesByIndexKey, useSelectEntitiesByIndexKeys, useSelectEntitiesByPks, useSelectEntityByPk, useSelectPksByIndexKey, useSelectPksByIndexKeys };
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { useMemo, useRef, useEffect } from "react";
|
|
|
3
3
|
import { useSyncExternalStore } from "react";
|
|
4
4
|
var EMPTY_ARRAY = [];
|
|
5
5
|
var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
6
|
+
const isInitialized = useRef(false);
|
|
6
7
|
const snapshotValueRef = useRef(
|
|
7
8
|
keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat()
|
|
8
9
|
);
|
|
@@ -21,6 +22,12 @@ var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
|
21
22
|
osc.current();
|
|
22
23
|
};
|
|
23
24
|
reactiveIndex.updateEventEmitter.subscribeOnKeys(keys, list);
|
|
25
|
+
if (!isInitialized.current) {
|
|
26
|
+
isInitialized.current = true;
|
|
27
|
+
} else {
|
|
28
|
+
snapshotValueRef.current = keys.map((key) => Array.from(reactiveIndex.getPksByKey(key))).flat();
|
|
29
|
+
osc.current();
|
|
30
|
+
}
|
|
24
31
|
return () => {
|
|
25
32
|
reactiveIndex.updateEventEmitter.unsubscribeFromKeys(keys, list);
|
|
26
33
|
};
|
|
@@ -38,6 +45,7 @@ var useSelectPksByIndexKeys = (reactiveIndex, keys) => {
|
|
|
38
45
|
return snapshot;
|
|
39
46
|
};
|
|
40
47
|
var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
48
|
+
const isInitialized = useRef(false);
|
|
41
49
|
const snapshotValueRef = useRef(
|
|
42
50
|
Array.from(reactiveIndex.getPksByKey(key))
|
|
43
51
|
);
|
|
@@ -58,6 +66,14 @@ var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
|
58
66
|
osc.current();
|
|
59
67
|
};
|
|
60
68
|
reactiveIndex.updateEventEmitter.subscribeOnKey(key, list);
|
|
69
|
+
if (!isInitialized.current) {
|
|
70
|
+
isInitialized.current = true;
|
|
71
|
+
} else {
|
|
72
|
+
snapshotValueRef.current = Array.from(
|
|
73
|
+
reactiveIndex.getPksByKey(key)
|
|
74
|
+
);
|
|
75
|
+
osc.current();
|
|
76
|
+
}
|
|
61
77
|
return () => {
|
|
62
78
|
reactiveIndex.updateEventEmitter.unsubscribeFromKey(key, list);
|
|
63
79
|
};
|
|
@@ -74,7 +90,8 @@ var useSelectPksByIndexKey = (reactiveIndex, key) => {
|
|
|
74
90
|
);
|
|
75
91
|
return snapshot;
|
|
76
92
|
};
|
|
77
|
-
var
|
|
93
|
+
var useSelectEntityByPk = (reactiveCollection, pk) => {
|
|
94
|
+
const isInitialized = useRef(false);
|
|
78
95
|
const snapshotValueRef = useRef(
|
|
79
96
|
reactiveCollection.getOneByPk(pk)
|
|
80
97
|
);
|
|
@@ -93,6 +110,12 @@ var selectEntityByPk = (reactiveCollection, pk) => {
|
|
|
93
110
|
osc.current();
|
|
94
111
|
};
|
|
95
112
|
reactiveCollection.updateEventEmitter.subscribeOnKey(pk, list);
|
|
113
|
+
if (!isInitialized.current) {
|
|
114
|
+
isInitialized.current = true;
|
|
115
|
+
} else {
|
|
116
|
+
snapshotValueRef.current = reactiveCollection.getOneByPk(pk);
|
|
117
|
+
osc.current();
|
|
118
|
+
}
|
|
96
119
|
return () => {
|
|
97
120
|
reactiveCollection.updateEventEmitter.unsubscribeFromKey(pk, list);
|
|
98
121
|
};
|
|
@@ -106,6 +129,7 @@ var selectEntityByPk = (reactiveCollection, pk) => {
|
|
|
106
129
|
return snapshot;
|
|
107
130
|
};
|
|
108
131
|
var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
132
|
+
const isInitialized = useRef(false);
|
|
109
133
|
const snapshotValueRef = useRef(
|
|
110
134
|
pks.map((pk) => reactiveCollection.getOneByPk(pk))
|
|
111
135
|
);
|
|
@@ -126,6 +150,14 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
|
126
150
|
osc.current();
|
|
127
151
|
};
|
|
128
152
|
reactiveCollection.updateEventEmitter.subscribeOnKeys(pks, list);
|
|
153
|
+
if (!isInitialized.current) {
|
|
154
|
+
isInitialized.current = true;
|
|
155
|
+
} else {
|
|
156
|
+
snapshotValueRef.current = pks.map(
|
|
157
|
+
(pk) => reactiveCollection.getOneByPk(pk)
|
|
158
|
+
);
|
|
159
|
+
osc.current();
|
|
160
|
+
}
|
|
129
161
|
return () => {
|
|
130
162
|
reactiveCollection.updateEventEmitter.unsubscribeFromKeys(
|
|
131
163
|
pks,
|
|
@@ -145,7 +177,7 @@ var useSelectEntitiesByPks = (reactiveCollection, pks) => {
|
|
|
145
177
|
);
|
|
146
178
|
return snapshot;
|
|
147
179
|
};
|
|
148
|
-
var
|
|
180
|
+
var useSelectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
|
|
149
181
|
const pks = useSelectPksByIndexKey(reactiveIndex, key);
|
|
150
182
|
return useSelectEntitiesByPks(
|
|
151
183
|
reactiveCollection,
|
|
@@ -155,7 +187,7 @@ var selectEntitiesByIndexKey = (reactiveCollection, reactiveIndex, key) => {
|
|
|
155
187
|
var unique = (array) => {
|
|
156
188
|
return Array.from(new Set(array));
|
|
157
189
|
};
|
|
158
|
-
var
|
|
190
|
+
var useSelectEntitiesByIndexKeys = (reactiveCollection, reactiveIndex, keys) => {
|
|
159
191
|
const pks = useSelectPksByIndexKeys(reactiveIndex, keys) || EMPTY_ARRAY;
|
|
160
192
|
return useSelectEntitiesByPks(reactiveCollection, unique(Array.from(pks)));
|
|
161
193
|
};
|
|
@@ -189,11 +221,11 @@ export {
|
|
|
189
221
|
OIMRICollectionsContext,
|
|
190
222
|
OIMRICollectionsProvider,
|
|
191
223
|
createOIMCollectionsContext,
|
|
192
|
-
selectEntitiesByIndexKey,
|
|
193
|
-
selectEntitiesByIndexKeys,
|
|
194
|
-
selectEntityByPk,
|
|
195
224
|
useOIMCollectionsContext,
|
|
225
|
+
useSelectEntitiesByIndexKey,
|
|
226
|
+
useSelectEntitiesByIndexKeys,
|
|
196
227
|
useSelectEntitiesByPks,
|
|
228
|
+
useSelectEntityByPk,
|
|
197
229
|
useSelectPksByIndexKey,
|
|
198
230
|
useSelectPksByIndexKeys
|
|
199
231
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oimdb/react",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -30,11 +30,8 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"@oimdb/core": "^1.0.0"
|
|
35
|
-
},
|
|
36
33
|
"peerDependencies": {
|
|
37
|
-
"@oimdb/core": "^1.0.
|
|
34
|
+
"@oimdb/core": "^1.0.6",
|
|
38
35
|
"react": "^18.0.0"
|
|
39
36
|
},
|
|
40
37
|
"devDependencies": {
|