Skip to content

Commit 3ca442c

Browse files
committed
[store] forEachTableCell
1 parent aa582b9 commit 3ca442c

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

src/store.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
StoreListenerStats,
3434
Table,
3535
TableCallback,
36+
TableCellCallback,
3637
Tables,
3738
TablesSchema,
3839
TransactionListener,
@@ -1329,6 +1330,11 @@ export const createStore: typeof createStoreDecl = (): Store => {
13291330
),
13301331
);
13311332

1333+
const forEachTableCell = (
1334+
tableId: Id,
1335+
tableCellCallback: TableCellCallback,
1336+
): void => mapForEach(mapGet(tableCellIds, id(tableId)), tableCellCallback);
1337+
13321338
const forEachRow = (tableId: Id, rowCallback: RowCallback): void =>
13331339
collForEach(mapGet(tablesMap, id(tableId)), (rowMap, rowId) =>
13341340
rowCallback(rowId, (cellCallback) => mapForEach(rowMap, cellCallback)),
@@ -1491,6 +1497,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
14911497
finishTransaction,
14921498

14931499
forEachTable,
1500+
forEachTableCell,
14941501
forEachRow,
14951502
forEachCell,
14961503
forEachValue,

src/types/docs/store.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@
289289
* @category Callback
290290
*/
291291
/// TableCallback
292+
/**
293+
* The TableCellCallback type describes a function that takes a Cell's Id and
294+
* the count of times it appears across a whole Table.
295+
*
296+
* A TableCellCallback is provided when using the forEachTableCell method, so
297+
* that you can do something based on every Cell used across a Table. See that
298+
* method for specific examples.
299+
*
300+
* @param cellId The Id of the Cell that the callback can operate on.
301+
* @param count The number of times this Cell is used across a whole Table.
302+
* @category Callback
303+
*/
304+
/// TableCellCallback
292305
/**
293306
* The RowCallback type describes a function that takes a Row's Id and a
294307
* callback to loop over each Cell within it.
@@ -3189,6 +3202,35 @@
31893202
* @category Iterator
31903203
*/
31913204
/// Store.forEachTable
3205+
/**
3206+
* The forEachTableCell method takes a function that it will then call for
3207+
* each Cell used across the whole Table.
3208+
*
3209+
* This method is useful for iterating over the Cell structure of the Table in
3210+
* a functional style. The `tableCellCallback` parameter is a
3211+
* TableCellCallback function that will be called with the Id of each Cell and
3212+
* the count of Rows in the Table in which it appears.
3213+
*
3214+
* @param tableId The Id of the Table containing the Cells to iterate over.
3215+
* @param tableCellCallback The function that should be called for every Cell
3216+
* Id used across the whole Table.
3217+
* @example
3218+
* This example iterates over each Cell Id used across the whole Table.
3219+
*
3220+
* ```js
3221+
* const store = createStore().setTables({
3222+
* pets: {fido: {species: 'dog'}, felix: {species: 'cat', legs: 4}},
3223+
* });
3224+
* store.forEachTableCell('pets', (cellId, count) => {
3225+
* console.log(`${cellId}: ${count}`);
3226+
* });
3227+
* // -> 'species: 2'
3228+
* // -> 'legs: 1'
3229+
* ```
3230+
* @category Iterator
3231+
* @since v3.3
3232+
*/
3233+
/// Store.forEachTableCell
31923234
/**
31933235
* The forEachRow method takes a function that it will then call for each Row
31943236
* in a specified Table.

src/types/store.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export type TableCallback = (
6868
forEachRow: (rowCallback: RowCallback) => void,
6969
) => void;
7070

71+
/// TableCellCallback
72+
export type TableCellCallback = (cellId: Id, count: number) => void;
73+
7174
/// RowCallback
7275
export type RowCallback = (
7376
rowId: Id,
@@ -456,6 +459,9 @@ export interface Store {
456459
/// Store.forEachTable
457460
forEachTable(tableCallback: TableCallback): void;
458461

462+
/// Store.forEachTableCell
463+
forEachTableCell(tableId: Id, tableCellCallback: TableCellCallback): void;
464+
459465
/// Store.forEachRow
460466
forEachRow(tableId: Id, rowCallback: RowCallback): void;
461467

src/types/with-schemas/store.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ export type TableCallback<
173173
Params1 extends any[] = Truncate<Params2>,
174174
> = ((...params: Params2) => void) | ((...params: Params1) => void);
175175

176+
/// TableCellCallback
177+
export type TableCellCallback<
178+
Schema extends OptionalTablesSchema,
179+
TableId extends TableIdFromSchema<Schema>,
180+
> = (cellId: CellIdFromSchema<Schema, TableId>, count: number) => void;
181+
176182
/// RowCallback
177183
export type RowCallback<
178184
Schema extends OptionalTablesSchema,
@@ -838,6 +844,12 @@ export interface Store<in out Schemas extends OptionalSchemas> {
838844
/// Store.forEachTable
839845
forEachTable(tableCallback: TableCallback<Schemas[0]>): void;
840846

847+
/// Store.forEachTableCell
848+
forEachTableCell<TableId extends TableIdFromSchema<Schemas[0]>>(
849+
tableId: TableId,
850+
tableCellCallback: TableCellCallback<Schemas[0], TableId>,
851+
): void;
852+
841853
/// Store.forEachRow
842854
forEachRow<TableId extends TableIdFromSchema<Schemas[0]>>(
843855
tableId: TableId,

test/unit/store-advanced.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ describe('Miscellaneous', () => {
389389
expect(row).toEqual({c1: 1, c2: 2});
390390
});
391391

392+
test('forEachTableCell', () => {
393+
store.setTables({t1: {r1: {c1: 1, c2: 2}, r2: {c2: 2, c3: 3}}});
394+
const cells: Row = {};
395+
store.forEachTableCell('t1', (cellId, count) => (cells[cellId] = count));
396+
expect(cells).toEqual({c1: 1, c2: 2, c3: 1});
397+
});
398+
392399
test('forEachValue', () => {
393400
store.setValues({v1: 1, v2: 2});
394401
const values: Values = {};

0 commit comments

Comments
 (0)