|
11 | 11 | * @module relationships |
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} from './common.d'; |
16 | 16 |
|
17 | 17 | /** |
@@ -39,6 +39,25 @@ export type Relationship = { |
39 | 39 | linkedRowIds: {[firstRowId: Id]: Ids}; |
40 | 40 | }; |
41 | 41 |
|
| 42 | +/** |
| 43 | + * The RelationshipCallback type describes a function that takes an |
| 44 | + * Relationship's Id and a callback to loop over each local Row within it. |
| 45 | + * |
| 46 | + * A RelationshipCallback is provided when using the forEachRelationship method, |
| 47 | + * so that you can do something based on every Relationship in the Relationships |
| 48 | + * object. See that method for specific examples. |
| 49 | + * |
| 50 | + * @param relationshipId The Id of the Relationship that the callback can |
| 51 | + * operate on. |
| 52 | + * @param forEachRow A function that will let you iterate over the local Row |
| 53 | + * objects in this Relationship. |
| 54 | + * @category Callback |
| 55 | + */ |
| 56 | +export type RelationshipCallback = ( |
| 57 | + relationshipId: Id, |
| 58 | + forEachRow: (rowCallback: RowCallback) => void, |
| 59 | +) => void; |
| 60 | + |
42 | 61 | /** |
43 | 62 | * The RemoteRowIdListener type describes a function that is used to listen to |
44 | 63 | * changes to the remote Row Id end of a Relationship. |
@@ -405,6 +424,49 @@ export interface Relationships { |
405 | 424 | */ |
406 | 425 | getRelationshipIds(): Ids; |
407 | 426 |
|
| 427 | + /** |
| 428 | + * The forEachRelationship method takes a function that it will then call for |
| 429 | + * each Relationship in a specified Relationships object. |
| 430 | + * |
| 431 | + * This method is useful for iterating over the structure of the Relationships |
| 432 | + * object in a functional style. The `relationshipCallback` parameter is a |
| 433 | + * RelationshipCallback function that will be called with the Id of each |
| 434 | + * Relationship, and with a function that can then be used to iterate over |
| 435 | + * each local Row involved in the Relationship. |
| 436 | + * |
| 437 | + * @param relationshipCallback The function that should be called for every |
| 438 | + * Relationship. |
| 439 | + * @example |
| 440 | + * This example iterates over each Relationship in a Relationships object, and |
| 441 | + * lists each Row Id within them. |
| 442 | + * |
| 443 | + * ```js |
| 444 | + * const store = createStore().setTable('pets', { |
| 445 | + * fido: {species: 'dog', next: 'felix'}, |
| 446 | + * felix: {species: 'cat', next: 'cujo'}, |
| 447 | + * cujo: {species: 'dog'}, |
| 448 | + * }); |
| 449 | + * const relationships = createRelationships(store) |
| 450 | + * .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species') |
| 451 | + * .setRelationshipDefinition('petSequence', 'pets', 'pets', 'next'); |
| 452 | + * |
| 453 | + * relationships.forEachRelationship((relationshipId, forEachRow) => { |
| 454 | + * console.log(relationshipId); |
| 455 | + * forEachRow((rowId) => console.log(`- ${rowId}`)); |
| 456 | + * }); |
| 457 | + * // -> 'petSpecies' |
| 458 | + * // -> '- fido' |
| 459 | + * // -> '- felix' |
| 460 | + * // -> '- cujo' |
| 461 | + * // -> 'petSequence' |
| 462 | + * // -> '- fido' |
| 463 | + * // -> '- felix' |
| 464 | + * // -> '- cujo' |
| 465 | + * ``` |
| 466 | + * @category Iterator |
| 467 | + */ |
| 468 | + forEachRelationship(relationshipCallback: RelationshipCallback): void; |
| 469 | + |
408 | 470 | /** |
409 | 471 | * The hasRelationship method returns a boolean indicating whether a given |
410 | 472 | * Relationship exists in the Relationships object. |
|
0 commit comments