Skip to content

Commit 4e67023

Browse files
committed
[store] TableCell tools
1 parent 1e766a8 commit 4e67023

File tree

7 files changed

+481
-14
lines changed

7 files changed

+481
-14
lines changed

src/tools/api/core.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export type TableTypes = [
9797
cellIdType: string,
9898
cellCallbackType: string,
9999
rowCallbackType: string,
100+
tableCellCallbackType: string,
100101
];
101102
export type SharedTableTypes = [
102103
tablesType: string,
@@ -106,6 +107,7 @@ export type SharedTableTypes = [
106107
tablesListenerType: string,
107108
tableIdsListenerType: string,
108109
tableListenerType: string,
110+
tableCellIdsListenerType: string,
109111
rowIdsListenerType: string,
110112
sortedRowIdsListenerType: string,
111113
rowListenerType: string,
@@ -290,10 +292,12 @@ export const getStoreCoreApi = (
290292
_cellType,
291293
cellCallbackType,
292294
rowCallbackType,
295+
tableCellCallbackType,
293296
tableCallbackType,
294297
tablesListenerType,
295298
tableIdsListenerType,
296299
tableListenerType,
300+
tableCellIdsListenerType,
297301
rowIdsListenerType,
298302
sortedRowIdsListenerType,
299303
rowListenerType,
@@ -359,6 +363,16 @@ export const getStoreCoreApi = (
359363
`a Row Id from the '${tableId}' Table, and a Cell iterator`,
360364
),
361365
),
366+
367+
// TableCellCallbackType
368+
addType(
369+
tableName + TABLE + CELL + CALLBACK,
370+
tableCellCallbackType + tableIdGeneric,
371+
getCallbackDoc(
372+
`a Cell Id from anywhere in the '${tableId}' Table, and a count ` +
373+
'of how many times it appears',
374+
),
375+
),
362376
];
363377
mapSet(tablesTypes, tableId, tableTypes);
364378
addImport(1, moduleDefinition, ...tableTypes);
@@ -375,6 +389,7 @@ export const getStoreCoreApi = (
375389
tablesListenerType,
376390
tableIdsListenerType,
377391
tableListenerType,
392+
tableCellIdsListenerType,
378393
rowIdsListenerType,
379394
sortedRowIdsListenerType,
380395
rowListenerType,
@@ -391,6 +406,7 @@ export const getStoreCoreApi = (
391406
tablesListenerType,
392407
tableIdsListenerType,
393408
tableListenerType,
409+
tableCellIdsListenerType,
394410
rowIdsListenerType,
395411
sortedRowIdsListenerType,
396412
rowListenerType,
@@ -448,6 +464,7 @@ export const getStoreCoreApi = (
448464
cellIdType,
449465
cellCallbackType,
450466
rowCallbackType,
467+
tableCellCallbackType,
451468
] = mapGet(tablesTypes, tableId) as TableTypes;
452469

453470
// getTable, hasTable, setTable, delTable
@@ -470,6 +487,28 @@ export const getStoreCoreApi = (
470487
),
471488
);
472489

490+
// getTableCellIds
491+
addProxyMethod(
492+
0,
493+
tableName,
494+
TABLE + CELL_IDS,
495+
IDS,
496+
getIdsDoc(CELL, 'the whole of ' + getTableDoc(tableId)),
497+
EMPTY_STRING,
498+
TABLE_ID,
499+
);
500+
501+
// forEachTableCell
502+
addProxyMethod(
503+
5,
504+
tableName,
505+
TABLE + CELL,
506+
VOID,
507+
getForEachDoc(TABLE + CELL, 'the whole of ' + getTableDoc(tableId)),
508+
'tableCellCallback: ' + tableCellCallbackType,
509+
TABLE_ID + ', tableCellCallback as any',
510+
);
511+
473512
// getRowIds
474513
addProxyMethod(
475514
0,
@@ -591,6 +630,19 @@ export const getStoreCoreApi = (
591630
TABLE_ID + ', rowId, ' + CELL_ID + paramsInCall,
592631
),
593632
);
633+
634+
// hasTableCell
635+
addProxyMethod(
636+
1,
637+
tableName + cellName,
638+
TABLE + CELL,
639+
BOOLEAN,
640+
VERBS[1] +
641+
` the '${cellId}' Cell anywhere in ` +
642+
getTableDoc(tableId),
643+
EMPTY_STRING,
644+
TABLE_ID + ', ' + CELL_ID,
645+
);
594646
},
595647
);
596648
});
@@ -634,6 +686,15 @@ export const getStoreCoreApi = (
634686
'tableId',
635687
);
636688

