|
| 1 | +import { Action, ActionCreator, StoreEnhancer, compose } from 'redux' |
| 2 | + |
| 3 | +/** |
| 4 | + * @public |
| 5 | + */ |
| 6 | +export interface EnhancerOptions { |
| 7 | + /** |
| 8 | + * the instance name to be showed on the monitor page. Default value is `document.title`. |
| 9 | + * If not specified and there's no document title, it will consist of `tabId` and `instanceId`. |
| 10 | + */ |
| 11 | + name?: string |
| 12 | + /** |
| 13 | + * action creators functions to be available in the Dispatcher. |
| 14 | + */ |
| 15 | + actionCreators?: ActionCreator<any>[] | { [key: string]: ActionCreator<any> } |
| 16 | + /** |
| 17 | + * if more than one action is dispatched in the indicated interval, all new actions will be collected and sent at once. |
| 18 | + * It is the joint between performance and speed. When set to `0`, all actions will be sent instantly. |
| 19 | + * Set it to a higher value when experiencing perf issues (also `maxAge` to a lower value). |
| 20 | + * |
| 21 | + * @default 500 ms. |
| 22 | + */ |
| 23 | + latency?: number |
| 24 | + /** |
| 25 | + * (> 1) - maximum allowed actions to be stored in the history tree. The oldest actions are removed once maxAge is reached. It's critical for performance. |
| 26 | + * |
| 27 | + * @default 50 |
| 28 | + */ |
| 29 | + maxAge?: number |
| 30 | + /** |
| 31 | + * - `undefined` - will use regular `JSON.stringify` to send data (it's the fast mode). |
| 32 | + * - `false` - will handle also circular references. |
| 33 | + * - `true` - will handle also date, regex, undefined, error objects, symbols, maps, sets and functions. |
| 34 | + * - object, which contains `date`, `regex`, `undefined`, `error`, `symbol`, `map`, `set` and `function` keys. |
| 35 | + * For each of them you can indicate if to include (by setting as `true`). |
| 36 | + * For `function` key you can also specify a custom function which handles serialization. |
| 37 | + * See [`jsan`](https://github.com/kolodny/jsan) for more details. |
| 38 | + */ |
| 39 | + serialize?: |
| 40 | + | boolean |
| 41 | + | { |
| 42 | + date?: boolean |
| 43 | + regex?: boolean |
| 44 | + undefined?: boolean |
| 45 | + error?: boolean |
| 46 | + symbol?: boolean |
| 47 | + map?: boolean |
| 48 | + set?: boolean |
| 49 | + function?: boolean | Function |
| 50 | + } |
| 51 | + /** |
| 52 | + * function which takes `action` object and id number as arguments, and should return `action` object back. |
| 53 | + */ |
| 54 | + actionSanitizer?: <A extends Action>(action: A, id: number) => A |
| 55 | + /** |
| 56 | + * function which takes `state` object and index as arguments, and should return `state` object back. |
| 57 | + */ |
| 58 | + stateSanitizer?: <S>(state: S, index: number) => S |
| 59 | + /** |
| 60 | + * *string or array of strings as regex* - actions types to be hidden / shown in the monitors (while passed to the reducers). |
| 61 | + * If `actionsWhitelist` specified, `actionsBlacklist` is ignored. |
| 62 | + */ |
| 63 | + actionsBlacklist?: string | string[] |
| 64 | + /** |
| 65 | + * *string or array of strings as regex* - actions types to be hidden / shown in the monitors (while passed to the reducers). |
| 66 | + * If `actionsWhitelist` specified, `actionsBlacklist` is ignored. |
| 67 | + */ |
| 68 | + actionsWhitelist?: string | string[] |
| 69 | + /** |
| 70 | + * called for every action before sending, takes `state` and `action` object, and returns `true` in case it allows sending the current data to the monitor. |
| 71 | + * Use it as a more advanced version of `actionsBlacklist`/`actionsWhitelist` parameters. |
| 72 | + */ |
| 73 | + predicate?: <S, A extends Action>(state: S, action: A) => boolean |
| 74 | + /** |
| 75 | + * if specified as `false`, it will not record the changes till clicking on `Start recording` button. |
| 76 | + * Available only for Redux enhancer, for others use `autoPause`. |
| 77 | + * |
| 78 | + * @default true |
| 79 | + */ |
| 80 | + shouldRecordChanges?: boolean |
| 81 | + /** |
| 82 | + * if specified, whenever clicking on `Pause recording` button and there are actions in the history log, will add this action type. |
| 83 | + * If not specified, will commit when paused. Available only for Redux enhancer. |
| 84 | + * |
| 85 | + * @default "@@PAUSED"" |
| 86 | + */ |
| 87 | + pauseActionType?: string |
| 88 | + /** |
| 89 | + * auto pauses when the extension’s window is not opened, and so has zero impact on your app when not in use. |
| 90 | + * Not available for Redux enhancer (as it already does it but storing the data to be sent). |
| 91 | + * |
| 92 | + * @default false |
| 93 | + */ |
| 94 | + autoPause?: boolean |
| 95 | + /** |
| 96 | + * if specified as `true`, it will not allow any non-monitor actions to be dispatched till clicking on `Unlock changes` button. |
| 97 | + * Available only for Redux enhancer. |
| 98 | + * |
| 99 | + * @default false |
| 100 | + */ |
| 101 | + shouldStartLocked?: boolean |
| 102 | + /** |
| 103 | + * if set to `false`, will not recompute the states on hot reloading (or on replacing the reducers). Available only for Redux enhancer. |
| 104 | + * |
| 105 | + * @default true |
| 106 | + */ |
| 107 | + shouldHotReload?: boolean |
| 108 | + /** |
| 109 | + * if specified as `true`, whenever there's an exception in reducers, the monitors will show the error message, and next actions will not be dispatched. |
| 110 | + * |
| 111 | + * @default false |
| 112 | + */ |
| 113 | + shouldCatchErrors?: boolean |
| 114 | + /** |
| 115 | + * If you want to restrict the extension, specify the features you allow. |
| 116 | + * If not specified, all of the features are enabled. When set as an object, only those included as `true` will be allowed. |
| 117 | + * Note that except `true`/`false`, `import` and `export` can be set as `custom` (which is by default for Redux enhancer), meaning that the importing/exporting occurs on the client side. |
| 118 | + * Otherwise, you'll get/set the data right from the monitor part. |
| 119 | + */ |
| 120 | + features?: { |
| 121 | + /** |
| 122 | + * start/pause recording of dispatched actions |
| 123 | + */ |
| 124 | + pause?: boolean |
| 125 | + /** |
| 126 | + * lock/unlock dispatching actions and side effects |
| 127 | + */ |
| 128 | + lock?: boolean |
| 129 | + /** |
| 130 | + * persist states on page reloading |
| 131 | + */ |
| 132 | + persist?: boolean |
| 133 | + /** |
| 134 | + * export history of actions in a file |
| 135 | + */ |
| 136 | + export?: boolean | 'custom' |
| 137 | + /** |
| 138 | + * import history of actions from a file |
| 139 | + */ |
| 140 | + import?: boolean | 'custom' |
| 141 | + /** |
| 142 | + * jump back and forth (time travelling) |
| 143 | + */ |
| 144 | + jump?: boolean |
| 145 | + /** |
| 146 | + * skip (cancel) actions |
| 147 | + */ |
| 148 | + skip?: boolean |
| 149 | + /** |
| 150 | + * drag and drop actions in the history list |
| 151 | + */ |
| 152 | + reorder?: boolean |
| 153 | + /** |
| 154 | + * dispatch custom actions or action creators |
| 155 | + */ |
| 156 | + dispatch?: boolean |
| 157 | + /** |
| 158 | + * generate tests for the selected actions |
| 159 | + */ |
| 160 | + test?: boolean |
| 161 | + } |
| 162 | + /** |
| 163 | + * Set to true or a stacktrace-returning function to record call stack traces for dispatched actions. |
| 164 | + * Defaults to false. |
| 165 | + */ |
| 166 | + trace?: boolean | (<A extends Action>(action: A) => string) |
| 167 | + /** |
| 168 | + * The maximum number of stack trace entries to record per action. Defaults to 10. |
| 169 | + */ |
| 170 | + traceLimit?: number |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * @public |
| 175 | + */ |
| 176 | +export const composeWithDevTools: { |
| 177 | + (options: EnhancerOptions): typeof compose |
| 178 | + <StoreExt>(...funcs: Array<StoreEnhancer<StoreExt>>): StoreEnhancer<StoreExt> |
| 179 | +} = |
| 180 | + typeof window !== 'undefined' && |
| 181 | + (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ |
| 182 | + ? (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ |
| 183 | + : function() { |
| 184 | + if (arguments.length === 0) return undefined |
| 185 | + if (typeof arguments[0] === 'object') return compose |
| 186 | + return compose.apply(null, (arguments as any) as Function[]) |
| 187 | + } |
| 188 | + |
| 189 | +/** |
| 190 | + * @public |
| 191 | + */ |
| 192 | +export const devToolsEnhancer: { |
| 193 | + (options: EnhancerOptions): StoreEnhancer<any> |
| 194 | +} = |
| 195 | + typeof window !== 'undefined' && (window as any).__REDUX_DEVTOOLS_EXTENSION__ |
| 196 | + ? (window as any).__REDUX_DEVTOOLS_EXTENSION__ |
| 197 | + : function() { |
| 198 | + return function(noop) { |
| 199 | + return noop |
| 200 | + } |
| 201 | + } |
0 commit comments