@gongt/ts-stl-client
Version:
50 lines (40 loc) • 1.66 kB
text/typescript
import {IS_CLIENT} from "@gongt/ts-stl-library/check-environment";
import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data";
import {Singleton} from "@gongt/ts-stl-library/pattern/singleton-class";
import {ArrayOrSingle} from "../global";
import {IState} from "./preload-state";
import {AppStore, LogicFunction, REDUX_PRELOAD_NAME, ReduxStore} from "./store";
export type EmptyPreloadHandler = (state: any) => void;
declare const __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
export interface IClientStateCreator<StateInterface extends IState> {
(store: StateInterface, req: GlobalVariable): StateInterface
}
(true)
export class ReduxStoreWindow<StateInterface extends IState> extends ReduxStore<StateInterface> {
protected readonly composeEnhancers;
public readonly singleton: AppStore<StateInterface>;
constructor(logicRegister?: ArrayOrSingle<LogicFunction<StateInterface>>) {
super(logicRegister);
if (IS_CLIENT && '__REDUX_DEVTOOLS_EXTENSION_COMPOSE__' in window) {
console.info('using redux dev-tools.');
this.composeEnhancers = __REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// TODO, Specify here name, actionsBlacklist, actionsCreators and other options
});
}
}
private emptyPlHandlers: EmptyPreloadHandler[] = [];
pushEmptyPreloadHandler(fn: EmptyPreloadHandler) {
this.emptyPlHandlers.push(fn);
}
createStore(): AppStore<StateInterface> {
const global = new GlobalVariable();
if (!global.has(REDUX_PRELOAD_NAME)) {
const pl = {};
this.emptyPlHandlers.forEach((fn) => {
fn(pl);
});
global.set(REDUX_PRELOAD_NAME, pl);
}
return super.createStore(global);
};
}