Skip to content

Commit 337efe9

Browse files
committed
[hooks] Set callbacks can take parameterized Ids
1 parent 69c7030 commit 337efe9

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/ui-react/hooks.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
} from '../types/checkpoints';
6767
import {
6868
CheckpointsOrCheckpointsId,
69+
GetId,
6970
IndexesOrIndexesId,
7071
MetricsOrMetricsId,
7172
QueriesOrQueriesId,
@@ -209,7 +210,13 @@ import {
209210
ResultTableCellIdsListener,
210211
ResultTableListener,
211212
} from '../types/queries.d';
212-
import {getUndefined, ifNotUndefined, isUndefined} from '../common/other';
213+
import {arrayIsEmpty, arrayMap} from '../common/array';
214+
import {
215+
getUndefined,
216+
ifNotUndefined,
217+
isFunction,
218+
isUndefined,
219+
} from '../common/other';
213220
import {
214221
useCheckpointsOrCheckpointsById,
215222
useIndexesOrIndexesById,
@@ -223,7 +230,6 @@ import {ListenerArgument} from '../common/listeners';
223230
import {Persister} from '../types/persisters.d';
224231
import React from 'react';
225232
import {TRANSACTION} from '../tools/common/strings';
226-
import {arrayIsEmpty} from '../common/array';
227233

228234
export {
229235
useCheckpoints,
@@ -319,14 +325,22 @@ const useSetCallback = <Parameter, Thing>(
319325
getDeps: React.DependencyList = EMPTY_ARRAY,
320326
then: (store: Store, thing: Thing) => void = getUndefined,
321327
thenDeps: React.DependencyList = EMPTY_ARRAY,
322-
...args: Ids
328+
...args: (Id | GetId<Parameter>)[]
323329
): ParameterizedCallback<Parameter> => {
324330
const store = useStoreOrStoreById(storeOrStoreId);
325331
return useCallback(
326332
(parameter) =>
327333
ifNotUndefined(store, (store: any) =>
328334
ifNotUndefined(get(parameter as any, store), (thing: Thing) =>
329-
then(store['set' + settable](...args, thing), thing),
335+
then(
336+
store['set' + settable](
337+
...arrayMap(args, (arg) =>
338+
isFunction(arg) ? arg(parameter as any, store) : arg,
339+
),
340+
thing,
341+
),
342+
thing,
343+
),
330344
),
331345
),
332346
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -602,7 +616,7 @@ export const useSetTablesCallback: typeof useSetTablesCallbackDecl = <
602616
);
603617

604618
export const useSetTableCallback: typeof useSetTableCallbackDecl = <Parameter>(
605-
tableId: Id,
619+
tableId: Id | GetId<Parameter>,
606620
getTable: (parameter: Parameter, store: Store) => Table,
607621
getTableDeps?: React.DependencyList,
608622
storeOrStoreId?: StoreOrStoreId,
@@ -620,8 +634,8 @@ export const useSetTableCallback: typeof useSetTableCallbackDecl = <Parameter>(
620634
);
621635

622636
export const useSetRowCallback: typeof useSetRowCallbackDecl = <Parameter>(
623-
tableId: Id,
624-
rowId: Id,
637+
tableId: Id | GetId<Parameter>,
638+
rowId: Id | GetId<Parameter>,
625639
getRow: (parameter: Parameter, store: Store) => Row,
626640
getRowDeps?: React.DependencyList,
627641
storeOrStoreId?: StoreOrStoreId,
@@ -640,7 +654,7 @@ export const useSetRowCallback: typeof useSetRowCallbackDecl = <Parameter>(
640654
);
641655

642656
export const useAddRowCallback: typeof useAddRowCallbackDecl = <Parameter>(
643-
tableId: Id,
657+
tableId: Id | GetId<Parameter>,
644658
getRow: (parameter: Parameter, store: Store) => Row,
645659
getRowDeps: React.DependencyList = EMPTY_ARRAY,
646660
storeOrStoreId?: StoreOrStoreId,
@@ -653,7 +667,15 @@ export const useAddRowCallback: typeof useAddRowCallbackDecl = <Parameter>(
653667
(parameter) =>
654668
ifNotUndefined(store, (store) =>
655669
ifNotUndefined(getRow(parameter as any, store), (row: Row) =>
656-
then(store.addRow(tableId, row, reuseRowIds), store, row),
670+
then(
671+
store.addRow(
672+
isFunction(tableId) ? tableId(parameter as any, store) : tableId,
673+
row,
674+
reuseRowIds,
675+
),
676+
store,
677+
row,
678+
),
657679
),
658680
),
659681
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -664,8 +686,8 @@ export const useAddRowCallback: typeof useAddRowCallbackDecl = <Parameter>(
664686
export const useSetPartialRowCallback: typeof useSetPartialRowCallbackDecl = <
665687
Parameter,
666688
>(
667-
tableId: Id,
668-
rowId: Id,
689+
tableId: Id | GetId<Parameter>,
690+
rowId: Id | GetId<Parameter>,
669691
getPartialRow: (parameter: Parameter, store: Store) => Row,
670692
getPartialRowDeps?: React.DependencyList,
671693
storeOrStoreId?: StoreOrStoreId,
@@ -684,9 +706,9 @@ export const useSetPartialRowCallback: typeof useSetPartialRowCallbackDecl = <
684706
);
685707

686708
export const useSetCellCallback: typeof useSetCellCallbackDecl = <Parameter>(
687-
tableId: Id,
688-
rowId: Id,
689-
cellId: Id,
709+
tableId: Id | GetId<Parameter>,
710+
rowId: Id | GetId<Parameter>,
711+
cellId: Id | GetId<Parameter>,
690712
getCell: (parameter: Parameter, store: Store) => Cell | MapCell,
691713
getCellDeps?: React.DependencyList,
692714
storeOrStoreId?: StoreOrStoreId,
@@ -741,7 +763,7 @@ export const useSetPartialValuesCallback: typeof useSetPartialValuesCallbackDecl
741763
);
742764

743765
export const useSetValueCallback: typeof useSetValueCallbackDecl = <Parameter>(
744-
valueId: Id,
766+
valueId: Id | GetId<Parameter>,
745767
getValue: (parameter: Parameter, store: Store) => Value | MapValue,
746768
getValueDeps?: React.DependencyList,
747769
storeOrStoreId?: StoreOrStoreId,

0 commit comments

Comments
 (0)