Advertisement
Guest User

Untitled

a guest
May 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // store/coreReducer.ts
  2.  
  3. import { Action } from 'redux';
  4.  
  5. interface Dictionary<T> {
  6. [key: string]: T;
  7. }
  8.  
  9. export function callAction<Data>(type: string, data?: Data): Action<Data> {
  10. return { type, data };
  11. }
  12.  
  13. export abstract class ReducerCollection<State> {
  14. protected actions: Dictionary<(state: State, action?) => State> = this.createActions();
  15. protected initialState: State = this.initState();
  16.  
  17. public changeState<Data>(
  18. state: State = this.initialState,
  19. action: Action<Data> = { type: '', data: undefined }
  20. ): State {
  21. if (action.type && this.actions[action.type]) {
  22. return this.actions[action.type](state, action);
  23. }
  24.  
  25. return state;
  26. }
  27.  
  28. protected abstract createActions(): Dictionary<(state: State, action?) => State>;
  29.  
  30. protected abstract initState(): State;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement