import { createStore , applyMiddleware } from "redux" ; import { persistStore } from "redux-persist" ; import thunkMiddleware from "redux-thunk" ; import { createLogger } from "redux-logger" ; import { useDispatch } from "react-redux" ; import { ActionMeta } from "redux-actions" ; import promiseMiddleware from "./promise-middleware" ; import reducers from "./construct" ; export * from "./reducers" ; export * from "./construct" ; const middleware = [ thunkMiddleware , promiseMiddleware , ] ; if ( __DEV__ ) { const ignoreActions = [ "persist/PERSIST" , "persist/REHYDRATE" , ] ; const predicate = ( _ , action ) => { return ! ignoreActions . includes ( action . type ) ; } ; const logger = { log : ( msg ) => { if ( msg [ 0 ] === "#" ) { console . log ( msg ) ; } } } ; middleware . push ( createLogger ( { predicate , logger , stateTransformer : ( ) => "state" , actionTransformer : ( { type , error , payload } ) => ( { type , error , payload : ( payload !== undefined ) } ) , titleFormatter : ( action , time , took ) => { let prefix = "->" ; if ( action . error ) { prefix = "xx" ; } else if ( action . payload ) { prefix = "==" ; } return `# ${ prefix } ${ action . type } @ ${ time } (in ${ took . toFixed ( 2 ) } ms)` ; } , colors : { title : false , prevState : false , action : false , nextState : false , error : false } } ) ) ; } export function asyncDispatch < T , V > ( action ) { return store . dispatch ( action ) as any ; } export function useAsyncDispatch ( ) { const dispatch = useDispatch ( ) ; return function ( action ) : any { return dispatch ( action ) as any ; } as typeof asyncDispatch ; } export const store = createStore ( reducers , applyMiddleware ( ... middleware ) ) ; export const persistor = persistStore ( store , { manualPersist : true } as any ) ;