689+
// addTableCellIdsListener
690+
addProxyListener(
691+
TABLE + CELL_IDS,
692+
tableCellIdsListenerType,
693+
getListenerDoc(14, 3, 1),
694+
`tableId: ${tableIdType} | null`,
695+
'tableId',
696+
);
697+
637698
// addRowIdsListener
638699
addProxyListener(
639700
ROW_IDS,

src/tools/api/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ export const getTypeFunctions = (
174174
tIdGeneric,
175175
);
176176

177+
const tableCellCallbackType = addType(
178+
TABLE + CELL + CALLBACK,
179+
`(cellId: ${cellIdType}<TId>, count: number) ` + RETURNS_VOID,
180+
getCallbackDoc(A + CELL + ' Id, and count of how many times it appears'),
181+
tIdGeneric,
182+
);
183+
177184
const tableIdForEachRowArrayType = addType(
178185
'TableIdForEachRowArray',
179186
`TId extends ${tableIdType} ? [tableId: TId, forEachRow: ` +
@@ -232,6 +239,12 @@ export const getTypeFunctions = (
232239
getListenerTypeDoc(3),
233240
);
234241

242+
const tableCellIdsListenerType = addType(
243+
TABLE + CELL_IDS + LISTENER,
244+
`(${storeParam}, tableId: ${tableIdType})` + RETURNS_VOID,
245+
getListenerTypeDoc(14, 3),
246+
);
247+
235248
const rowIdsListenerType = addType(
236249
ROW_IDS + LISTENER,
237250
`(${storeParam}, tableId: ${tableIdType})` + RETURNS_VOID,
@@ -333,10 +346,12 @@ export const getTypeFunctions = (
333346
cellType,
334347
cellCallbackType,
335348
rowCallbackType,
349+
tableCellCallbackType,
336350
tableCallbackType,
337351
tablesListenerType,
338352
tableIdsListenerType,
339353
tableListenerType,
354+
tableCellIdsListenerType,
340355
rowIdsListenerType,
341356
sortedRowIdsListenerType,
342357
rowListenerType,

src/tools/api/ui-react.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ID,
1010
LISTENER_,
1111
OPTIONAL_COLON,
12+
OR_UNDEFINED,
1213
PARTIAL,
1314
PROPS,
1415
PROVIDER,
@@ -63,7 +64,6 @@ import {SharedTableTypes, SharedValueTypes, TableTypes} from './core';
6364
import {TablesSchema, ValuesSchema} from '../../types/store.d';
6465
import {arrayPush, arrayUnshift} from '../../common/array';
6566
import {Id} from '../../types/common.d';
66-
import {OR_UNDEFINED} from '../common/strings';
6767
import {getSchemaFunctions} from '../common/schema';
6868
import {isUndefined} from '../../common/other';
6969
import {objIsEmpty} from '../../common/obj';
@@ -358,6 +358,7 @@ export const getStoreUiReactApi = (
358358
tablesListenerType,
359359
tableIdsListenerType,
360360
tableListenerType,
361+
tableCellIdsListenerType,
361362
rowIdsListenerType,
362363
sortedRowIdsListenerType,
363364
rowListenerType,
@@ -375,6 +376,7 @@ export const getStoreUiReactApi = (
375376
tablesListenerType,
376377
tableIdsListenerType,
377378
tableListenerType,
379+
tableCellIdsListenerType,
378380
rowIdsListenerType,
379381
sortedRowIdsListenerType,
380382
rowListenerType,
@@ -632,6 +634,16 @@ export const getStoreUiReactApi = (
632634
TABLE_ID,
633635
);
634636

637+
// useTableCellIds
638+
addProxyHook(
639+
tableName + TABLE + CELL_IDS,
640+
TABLE + CELL_IDS,
641+
IDS,
642+
getIdsDoc(CELL, 'the whole of ' + getTableDoc(tableId)) + AND_REGISTERS,
643+
EMPTY_STRING,
644+
TABLE_ID,
645+
);
646+
635647
// useRowIds
636648
const useRowIds = addProxyHook(
637649
tableName + ROW_IDS,
@@ -948,6 +960,19 @@ export const getStoreUiReactApi = (
948960
getListenerHookParamsInCall('tableId'),
949961
);
950962

963+
// useTableCellIdsListener
964+
addProxyHook(
965+
TABLE + CELL_IDS + LISTENER,
966+
TABLE + CELL_IDS + LISTENER,
967+
VOID,
968+
getListenerDoc(14, 3, 1),
969+
getListenerHookParams(
970+
tableCellIdsListenerType,
971+
`tableId: ${tableIdType} | null`,
972+
),
973+
getListenerHookParamsInCall('tableId'),
974+
);
975+
951976
// useRowIdsListener
952977
addProxyHook(
953978
ROW_IDS + LISTENER,

src/tools/common/code.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ export const getCodeFunctions = (): [
157157
const addConstant = (name: Id, body: LINE_OR_LINE_TREE): Id =>
158158
mapGet(constants, name) === body ? name : mapUnique(constants, name, body);
159159

160+
const getSortableImport = (importMaybeAs: string): string => {
161+
const as = importMaybeAs.indexOf(' as ');
162+
return as != -1 ? importMaybeAs.substring(as + 4) : importMaybeAs;
163+
};
164+
160165
const getImports = (location: 0 | 1 = 0): LINE[] =>
161166
arrayMap(
162167
[
@@ -165,7 +170,11 @@ export const getCodeFunctions = (): [
165170
allImports[location],
166171
(items, source) =>
167172
`import {${join(
168-
arraySort(collValues(items)),
173+
arraySort(collValues(items), (import1, import2) =>
174+
getSortableImport(import1) > getSortableImport(import2)
175+
? 1
176+
: -1,
177+
),
169178
', ',
170179
)}} from '${source}';`,
171180
),

src/tools/common/strings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const NOUNS = [
187187
A + VALUE,
188188
'invalid Value changes',
189189
THE + 'sorted ' + ROW + SPACE + IDS,
190+
THE + CELL + SPACE + IDS + ' anywhere',
190191
];
191192

192193
const CONTENT = [EMPTY_STRING, 'tabular ', 'keyed value '];

src/types/docs/tools.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@
425425
* // -> `export type Tables = {pets?: {[rowId: Id]: {price?: number}}};`
426426
*
427427
* const tsLines = ts.split('\n');
428-
* console.log(tsLines[79]);
428+
* console.log(tsLines[81]);
429429
* // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
430430
* ```
431431
* @example
@@ -446,7 +446,7 @@
446446
* // -> 'export type Tables = {pets?: {[rowId: Id]: {price: number}}};'
447447
*
448448
* const tsLines = ts.split('\n');
449-
* console.log(tsLines[81]);
449+
* console.log(tsLines[83]);
450450
* // -> ' hasPetsTable: (): boolean => store.hasTable(PETS),'
451451
* ```
452452
* @category Modelling

0 commit comments

Comments
 (0)