1 | import {IS_CLIENT} from "@gongt/ts-stl-library/check-environment";
|
2 | import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data";
|
3 | import {Singleton} from "@gongt/ts-stl-library/pattern/singleton-class";
|
4 | import {ArrayOrSingle} from "../global";
|
5 | import {IState} from "./preload-state";
|
6 | import {AppStore, LogicFunction, REDUX_PRELOAD_NAME, ReduxStore} from "./store";
|
7 |
|
8 | export type EmptyPreloadHandler = (state: any) => void;
|
9 |
|
10 | declare const __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
|
11 |
|
12 | export interface IClientStateCreator<StateInterface extends IState> {
|
13 | (store: StateInterface, req: GlobalVariable): StateInterface
|
14 | }
|
15 |
|
16 | @Singleton(true)
|
17 | export 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 |
|
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 | }
|