|
11 | 11 | * @module indexes |
12 | 12 | */ |
13 | 13 |
|
14 | | -import {GetCell, Store} from './store.d'; |
| 14 | +import {GetCell, RowCallback, Store} from './store.d'; |
15 | 15 | import {Id, IdOrNull, Ids, SortKey} from './common.d'; |
16 | 16 |
|
17 | 17 | /** |
@@ -43,6 +43,42 @@ export type Index = {[sliceId: Id]: Slice}; |
43 | 43 | */ |
44 | 44 | export type Slice = Ids; |
45 | 45 |
|
| 46 | +/** |
| 47 | + * The IndexCallback type describes a function that takes an Index's Id and a |
| 48 | + * callback to loop over each Slice within it. |
| 49 | + * |
| 50 | + * A IndexCallback is provided when using the forEachIndex method, so that you |
| 51 | + * can do something based on every Index in the Indexes object. See that method |
| 52 | + * for specific examples. |
| 53 | + * |
| 54 | + * @param indexId The Id of the Index that the callback can operate on. |
| 55 | + * @param forEachRow A function that will let you iterate over the Slice objects |
| 56 | + * in this Index. |
| 57 | + * @category Callback |
| 58 | + */ |
| 59 | +export type IndexCallback = ( |
| 60 | + indexId: Id, |
| 61 | + forEachSlice: (sliceCallback: SliceCallback) => void, |
| 62 | +) => void; |
| 63 | + |
| 64 | +/** |
| 65 | + * The SliceCallback type describes a function that takes a Slice's Id and a |
| 66 | + * callback to loop over each Row within it. |
| 67 | + * |
| 68 | + * A SliceCallback is provided when using the forEachSlice method, so that you |
| 69 | + * can do something based on every Slice in an Index. See that method for |
| 70 | + * specific examples. |
| 71 | + * |
| 72 | + * @param sliceId The Id of the Slice that the callback can operate on. |
| 73 | + * @param forEachRow A function that will let you iterate over the Row objects |
| 74 | + * in this Slice. |
| 75 | + * @category Callback |
| 76 | + */ |
| 77 | +export type SliceCallback = ( |
| 78 | + sliceId: Id, |
| 79 | + forEachRow: (rowCallback: RowCallback) => void, |
| 80 | +) => void; |
| 81 | + |
46 | 82 | /** |
47 | 83 | * The SliceIdsListener type describes a function that is used to listen to |
48 | 84 | * changes to the Slice Ids in an Index. |
@@ -362,6 +398,82 @@ export interface Indexes { |
362 | 398 | */ |
363 | 399 | getIndexIds(): Ids; |
364 | 400 |
|
| 401 | + /** |
| 402 | + * The forEachIndex method takes a function that it will then call for each |
| 403 | + * Index in a specified Indexes object. |
| 404 | + * |
| 405 | + * This method is useful for iterating over the structure of the Indexes |
| 406 | + * object in a functional style. The `indexCallback` parameter is a |
| 407 | + * IndexCallback function that will called with the Id of each Index, and with |
| 408 | + * a function that can then be used to iterate over each Slice of the Index, |
| 409 | + * should you wish. |
| 410 | + * |
| 411 | + * @param indexCallback The function that should be called for every Index. |
| 412 | + * @example |
| 413 | + * This example iterates over each Index in a Indexes object, and lists each |
| 414 | + * Slice Id within them. |
| 415 | + * |
| 416 | + * ```js |
| 417 | + * const store = createStore().setTable('pets', { |
| 418 | + * fido: {species: 'dog', color: 'brown'}, |
| 419 | + * felix: {species: 'cat', color: 'black'}, |
| 420 | + * cujo: {species: 'dog', color: 'black'}, |
| 421 | + * }); |
| 422 | + * const indexes = createIndexes(store) |
| 423 | + * .setIndexDefinition('bySpecies', 'pets', 'species') |
| 424 | + * .setIndexDefinition('byColor', 'pets', 'color'); |
| 425 | + * |
| 426 | + * indexes.forEachIndex((indexId, forEachSlice) => { |
| 427 | + * console.log(indexId); |
| 428 | + * forEachSlice((sliceId) => console.log(`- ${sliceId}`)); |
| 429 | + * }); |
| 430 | + * // -> 'bySpecies' |
| 431 | + * // -> '- dog' |
| 432 | + * // -> '- cat' |
| 433 | + * // -> 'byColor' |
| 434 | + * // -> '- brown' |
| 435 | + * // -> '- black' |
| 436 | + * ``` |
| 437 | + * @category Iterator |
| 438 | + */ |
| 439 | + forEachIndex(indexCallback: IndexCallback): void; |
| 440 | + |
| 441 | + /** |
| 442 | + * The forEachSlice method takes a function that it will then call for each |
| 443 | + * Slice in a specified Index. |
| 444 | + * |
| 445 | + * This method is useful for iterating over the Slice structure of the Index |
| 446 | + * in a functional style. The `rowCallback` parameter is a RowCallback |
| 447 | + * function that will called with the Id and value of each Row in the Slice. |
| 448 | + * |
| 449 | + * @param indexId The Id of the Index to iterate over. |
| 450 | + * @param sliceCallback The function that should be called for every Slice. |
| 451 | + * @example |
| 452 | + * This example iterates over each Row in a Slice, and lists its Id. |
| 453 | + * |
| 454 | + * ```js |
| 455 | + * const store = createStore().setTable('pets', { |
| 456 | + * fido: {species: 'dog'}, |
| 457 | + * felix: {species: 'cat'}, |
| 458 | + * cujo: {species: 'dog'}, |
| 459 | + * }); |
| 460 | + * const indexes = createIndexes(store); |
| 461 | + * indexes.setIndexDefinition('bySpecies', 'pets', 'species'); |
| 462 | + * |
| 463 | + * indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => { |
| 464 | + * console.log(sliceId); |
| 465 | + * forEachRow((rowId) => console.log(`- ${rowId}`)); |
| 466 | + * }); |
| 467 | + * // -> 'dog' |
| 468 | + * // -> '- fido' |
| 469 | + * // -> '- cujo' |
| 470 | + * // -> 'cat' |
| 471 | + * // -> '- felix' |
| 472 | + * ``` |
| 473 | + * @category Iterator |
| 474 | + */ |
| 475 | + forEachSlice(indexId: Id, sliceCallback: SliceCallback): void; |
| 476 | + |
365 | 477 | /** |
366 | 478 | * The hasIndex method returns a boolean indicating whether a given Index |
367 | 479 | * exists in the Indexes object. |
|
0 commit comments