|
| 1 | +/** |
| 2 | + * The middleware module of the TinyBase project provides the ability to |
| 3 | + * intercept and validate incoming writes to Store objects. |
| 4 | + * |
| 5 | + * The main entry point to this module is the createMiddleware function, which |
| 6 | + * returns a new Middleware object. From there, you can register hooks that are |
| 7 | + * called before data is written to the Store. |
| 8 | + * @packageDocumentation |
| 9 | + * @module middleware |
| 10 | + * @since v8.0.0 |
| 11 | + */ |
| 12 | +/// middleware |
| 13 | +/** |
| 14 | + * A Middleware object lets you intercept and validate writes to a Store. |
| 15 | + * |
| 16 | + * This is useful for enforcing business rules, data validation, or |
| 17 | + * transformation logic before data is persisted in the Store. |
| 18 | + * |
| 19 | + * Create a Middleware object easily with the createMiddleware function. |
| 20 | + * @example |
| 21 | + * This example shows a very simple lifecycle of a Middleware object: from |
| 22 | + * creation, to getting the Store reference, and then destroying it. |
| 23 | + * |
| 24 | + * ```js |
| 25 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 26 | + * |
| 27 | + * const store = createStore(); |
| 28 | + * const middleware = createMiddleware(store); |
| 29 | + * |
| 30 | + * console.log(middleware.getStore() == store); |
| 31 | + * // -> true |
| 32 | + * |
| 33 | + * middleware.destroy(); |
| 34 | + * ``` |
| 35 | + * @category Middleware |
| 36 | + * @since v8.0.0 |
| 37 | + */ |
| 38 | +/// Middleware |
| 39 | +{ |
| 40 | + /** |
| 41 | + * The getStore method returns a reference to the underlying Store that is |
| 42 | + * backing this Middleware object. |
| 43 | + * @returns A reference to the Store. |
| 44 | + * @example |
| 45 | + * This example creates a Middleware object against a newly-created Store and |
| 46 | + * then gets its reference in order to update its data. |
| 47 | + * |
| 48 | + * ```js |
| 49 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 50 | + * |
| 51 | + * const middleware = createMiddleware(createStore()); |
| 52 | + * console.log(middleware.getStore().getTables()); |
| 53 | + * // -> {} |
| 54 | + * middleware.destroy(); |
| 55 | + * ``` |
| 56 | + * @category Getter |
| 57 | + * @since v8.0.0 |
| 58 | + */ |
| 59 | + /// Middleware.getStore |
| 60 | + /** |
| 61 | + * The destroy method should be called when this Middleware object is no |
| 62 | + * longer used. It removes all hooks and listeners from the Store, and |
| 63 | + * unregisters the Middleware from the Store. |
| 64 | + * @example |
| 65 | + * This example creates a Middleware object against a newly-created Store and |
| 66 | + * then destroys it. |
| 67 | + * |
| 68 | + * ```js |
| 69 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 70 | + * |
| 71 | + * const store = createStore(); |
| 72 | + * const middleware = createMiddleware(store); |
| 73 | + * middleware.destroy(); |
| 74 | + * ``` |
| 75 | + * @category Lifecycle |
| 76 | + * @since v8.0.0 |
| 77 | + */ |
| 78 | + /// Middleware.destroy |
| 79 | +} |
| 80 | +/** |
| 81 | + * The createMiddleware function creates a Middleware object, and is the main |
| 82 | + * entry point into the middleware module. |
| 83 | + * |
| 84 | + * A given Store can only have one Middleware object associated with it. If you |
| 85 | + * call this function twice on the same Store, your second call will return a |
| 86 | + * reference to the Middleware object created by the first. |
| 87 | + * @param store The Store for which to register the Middleware. |
| 88 | + * @returns A reference to the new Middleware object. |
| 89 | + * @example |
| 90 | + * This example creates a Middleware object. |
| 91 | + * |
| 92 | + * ```js |
| 93 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 94 | + * |
| 95 | + * const store = createStore(); |
| 96 | + * const middleware = createMiddleware(store); |
| 97 | + * console.log(middleware.getStore() == store); |
| 98 | + * // -> true |
| 99 | + * middleware.destroy(); |
| 100 | + * ``` |
| 101 | + * @example |
| 102 | + * This example creates a Middleware object, and calls the method a second time |
| 103 | + * for the same Store to return the same object. |
| 104 | + * |
| 105 | + * ```js |
| 106 | + * import {createMiddleware, createStore} from 'tinybase'; |
| 107 | + * |
| 108 | + * const store = createStore(); |
| 109 | + * const middleware = createMiddleware(store); |
| 110 | + * console.log(middleware === createMiddleware(store)); |
| 111 | + * // -> true |
| 112 | + * middleware.destroy(); |
| 113 | + * ``` |
| 114 | + * @category Creation |
| 115 | + * @since v8.0.0 |
| 116 | + */ |
| 117 | +/// createMiddleware |
0 commit comments