|
108 | 108 | * @since v8.0.0 |
109 | 109 | */ |
110 | 110 | /// WillSetValuesCallback |
| 111 | +/** |
| 112 | + * The WillSetTablesCallback type describes a function that is called before |
| 113 | + * Tables are set in the Store. |
| 114 | + * |
| 115 | + * The callback receives the Tables object that is about to be set. It can |
| 116 | + * return the Tables (possibly transformed) to allow the write, or `undefined` |
| 117 | + * to prevent the Tables from being set. |
| 118 | + * |
| 119 | + * Multiple WillSetTablesCallback functions can be registered and they will be |
| 120 | + * called sequentially, the Tables being updated successively. If any callback |
| 121 | + * returns `undefined`, the chain short-circuits and the Tables will not be |
| 122 | + * set. |
| 123 | + * @param tables The Tables object about to be set. |
| 124 | + * @returns The Tables to use (possibly transformed), or `undefined` to prevent |
| 125 | + * the write. |
| 126 | + * @category Callback |
| 127 | + * @since v8.0.0 |
| 128 | + */ |
| 129 | +/// WillSetTablesCallback |
111 | 130 | /** |
112 | 131 | * The WillDelCellCallback type describes a function that is called before a |
113 | 132 | * Cell is deleted from the Store. |
|
516 | 535 | * @since v8.0.0 |
517 | 536 | */ |
518 | 537 | /// Middleware.addWillSetValuesCallback |
| 538 | + /** |
| 539 | + * The addWillSetTablesCallback method registers a WillSetTablesCallback that |
| 540 | + * will be called before Tables are set in the Store. |
| 541 | + * |
| 542 | + * The callback can transform the Tables or return `undefined` to prevent the |
| 543 | + * write. Multiple callbacks can be registered and they are called |
| 544 | + * sequentially, each receiving the (possibly transformed) tables from the |
| 545 | + * previous callback. |
| 546 | + * @param callback The WillSetTablesCallback to register. |
| 547 | + * @returns A reference to the Middleware object, for chaining. |
| 548 | + * @example |
| 549 | + * This example registers a callback that upper-cases all string Cell values |
| 550 | + * when entire Tables are set in the pet store. |
| 551 | + * |
| 552 | + * ```js |
| 553 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 554 | + * |
| 555 | + * const store = createStore(); |
| 556 | + * const middleware = createMiddleware(store); |
| 557 | + * |
| 558 | + * middleware.addWillSetTablesCallback((tables) => |
| 559 | + * Object.fromEntries( |
| 560 | + * Object.entries(tables).map(([tableId, table]) => [ |
| 561 | + * tableId, |
| 562 | + * Object.fromEntries( |
| 563 | + * Object.entries(table).map(([rowId, row]) => [ |
| 564 | + * rowId, |
| 565 | + * Object.fromEntries( |
| 566 | + * Object.entries(row).map(([k, v]) => [ |
| 567 | + * k, |
| 568 | + * typeof v === 'string' ? v.toUpperCase() : v, |
| 569 | + * ]), |
| 570 | + * ), |
| 571 | + * ]), |
| 572 | + * ), |
| 573 | + * ]), |
| 574 | + * ), |
| 575 | + * ); |
| 576 | + * |
| 577 | + * store.setTables({pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}); |
| 578 | + * console.log(store.getTables()); |
| 579 | + * // -> {pets: {fido: {species: 'DOG'}, felix: {species: 'CAT'}}} |
| 580 | + * |
| 581 | + * middleware.destroy(); |
| 582 | + * ``` |
| 583 | + * @example |
| 584 | + * This example registers a callback that prevents setting any Tables that |
| 585 | + * include a 'banned' table. |
| 586 | + * |
| 587 | + * ```js |
| 588 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 589 | + * |
| 590 | + * const store = createStore(); |
| 591 | + * const middleware = createMiddleware(store); |
| 592 | + * |
| 593 | + * middleware.addWillSetTablesCallback((tables) => |
| 594 | + * 'banned' in tables ? undefined : tables, |
| 595 | + * ); |
| 596 | + * |
| 597 | + * store.setTables({banned: {r1: {c1: 1}}, pets: {fido: {species: 'dog'}}}); |
| 598 | + * console.log(store.getTables()); |
| 599 | + * // -> {} |
| 600 | + * |
| 601 | + * middleware.destroy(); |
| 602 | + * ``` |
| 603 | + * @category Configuration |
| 604 | + * @since v8.0.0 |
| 605 | + */ |
| 606 | + /// Middleware.addWillSetTablesCallback |
519 | 607 | /** |
520 | 608 | * The addWillDelCellCallback method registers a WillDelCellCallback that will |
521 | 609 | * be called before any Cell is deleted from the Store. |
|
0 commit comments