UNPKG

@gongt/ts-stl-client

Version:
61 lines (48 loc) 2.15 kB
///<reference types="node"/> import {createSingletonHistory, HistoryCreator, MyHistoryOptions} from "@gongt/ts-stl-library/browser/history-object"; import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data"; import {ConnectedRouter, ConnectedRouterProps, routerMiddleware} from "react-router-redux"; import {reactUseRedux} from "../../react-redux/client"; import {ReactRender} from "../../react/render"; import {ReduxStoreWindow} from "../../redux/store-client"; import {ReduxStore} from "../store"; import {routing as ReduxRouter} from "./redux-router.store"; export interface ReduxRouterOptions { history: HistoryCreator; } if (!ConnectedRouter) { throw new TypeError('package `react-router-redux` not export ConnectedRouter, have you installed @next version?'); } export function reduxHandleReactRouter(react: ReactRender, redux: ReduxStoreWindow<any>, options: MyHistoryOptions = {},) { reduxHandleReactRouterBase(react, redux, () => { return options; }); reactUseRedux(react); } export function reduxHandleReactRouterBase(react: ReactRender, redux: ReduxStore<any>, options: (global: GlobalVariable) => MyHistoryOptions) { const preventDuplicate = new GlobalVariable(react); if (preventDuplicate.has('reduxRouter')) { throw new TypeError('reduxHandleReactRouter(): duplicate call.') } if (preventDuplicate.has('router')) { throw new TypeError('reduxHandleReactRouter(): must not call reactUseRouter().') } if (preventDuplicate.has('redux')) { throw new TypeError('reduxHandleReactRouter(): must not call reactUseRedux().') } preventDuplicate.set('reduxRouter', true); react.wrapComponent<ConnectedRouterProps<any>>('ConnectedRouter', ConnectedRouter, (global: GlobalVariable) => { let history = createSingletonHistory(global, options(global)); return { history, }; }); redux.useDynamic((global) => { return routerMiddleware(createSingletonHistory(global, options(global))); }); redux.register(ReduxRouter); }