2021-11-06 17:38:27 +03:00
|
|
|
import assign from './utils/assign';
|
2023-01-16 16:15:34 +03:00
|
|
|
import { compose } from 'redux';
|
|
|
|
import type {
|
2021-11-06 17:38:27 +03:00
|
|
|
Action,
|
|
|
|
Dispatch,
|
|
|
|
PreloadedState,
|
|
|
|
Reducer,
|
|
|
|
StoreEnhancer,
|
|
|
|
} from 'redux';
|
2023-01-16 16:15:34 +03:00
|
|
|
import type { Config, EnhancerOptions, InferComposedStoreExt } from './index';
|
2021-11-06 17:38:27 +03:00
|
|
|
|
|
|
|
function enhancer(options?: EnhancerOptions): StoreEnhancer {
|
|
|
|
const config: Config = options || {};
|
|
|
|
config.features = { pause: true, export: true, test: true };
|
|
|
|
config.type = 'redux';
|
|
|
|
if (config.autoPause === undefined) config.autoPause = true;
|
|
|
|
if (config.latency === undefined) config.latency = 500;
|
|
|
|
|
|
|
|
return function (createStore) {
|
|
|
|
return function <S, A extends Action<unknown>>(
|
|
|
|
reducer: Reducer<S, A>,
|
2023-07-12 21:03:20 +03:00
|
|
|
preloadedState: PreloadedState<S> | undefined,
|
2021-11-06 17:38:27 +03:00
|
|
|
) {
|
|
|
|
const store = createStore(reducer, preloadedState);
|
|
|
|
const origDispatch = store.dispatch;
|
|
|
|
|
|
|
|
const devTools = window.__REDUX_DEVTOOLS_EXTENSION__!.connect(config);
|
|
|
|
devTools.init(store.getState());
|
|
|
|
|
|
|
|
const dispatch: Dispatch<A> = function (action) {
|
|
|
|
const r = origDispatch(action);
|
|
|
|
devTools.send(action, store.getState());
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.assign) return Object.assign(store, { dispatch: dispatch });
|
|
|
|
return assign(store, 'dispatch', dispatch);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function composeWithEnhancer(config?: EnhancerOptions) {
|
2023-01-16 16:15:34 +03:00
|
|
|
return function (...funcs: StoreEnhancer<unknown>[]) {
|
2021-11-06 17:38:27 +03:00
|
|
|
return compose(compose(...funcs), enhancer(config));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function composeWithDevTools(
|
2023-07-12 21:03:20 +03:00
|
|
|
config: Config,
|
2023-01-16 16:15:34 +03:00
|
|
|
): <StoreEnhancers extends readonly StoreEnhancer<unknown>[]>(
|
|
|
|
...funcs: StoreEnhancers
|
|
|
|
) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
|
|
|
|
export function composeWithDevTools<
|
2023-07-12 21:03:20 +03:00
|
|
|
StoreEnhancers extends readonly StoreEnhancer<unknown>[],
|
2023-01-16 16:15:34 +03:00
|
|
|
>(
|
|
|
|
...funcs: StoreEnhancers
|
|
|
|
): StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
|
|
|
|
export function composeWithDevTools(
|
|
|
|
...funcs: [Config] | StoreEnhancer<unknown>[]
|
|
|
|
) {
|
2021-11-06 17:38:27 +03:00
|
|
|
if (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__) {
|
|
|
|
if (funcs.length === 0) return enhancer();
|
|
|
|
if (typeof funcs[0] === 'object') return composeWithEnhancer(funcs[0]);
|
2023-01-16 16:15:34 +03:00
|
|
|
return composeWithEnhancer()(...(funcs as StoreEnhancer<unknown>[]));
|
2021-11-06 17:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (funcs.length === 0) return undefined;
|
|
|
|
if (typeof funcs[0] === 'object') return compose;
|
|
|
|
return compose(...(funcs as StoreEnhancer[]));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const devToolsEnhancer: (options?: EnhancerOptions) => StoreEnhancer =
|
|
|
|
typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__
|
|
|
|
? enhancer
|
|
|
|
: function () {
|
|
|
|
return function (noop) {
|
|
|
|
return noop;
|
|
|
|
};
|
|
|
|
};
|