All files / src/action actionManager.js

90.63% Statements 58/64
80.39% Branches 41/51
94.12% Functions 16/17
88.68% Lines 47/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                            20x 20x 12x       12x 12x 8x 6x 6x 1x 1x   5x           5x 5x   2x   2x 2x   1x   2x 1x 1x 1x       1x 1x           1x 1x                           30x 10x 7x     20x 20x 13x 7x 3x   1x         1x     2x 2x 2x       4x         5x         5x 5x 3x 2x 2x 1x   1x        
// @flow
import type {ActionManagerDef, Action, ActionType } from './types';
import get from 'lodash/get';
import set from 'lodash/set';
import updateWith from 'lodash/updateWith';
import isArray from 'lodash/isArray';
import isPlainObject from 'lodash/isPlainObject';
import ObjectPattern from './pattern/objectPattern';
import ArrayPattern from './pattern/arrayPattern';
import ConnectPattern from './pattern/connectPattern';
 
export class ActionManager implements ActionManagerDef {
  store = {};
  addAction = (action: Action<ActionType>): void => {
    const {key, id} = action.payload;
    if (action.type === 'UPDATE_OBJECT') {
      const patternItem = get(this.store, [key], {
        connect: new ConnectPattern(),
        object: new ObjectPattern()
      });
      patternItem.object.addAction(action);
      set(this.store, [key], patternItem);
    } else if (action.type === 'CREATE_ARRAY' || action.type === 'UPDATE_ARRAY' || action.type === 'DELETE_ARRAY') {
      let patternItem = get(this.store, [key], []).find(item => item.id === id);
      if (patternItem) {
        patternItem.array.addAction(action);
        updateWith(this.store, key, list => list.map(item => item.id === id ? patternItem : item));
      } else {
        patternItem = {
          id,
          array: new ArrayPattern(),
          connect: new ConnectPattern()
        };
        // $FlowFixMe
        patternItem.array.addAction(action);
        updateWith(this.store, key, list => (list || []).concat(patternItem));
      }
    } else Eif (action.type === 'CONNECT' || action.type === 'DISCONNECT' || action.type === 'CREATE_AND_CONNECT' || action.type === 'DISCONNECT_AND_DELETE') {
      // relation in object
      let patternItem = get(this.store, [key]);
      if (id && patternItem) {
      // relation in array
        patternItem = patternItem.find(item => item.id === id);
      }
      if (patternItem) {
        patternItem.connect.addAction(action);
        Eif (id) {
          updateWith(this.store, key, list => list.map(item => item.id === id ? patternItem : item));
        } else {
          updateWith(this.store, key, patternItem);
        }
      } else Eif (id) {
        patternItem = {
          id,
          array: new ArrayPattern(),
          connect: new ConnectPattern()
        };
        // $FlowFixMe
        patternItem.connect.addAction(action);
        updateWith(this.store, key, list => (list || []).concat(patternItem));
      } else {
        patternItem = {
          object: new ObjectPattern(),
          connect: new ConnectPattern()
        };
        // $FlowFixMe
        patternItem.connect.addAction(action);
        set(this.store, key, patternItem);
      }
    }
  }
 
  getActions = (key?: string, id?: string): Array<any> => {
    if (!key) {
      return Object.keys(this.store).reduce((result: any, key: any) => {
        return result.concat(this.getActions(key));
      }, []);
    }
    const item = get(this.store, key);
    if (isPlainObject(item)) {
      return item.object.getActions().concat(item.connect.getActions());
    } else if (isArray(item)) {
      if (id) {
        // get action by key, id
        const patternItem = item.find(item => item.id === id) || {
          array: new ArrayPattern(),
          connect: new ConnectPattern()
        };
        // $FlowFixMe
        return patternItem.array.getActions().concat(patternItem.connect.getActions());
      } else {
        // get all key action
        return item.reduce((result: Array<any>, currItem: Object) => {
          const actions = currItem.array.getActions().concat(currItem.connect.getActions());
          return result.concat(actions);
        }, []);
      }
    } else {
      return [];
    }
  }
 
  removeActions = (key: string, id?: string) => {
    Iif (!key) {
      return Object.keys(this.store).forEach(key => {
        this.removeActions(key);
      });
    }
    const item = get(this.store, key);
    if (isPlainObject(item)) {
      delete this.store[key];
    } else Eif (isArray(item)) {
      if (id) {
        updateWith(this.store, key, list => list.filter(item => item.id !== id));
      } else {
        delete this.store[key];
      }
    }
  }
}