SHARE
TWEET

Untitled

a guest May 19th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top