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 | 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 54x 45x 45x 45x 45x 24x 24x 22x 22x 45x 24x 2x 2x 22x 3x 24x 3x 3x 3x 3x 2x 54x 8x 8x 54x 10x 10x 10x 54x 54x 20x 51x 51x 87x 87x 87x 87x 67x 51x 20x 20x 20x 20x 20x 12x 12x 12x 2x 10x 12x 12x 16x | import { ChainedReducer } from './ChainedReducer'; import { Epic } from './Epic'; import * as React from 'react'; import { getIsHmr } from './onHmr'; import { StateGetter, Reducer } 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> = T extends null ? () => {} : T extends AnyFn ? T : never; export type ActionCreators<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<ActionCreators<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, ActionCreators<T>>; }; type ModuleWithActionsAndState<TState, TActions> = [ HandleWithState<TState>, TActions, StateGetter<TState> ]; export function createModule(name: symbol) { let hasState = false; let actions: ActionMap | null = null; let epic: Epic | null = null; let reducer: (Reducer<any> & ChainedReducer<any>) | null = null; let store: Store | null = 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(); store = registry.getStore(name); getState._store = store; React.useMemo(() => { store!.enable({ epic, reducer, }); if (!getIsHmr()) { store!.initState(); Iif (actions && actions.$init) { registry.dispatch(actions.$init()); } } }, []); // cannot use React.useLayoutEffect here // if a $mounted action modifies a store, it won't cause re-render React.useEffect(() => { if (getIsHmr()) { Eif (actions && actions.$remounted) { registry.dispatch(actions.$remounted()); } } else { if (actions && actions.$mounted) { registry.dispatch(actions.$mounted()); } } return () => { Iif (actions && actions.$unmounting) { registry.dispatch(actions.$unmounting()); } Eif (store) { store.disable(); } if (actions && actions.$unmounted) { registry.dispatch(actions.$unmounted()); } }; }, []); }; handle.epic = () => { epic = new Epic(); return epic; }; handle.reducer = initialState => { 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; } } |