All files createModule.ts

82.19% Statements 60/73
43.75% Branches 14/32
89.47% Functions 17/19
81.16% Lines 56/69

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                                                                                                              13x 13x 13x 13x 13x   13x 13x 13x   13x 13x 13x   13x     35x 28x 28x 9x 9x   28x 9x         9x         9x 9x           28x 9x                         35x 3x 3x   35x 8x 8x 8x   8x   35x         35x       12x   22x 22x 35x 35x 35x 35x   31x 22x             12x 12x 12x 12x       12x       10x 10x 10x 1x   9x   10x 10x       24x      
import { ChainedReducer } from './ChainedReducer';
import { Epic } from './Epic';
import * as React from 'react';
import { getIsHmr } from './onHmr';
import { StateGetter } from './types';
import { useMappedState } from './useMappedState';
import { snakeCase } from './utils';
import { useRegistry } from './useRegistry';
import { Store } from './Store';
 
export type Nullable<T> = T | null;
 
export type AnyFn = (...args: any[]) => any;
 
export type ConvertAC<T> = false extends T
  ? () => {}
  : T extends AnyFn
  ? T
  : () => {};
 
export type ConvertActions<T> = { [P in keyof T]: ConvertAC<T[P]> };
 
export type ActionMap = { [name: string]: Nullable<(...args: any[]) => {}> };
 
export interface HandleWithState<TState> {
  (): void;
  epic(): Epic;
  reducer(initialState: TState): ChainedReducer<TState>;
  reset(): void;
}
 
export interface Handle {
  (): void;
  epic(): Epic;
}
 
type ModuleBase = [Handle] & {
  withActions<T extends ActionMap>(
    actionMap: T
  ): ModuleWithActions<ConvertActions<T>>;
  withState<TState>(): ModuleWithState<TState>;
};
 
type ModuleWithActions<TActions> = [Handle, TActions] & {
  withState<TState>(): ModuleWithActionsAndState<TState, TActions>;
};
 
type ModuleWithState<TState> = [
  HandleWithState<TState>,
  StateGetter<TState>
] & {
  withActions<T extends ActionMap>(
    actionMap: T
  ): ModuleWithActionsAndState<TState, ConvertActions<T>>;
};
 
type ModuleWithActionsAndState<TState, TActions> = [
  HandleWithState<TState>,
  TActions,
  StateGetter<TState>
];
 
export function createModule(name: symbol) {
  let hasState = false;
  let actions: any = null;
  let epic: any = null;
  let reducer: any = null;
  let store: any = null;
 
  const base = [createHandle()] as any;
  base.withActions = withActions;
  base.withState = withState;
 
  getState._module = name.toString();
  getState._store = null as Store<any> | null;
  getState.useState = () => useMappedState([getState as any], state => state);
 
  return base as ModuleBase;
 
  function createHandle() {
    const handle: HandleWithState<any> = () => {
      const registry = useRegistry();
      if (!store) {
        store = registry.getStore(name);
        getState._store = store;
      }
      React.useMemo(() => {
        store.enable({
          epic,
          reducer,
        });
 
        Iif (getIsHmr()) {
          if (actions && actions.$remounted) {
            registry.dispatch(actions.$remounted());
          }
        } else {
          store.initState();
          Iif (actions && actions.$mounted) {
            registry.dispatch(actions.$mounted());
          }
        }
      }, []);
 
      React.useEffect(() => {
        return () => {
          if (actions && actions.$unmounting) {
            registry.dispatch(actions.$unmounting());
          }
          if (store) {
            store.disable();
          }
          if (actions && actions.$unmounted) {
            registry.dispatch(actions.$unmounted());
          }
        };
      }, []);
    };
    handle.epic = () => {
      epic = new Epic();
      return epic;
    };
    handle.reducer = initialState => {
      Eif (!reducer) {
        const chained = new ChainedReducer(initialState);
        reducer = chained.asReducer();
      }
      return reducer;
    };
    handle.reset = () => {
      epic = null;
      reducer = null;
      store = null;
    };
    return handle;
  }
 
  function createActions(actionMap: any) {
    actions = Object.keys(actionMap).reduce(
      (acc, key) => {
        const type = snakeCase(key).toUpperCase();
        acc[key] = (...args: any[]) => {
          const ac = actionMap[key] || (() => ({}));
          const action = ac(...args) as any;
          action.type = [name, type];
          return action;
        };
        acc[key].getType = () => [name, type];
        return acc;
      },
      {} as { [s: string]: any }
    ) as any;
  }
 
  function withActions(newActionMap: object) {
    createActions(newActionMap);
    const ret = [createHandle(), actions] as any;
    Eif (!hasState) {
      ret.withState = withState;
    } else {
      ret.push(getState);
    }
    return ret;
  }
 
  function withState() {
    hasState = true;
    const ret = [createHandle()] as any;
    if (!actions) {
      ret.withActions = withActions;
    } else {
      ret.push(actions);
    }
    ret.push(getState);
    return ret;
  }
 
  function getState() {
    return store ? store.state : undefined;
  }
}