Skip to content

Commit 3f1e10f

Browse files
committed
[hooks] useProvideStore
1 parent 09abc62 commit 3f1e10f

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

src/ui-react/components.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ import {
5555
useRelationshipsOrRelationshipsById,
5656
} from './context';
5757
import {Id, Ids} from '../types/common.d';
58-
import React, {ReactElement, useContext} from 'react';
58+
import React, {ReactElement} from 'react';
5959
import {
6060
createElement,
6161
getIndexStoreTableId,
6262
getProps,
6363
getRelationshipsStoreTableIds,
6464
} from './common';
6565
import {isArray, isUndefined} from '../common/other';
66+
import {objDel, objGet} from '../common/obj';
6667
import {
6768
useCell,
6869
useCellIds,
@@ -86,9 +87,10 @@ import {
8687
} from './hooks';
8788
import {CheckpointIds} from '../types/checkpoints';
8889
import {EMPTY_STRING} from '../common/strings';
90+
import {Store} from '../types/store';
8991
import {arrayMap} from '../common/array';
9092

91-
const {useMemo} = React;
93+
const {useCallback, useContext, useMemo, useState} = React;
9294

9395
const tableView = (
9496
{
@@ -236,12 +238,31 @@ export const Provider: typeof ProviderDecl = ({
236238
children,
237239
}: ProviderProps & {readonly children: React.ReactNode}): any => {
238240
const parentValue = useContext(Context);
241+
242+
const [extraStoresById, setExtraStoresById] = useState<{[id: Id]: Store}>({});
243+
const addExtraStore = useCallback(
244+
(id: Id, store: Store) =>
245+
setExtraStoresById((extraStoresById) =>
246+
objGet(extraStoresById, id) == store
247+
? extraStoresById
248+
: {...extraStoresById, [id]: store},
249+
),
250+
[],
251+
);
252+
const delExtraStore = useCallback(
253+
(id: Id) =>
254+
setExtraStoresById((extraStoresById) => ({
255+
...objDel(extraStoresById, id),
256+
})),
257+
[],
258+
);
259+
239260
return (
240261
<Context.Provider
241262
value={useMemo(
242263
() => [
243264
store ?? parentValue[0],
244-
{...parentValue[1], ...storesById},
265+
{...parentValue[1], ...storesById, ...extraStoresById},
245266
metrics ?? parentValue[2],
246267
{...parentValue[3], ...metricsById},
247268
indexes ?? parentValue[4],
@@ -252,10 +273,13 @@ export const Provider: typeof ProviderDecl = ({
252273
{...parentValue[9], ...queriesById},
253274
checkpoints ?? parentValue[10],
254275
{...parentValue[11], ...checkpointsById},
276+
addExtraStore,
277+
delExtraStore,
255278
],
256279
[
257280
store,
258281
storesById,
282+
extraStoresById,
259283
metrics,
260284
metricsById,
261285
indexes,
@@ -267,6 +291,8 @@ export const Provider: typeof ProviderDecl = ({
267291
checkpoints,
268292
checkpointsById,
269293
parentValue,
294+
addExtraStore,
295+
delExtraStore,
270296
],
271297
)}
272298
>

src/ui-react/context.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ import React from 'react';
2323
import {Relationships} from '../types/relationships';
2424
import {Store} from '../types/store';
2525

26-
const {createContext, useContext} = React;
26+
const {createContext, useContext, useEffect} = React;
2727

2828
type ContextValue = [
29-
Store?,
30-
{[storeId: Id]: Store}?,
31-
Metrics?,
32-
{[metricsId: Id]: Metrics}?,
33-
Indexes?,
34-
{[indexesId: Id]: Indexes}?,
35-
Relationships?,
36-
{[relationshipsId: Id]: Relationships}?,
37-
Queries?,
38-
{[queriesId: Id]: Queries}?,
39-
Checkpoints?,
40-
{[checkpointsId: Id]: Checkpoints}?,
29+
store?: Store,
30+
storesById?: {[storeId: Id]: Store},
31+
metrics?: Metrics,
32+
metricsById?: {[metricsId: Id]: Metrics},
33+
indexes?: Indexes,
34+
indexesById?: {[indexesId: Id]: Indexes},
35+
relationships?: Relationships,
36+
relationshipsById?: {[relationshipsId: Id]: Relationships},
37+
queries?: Queries,
38+
queriesById?: {[queriesId: Id]: Queries},
39+
checkpoints?: Checkpoints,
40+
checkpointsById?: {[checkpointsId: Id]: Checkpoints},
41+
addExtraStore?: (id: string, store: Store) => void,
42+
delExtraStore?: (id: string) => void,
4143
];
4244

4345
export const Context = createContext<ContextValue>([]);
@@ -92,6 +94,14 @@ export const useStoreOrStoreById = (
9294
storeOrStoreId?: StoreOrStoreId,
9395
): Store | undefined => useThingOrThingById(storeOrStoreId, 0);
9496

97+
export const useProvideStore = (storeId: Id, store: Store): void => {
98+
const {12: addExtraStore, 13: delExtraStore} = useContext(Context);
99+
useEffect(() => {
100+
addExtraStore?.(storeId, store);
101+
return () => delExtraStore?.(storeId);
102+
}, [addExtraStore, storeId, store, delExtraStore]);
103+
};
104+
95105
export const useMetrics: typeof useMetricsDecl = (
96106
id?: Id,
97107
): Metrics | undefined => useThing(id, 2);

src/ui-react/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export {
232232
useIndexesOrIndexesById,
233233
useMetrics,
234234
useMetricsOrMetricsById,
235+
useProvideStore,
235236
useQueries,
236237
useQueriesOrQueriesById,
237238
useRelationships,

0 commit comments

Comments
 (0)