| b69ab31 | | | 1 | import { Message } from '../types'; |
| b69ab31 | | | 2 | import { ReduxExtension } from './getReduxExtension'; |
| b69ab31 | | | 3 | |
| b69ab31 | | | 4 | // Original but incomplete type of the redux extension package |
| b69ab31 | | | 5 | type ConnectResponse = ReturnType<NonNullable<ReduxExtension>['connect']>; |
| b69ab31 | | | 6 | |
| b69ab31 | | | 7 | export type Connection = { |
| b69ab31 | | | 8 | /** Mark the connection as not initiated, so it can be initiated before using it. */ |
| b69ab31 | | | 9 | shouldInit?: boolean; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | /** Initiate the connection and add it to the extension connections. |
| b69ab31 | | | 12 | * Should only be executed once in the live time of the connection. |
| b69ab31 | | | 13 | */ |
| b69ab31 | | | 14 | init: ConnectResponse['init']; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | // FIXME https://github.com/reduxjs/redux-devtools/issues/1097 |
| b69ab31 | | | 17 | /** Add a subscription to the connection. |
| b69ab31 | | | 18 | * The provided listener will be executed when the user interacts with the extension |
| b69ab31 | | | 19 | * with actions like time traveling, importing a state or the likes. |
| b69ab31 | | | 20 | * |
| b69ab31 | | | 21 | * @param listener function to be executed when an action is submitted |
| b69ab31 | | | 22 | * @returns function to unsubscribe the applied listener |
| b69ab31 | | | 23 | */ |
| b69ab31 | | | 24 | subscribe: (listener: (message: Message) => void) => (() => void) | undefined; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | /** Send a new action to the connection to display the state change in the extension. |
| b69ab31 | | | 27 | * For example when the value of the store changes. |
| b69ab31 | | | 28 | */ |
| b69ab31 | | | 29 | send: ConnectResponse['send']; |
| b69ab31 | | | 30 | }; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | /** Wrapper for creating connections to the redux extension |
| b69ab31 | | | 33 | * Connections are used to display the stores value and value changes within the extension |
| b69ab31 | | | 34 | * as well as reacting to extension actions like time traveling. |
| b69ab31 | | | 35 | **/ |
| b69ab31 | | | 36 | export const createReduxConnection = ( |
| b69ab31 | | | 37 | extension: ReduxExtension | undefined, |
| b69ab31 | | | 38 | name: string, |
| b69ab31 | | | 39 | ) => { |
| b69ab31 | | | 40 | if (!extension) return undefined; |
| b69ab31 | | | 41 | const connection = extension.connect({ name }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | return Object.assign(connection, { |
| b69ab31 | | | 44 | shouldInit: true, |
| b69ab31 | | | 45 | }) as Connection; |
| b69ab31 | | | 46 | }; |