UNPKG

1.66 kBPlain TextView Raw
1import {IS_CLIENT} from "@gongt/ts-stl-library/check-environment";
2import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data";
3import {Singleton} from "@gongt/ts-stl-library/pattern/singleton-class";
4import {ArrayOrSingle} from "../global";
5import {IState} from "./preload-state";
6import {AppStore, LogicFunction, REDUX_PRELOAD_NAME, ReduxStore} from "./store";
7
8export type EmptyPreloadHandler = (state: any) => void;
9
10declare const __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
11
12export interface IClientStateCreator<StateInterface extends IState> {
13 (store: StateInterface, req: GlobalVariable): StateInterface
14}
15
16@Singleton(true)
17export class ReduxStoreWindow<StateInterface extends IState> extends ReduxStore<StateInterface> {
18 protected readonly composeEnhancers;
19 public readonly singleton: AppStore<StateInterface>;
20
21 constructor(logicRegister?: ArrayOrSingle<LogicFunction<StateInterface>>) {
22 super(logicRegister);
23
24 if (IS_CLIENT && '__REDUX_DEVTOOLS_EXTENSION_COMPOSE__' in window) {
25 console.info('using redux dev-tools.');
26 this.composeEnhancers = __REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
27 // TODO, Specify here name, actionsBlacklist, actionsCreators and other options
28 });
29 }
30 }
31
32 private emptyPlHandlers: EmptyPreloadHandler[] = [];
33
34 pushEmptyPreloadHandler(fn: EmptyPreloadHandler) {
35 this.emptyPlHandlers.push(fn);
36 }
37
38 createStore(): AppStore<StateInterface> {
39 const global = new GlobalVariable();
40 if (!global.has(REDUX_PRELOAD_NAME)) {
41 const pl = {};
42 this.emptyPlHandlers.forEach((fn) => {
43 fn(pl);
44 });
45 global.set(REDUX_PRELOAD_NAME, pl);
46 }
47 return super.createStore(global);
48 };
49}