import { createStore , combineReducers , applyMiddleware , Store , ReducersMapObject } from 'redux' ; import { composeWithDevTools } from 'redux-devtools-extension' ; import { createAggregate , createActions } from 'redux-aggregate' ; import thunk from 'redux-thunk' ; import { TimerAC } from './actions/timer' ; import { CounterST , CounterMT , CounterModel , CounterSB } from './models/counter' ; import { wait } from './helper/promise' ; export interface StoreST { counter : CounterST ; } export const Timer = createActions ( TimerAC , 'timer/' ) ; export const Counter = createAggregate ( CounterMT , 'counter/' ) ; Counter . subscribe ( Timer , CounterSB . Timer ) ; function storeFactory < R extends ReducersMapObject > ( reducer ) { return createStore ( combineReducers ( reducer ) , composeWithDevTools ( applyMiddleware ( thunk ) ) ) ; } export const store = storeFactory ( { counter : Counter . reducerFactory ( CounterModel ( { name : 'COUNTER' } ) ) } ) ; async function runTimerService ( ) { while ( true ) { await wait ( ) ; store . dispatch ( Timer . creators . tick ( ) ) ; } } store . dispatch ( Timer . creators . tick ( ) ) ; runTimerService ( ) ;