import { createReduxHistoryContext } from "redux-first-history" ; import { createBrowserHistory , History } from "history" ; import { applyMiddleware , compose , createStore , Store , StoreEnhancer } from "redux" ; import createSagaMiddleware , { SagaMiddleware } from "redux-saga" ; import { takeEvery } from "redux-saga/effects" ; import { Logger , LoggerConfig , LoggerImpl } from "./Logger" ; import { ActionHandler , ErrorHandler , executeAction } from "./module" ; import { Action , LOADING_ACTION , rootReducer , State } from "./reducer" ; import { captureError } from "./util/error-util" ; declare const window ; interface App { readonly browserHistory : History ; readonly store : Store < State > ; readonly sagaMiddleware : SagaMiddleware < any > ; readonly actionHandlers : { [ actionType ] : ActionHandler ; } ; readonly logger : LoggerImpl ; loggerConfig : LoggerConfig | null ; errorHandler : ErrorHandler ; } export const app = createApp ( ) ; export const logger = app . logger ; function composeWithDevTools ( enhancer ) { let composeEnhancers = compose ; if ( process . env . NODE_ENV === "development" ) { const extension = window . __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ; if ( extension ) { composeEnhancers = extension ( { actionsBlacklist : [ LOADING_ACTION ] , } ) ; } } return composeEnhancers ( enhancer ) ; } function createApp ( ) { const eventLogger = new LoggerImpl ( ) ; const sagaMiddleware = createSagaMiddleware ( { onError : ( error , info ) => captureError ( error , "@@framework/detached-saga" , { extraStacktrace : info . sagaStack } ) , } ) ; const { createReduxHistory , routerMiddleware , routerReducer } = createReduxHistoryContext ( { history : createBrowserHistory ( ) , } ) ; const store = createStore ( rootReducer ( routerReducer ) , composeWithDevTools ( applyMiddleware ( routerMiddleware , sagaMiddleware ) ) ) ; sagaMiddleware . run ( function * ( ) { yield takeEvery ( "*" , function * ( action ) { const handler = app . actionHandlers [ action . type ] ; if ( handler ) { yield * executeAction ( action . type , handler , ... action . payload ) ; } } ) ; } ) ; return { browserHistory : createReduxHistory ( store ) , store , sagaMiddleware , actionHandlers : { } , logger : eventLogger , loggerConfig : null , * errorHandler ( ) { } , } ; }