Skip to content

Commit 4a8237b

Browse files
committed
[hooks] useSortedRowIdsListener object arg
1 parent 3fb3875 commit 4a8237b

File tree

10 files changed

+769
-499
lines changed

10 files changed

+769
-499
lines changed

docs/api/ui-react/functions/store-hooks/usesortedrowidslistener/article.html

Lines changed: 43 additions & 2 deletions
Large diffs are not rendered by default.

docs/api/ui-react/functions/store-hooks/usesortedrowidslistener/index.html

Lines changed: 43 additions & 2 deletions
Large diffs are not rendered by default.

src/@types/ui-react/docs.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,6 @@
15231523
* @since v2.0.0
15241524
*/
15251525
/// useSortedRowIds
1526-
15271526
/**
15281527
* When called with an object as the first argument, the useSortedRowIds method
15291528
* destructures it to make it easier to skip optional parameters.
@@ -1571,7 +1570,6 @@
15711570
* @since v6.1.0
15721571
*/
15731572
/// useSortedRowIds.2
1574-
15751573
/**
15761574
* The useHasRow hook returns a boolean indicating whether a given Row exists in
15771575
* the Store, and registers a listener so that any changes to that result will
@@ -4428,6 +4426,67 @@
44284426
* @since v2.0.0
44294427
*/
44304428
/// useSortedRowIdsListener
4429+
/**
4430+
* When called with an object as the first argument, the useSortedRowIds method
4431+
* destructures it to make it easier to skip optional parameters.
4432+
* @param args A SortedRowIdsArgs object containing the Id of the Table in the
4433+
* Store, and optional `cellId`, `descending`, `offset`, and `limit` parameters.
4434+
* @param listener The function that will be called whenever the sorted Row Ids
4435+
* in the Table change.
4436+
* @param listenerDeps An optional array of dependencies for the `listener`
4437+
* function, which, if any change, result in the re-registration of the
4438+
* listener. This parameter defaults to an empty array.
4439+
* @param mutator An optional boolean that indicates that the listener mutates
4440+
* Store data.
4441+
* @param storeOrStoreId The Store to register the listener with: omit for the
4442+
* default context Store, provide an Id for a named context Store, or provide an
4443+
* explicit reference.
4444+
* @example
4445+
* This example uses the useSortedRowIdsListener hook to create a listener that
4446+
* is scoped to a single component. When the component is unmounted, the
4447+
* listener is removed from the Store.
4448+
*
4449+
* ```jsx
4450+
* import React from 'react';
4451+
* import {createRoot} from 'react-dom/client';
4452+
* import {createStore} from 'tinybase';
4453+
* import {Provider, useSortedRowIdsListener} from 'tinybase/ui-react';
4454+
*
4455+
* const App = ({store}) => (
4456+
* <Provider store={store}>
4457+
* <Pane />
4458+
* </Provider>
4459+
* );
4460+
* const Pane = () => {
4461+
* useSortedRowIdsListener({tableId: 'pets', cellId: 'species'}, () =>
4462+
* console.log('Sorted Row Ids changed'),
4463+
* );
4464+
* return <span>App</span>;
4465+
* };
4466+
*
4467+
* const store = createStore().setTables({
4468+
* pets: {
4469+
* fido: {species: 'dog'},
4470+
* felix: {species: 'cat'},
4471+
* },
4472+
* });
4473+
* const app = document.createElement('div');
4474+
* const root = createRoot(app);
4475+
* root.render(<App store={store} />); // !act
4476+
* console.log(store.getListenerStats().sortedRowIds);
4477+
* // -> 1
4478+
*
4479+
* store.setRow('pets', 'cujo', {species: 'wolf'}); // !act
4480+
* // -> 'Sorted Row Ids changed'
4481+
*
4482+
* root.unmount(); // !act
4483+
* console.log(store.getListenerStats().sortedRowIds);
4484+
* // -> 0
4485+
* ```
4486+
* @category Store hooks
4487+
* @since v6.1.0
4488+
*/
4489+
/// useSortedRowIdsListener.2
44314490
/**
44324491
* The useHasRowListener hook registers a listener function with the Store that
44334492
* will be called when a Row is added to or removed from the Store.

src/@types/ui-react/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,15 @@ export function useSortedRowIdsListener(
489489
storeOrStoreId?: StoreOrStoreId,
490490
): void;
491491

492+
/// useSortedRowIdsListener.2
493+
export function useSortedRowIdsListener(
494+
args: SortedRowIdsArgs,
495+
listener: SortedRowIdsListener,
496+
listenerDeps?: React.DependencyList,
497+
mutator?: boolean,
498+
storeOrStoreId?: StoreOrStoreId,
499+
): void;
500+
492501
/// useHasRowListener
493502
export function useHasRowListener(
494503
tableId: IdOrNull,

src/@types/ui-react/with-schemas/index.d.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -623,24 +623,39 @@ export type WithSchemas<Schemas extends OptionalSchemas> = {
623623
storeOrStoreId?: StoreOrStoreId<Schemas>,
624624
) => void;
625625

626-
/// useSortedRowIdsListener
627-
useSortedRowIdsListener: <
628-
TableId extends TableIdFromSchema<Schemas[0]>,
629-
CellIdOrUndefined extends CellIdFromSchema<Schemas[0], TableId> | undefined,
630-
Descending extends boolean,
631-
Offset extends number,
632-
Limit extends number | undefined,
633-
>(
634-
tableId: TableId,
635-
cellId: CellIdOrUndefined,
636-
descending: Descending,
637-
offset: Offset,
638-
limit: Limit,
639-
listener: SortedRowIdsListener<Schemas, TableId, CellIdOrUndefined>,
640-
listenerDeps?: React.DependencyList,
641-
mutator?: boolean,
642-
storeOrStoreId?: StoreOrStoreId<Schemas>,
643-
) => void;
626+
useSortedRowIdsListener: {
627+
/// useSortedRowIdsListener
628+
<
629+
TableId extends TableIdFromSchema<Schemas[0]>,
630+
CellIdOrUndefined extends
631+
| CellIdFromSchema<Schemas[0], TableId>
632+
| undefined,
633+
>(
634+
tableId: TableId,
635+
cellId: CellIdOrUndefined,
636+
descending: boolean,
637+
offset: number,
638+
limit: number | undefined,
639+
listener: SortedRowIdsListener<Schemas, TableId, CellIdOrUndefined>,
640+
listenerDeps?: React.DependencyList,
641+
mutator?: boolean,
642+
storeOrStoreId?: StoreOrStoreId<Schemas>,
643+
): void;
644+
645+
/// useSortedRowIdsListener.2
646+
<
647+
TableId extends TableIdFromSchema<Schemas[0]>,
648+
CellIdOrUndefined extends
649+
| CellIdFromSchema<Schemas[0], TableId>
650+
| undefined,
651+
>(
652+
args: SortedRowIdsArgs<Schemas[0], TableId>,
653+
listener: SortedRowIdsListener<Schemas, TableId, CellIdOrUndefined>,
654+
listenerDeps?: React.DependencyList,
655+
mutator?: boolean,
656+
storeOrStoreId?: StoreOrStoreId<Schemas>,
657+
): void;
658+
};
644659

645660
/// useHasRowListener
646661
useHasRowListener: <

src/ui-react/hooks.ts

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,26 @@ const useSortedRowIdsImpl = (
496496
[tableId, cellId, descending, offset, limit],
497497
);
498498

499+
export const useSortedRowIdsListenerImpl = (
500+
tableId: Id,
501+
cellId: Id | undefined,
502+
descending: boolean,
503+
offset: number,
504+
limit: number | undefined,
505+
listener: SortedRowIdsListener,
506+
listenerDeps?: DependencyList,
507+
mutator?: boolean,
508+
storeOrStoreId?: StoreOrStoreId,
509+
): void =>
510+
useListener(
511+
SORTED_ROW_IDS,
512+
useStoreOrStoreById(storeOrStoreId),
513+
listener,
514+
listenerDeps,
515+
[tableId, cellId, descending, offset, limit],
516+
mutator,
517+
);
518+
499519
// ---
500520

501521
export const useCreateStore: typeof useCreateStoreDecl = (
@@ -1130,23 +1150,40 @@ export const useRowIdsListener: typeof useRowIdsListenerDecl = (
11301150
);
11311151

11321152
export const useSortedRowIdsListener: typeof useSortedRowIdsListenerDecl = (
1133-
tableId: Id,
1134-
cellId: Id | undefined,
1135-
descending: boolean,
1136-
offset: number,
1137-
limit: number | undefined,
1138-
listener: SortedRowIdsListener,
1153+
tableIdOrArgs: Id | SortedRowIdsArgs,
1154+
cellIdOrListener: Id | undefined | SortedRowIdsListener,
1155+
descendingOrListenerDeps: boolean | DependencyList | undefined,
1156+
offsetOrMutator: number | boolean | undefined,
1157+
limitOrStoreOrStoreId: number | undefined | StoreOrStoreId,
1158+
listener?: SortedRowIdsListener,
11391159
listenerDeps?: DependencyList,
11401160
mutator?: boolean,
11411161
storeOrStoreId?: StoreOrStoreId,
11421162
): void =>
1143-
useListener(
1144-
SORTED_ROW_IDS,
1145-
useStoreOrStoreById(storeOrStoreId),
1146-
listener,
1147-
listenerDeps,
1148-
[tableId, cellId, descending, offset, limit],
1149-
mutator,
1163+
(useSortedRowIdsListenerImpl as any)(
1164+
...(isObject(tableIdOrArgs)
1165+
? [
1166+
tableIdOrArgs.tableId,
1167+
tableIdOrArgs.cellId,
1168+
tableIdOrArgs.descending ?? false,
1169+
tableIdOrArgs.offset ?? 0,
1170+
tableIdOrArgs.limit,
1171+
cellIdOrListener,
1172+
descendingOrListenerDeps,
1173+
offsetOrMutator,
1174+
limitOrStoreOrStoreId,
1175+
]
1176+
: [
1177+
tableIdOrArgs,
1178+
cellIdOrListener,
1179+
descendingOrListenerDeps,
1180+
offsetOrMutator,
1181+
limitOrStoreOrStoreId,
1182+
listener,
1183+
listenerDeps,
1184+
mutator,
1185+
storeOrStoreId,
1186+
]),
11501187
);
11511188

11521189
export const useHasRowListener: typeof useHasRowListenerDecl = (

0 commit comments

Comments
 (0)