addons/isl/src/third-party/jotai-devtools/utils/redux-extension/createReduxConnection.tsblame
View source
b69ab311import { Message } from '../types';
b69ab312import { ReduxExtension } from './getReduxExtension';
b69ab313
b69ab314// Original but incomplete type of the redux extension package
b69ab315type ConnectResponse = ReturnType<NonNullable<ReduxExtension>['connect']>;
b69ab316
b69ab317export type Connection = {
b69ab318 /** Mark the connection as not initiated, so it can be initiated before using it. */
b69ab319 shouldInit?: boolean;
b69ab3110
b69ab3111 /** Initiate the connection and add it to the extension connections.
b69ab3112 * Should only be executed once in the live time of the connection.
b69ab3113 */
b69ab3114 init: ConnectResponse['init'];
b69ab3115
b69ab3116 // FIXME https://github.com/reduxjs/redux-devtools/issues/1097
b69ab3117 /** Add a subscription to the connection.
b69ab3118 * The provided listener will be executed when the user interacts with the extension
b69ab3119 * with actions like time traveling, importing a state or the likes.
b69ab3120 *
b69ab3121 * @param listener function to be executed when an action is submitted
b69ab3122 * @returns function to unsubscribe the applied listener
b69ab3123 */
b69ab3124 subscribe: (listener: (message: Message) => void) => (() => void) | undefined;
b69ab3125
b69ab3126 /** Send a new action to the connection to display the state change in the extension.
b69ab3127 * For example when the value of the store changes.
b69ab3128 */
b69ab3129 send: ConnectResponse['send'];
b69ab3130};
b69ab3131
b69ab3132/** Wrapper for creating connections to the redux extension
b69ab3133 * Connections are used to display the stores value and value changes within the extension
b69ab3134 * as well as reacting to extension actions like time traveling.
b69ab3135 **/
b69ab3136export const createReduxConnection = (
b69ab3137 extension: ReduxExtension | undefined,
b69ab3138 name: string,
b69ab3139) => {
b69ab3140 if (!extension) return undefined;
b69ab3141 const connection = extension.connect({ name });
b69ab3142
b69ab3143 return Object.assign(connection, {
b69ab3144 shouldInit: true,
b69ab3145 }) as Connection;
b69ab3146};