store.js 640 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { combineReducers, createStore } from "redux"
  2. export class StoreWrapper {
  3. constructor() {
  4. this._store = null
  5. this._reducers = {}
  6. this._initialState = {}
  7. }
  8. addReducer(name, reducer, initialState) {
  9. this._reducers[name] = reducer
  10. this._initialState[name] = initialState
  11. }
  12. init() {
  13. this._store = createStore(
  14. combineReducers(this._reducers),
  15. this._initialState
  16. )
  17. }
  18. getStore() {
  19. return this._store
  20. }
  21. // Store API
  22. getState() {
  23. return this._store.getState()
  24. }
  25. dispatch(action) {
  26. return this._store.dispatch(action)
  27. }
  28. }
  29. export default new StoreWrapper()