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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | 1x 1x 13x 22x 11x 11x 25x 18x 18x 18x 18x 18x 18x 1x 5x 1x 12x 19x 19x 19x 19x 19x 19x 12x 12x 7x 8x 8x 5x 5x 5x 1x 12x 12x 1x | import { call, put as sagaPut } from 'redux-saga/effects'; import { Action, GReturnable, IIReturn, rpResolve } from './types'; /** * Automatically resolves promise of Action.promise * @param innerSaga - Generator Function for redux-saga */ const promiseHandler = <T extends string, P, R>( innerSaga: (args?: P) => IterableIterator<R> ) => function* innerFunction(action: Action<T, P, IIReturn<R>>) { try { const data: IIReturn<R> = yield call(innerSaga, action.payload); if (action.resolve) { action.resolve(data); } } catch (e) { if (action.reject) { action.reject(e); } } }; /** * Action Creator * @param type Action Type */ const actionCreator = <T, P, R>(type: T) => (payload?: P) => { let resolve: (value: IIReturn<R>) => void = () => { }; let reject: (reason?: any) => void = () => { }; const promise = new Promise<IIReturn<R>>((pResolve, pReject) => { resolve = pResolve; reject = pReject; }); return { type, payload, promise, resolve, reject }; }; export { rpResolve }; /** * Extract Actions to be used in reducer * @example * type Actions = ExtractActions<loginAction | logoutAction> */ export type ExtractActions<Base> = { [Key in keyof Base]: ActionUnion<Base[Key]> }[keyof Base]; export type ActionUnion<A> = { [Key in keyof A]: A[Key] extends (...args: any[]) => any ? ReturnType<A[Key]> extends IterableIterator<any> ? never : ReturnType<A[Key]> : never }[keyof A]; /** * Generator return value * @param t any */ export function reg<T>(t: T) { return t as GReturnable<T>; } // Helper types type invokeSignature<T extends string, P, R> = P extends undefined ? { (): Action<T, P, IIReturn<R>>; } : P extends boolean ? { (payload: boolean): Action<T, P, IIReturn<R>>; } : { (payload: P): Action<T, P, IIReturn<R>>; }; type handleAction<P, R, H> = | (P extends undefined ? () => IterableIterator<R> : (args: P) => IterableIterator<R>) | H; type actionCreator<T extends string, P, R> = P extends undefined ? () => Action<T, P, IIReturn<R>> : P extends boolean ? (payload: boolean) => Action<T, P, IIReturn<R>> : (payload: P) => Action<T, P, IIReturn<R>>; type actionHandler<T extends string, P, H, R> = H extends undefined ? undefined : (action: Action<T, P, IIReturn<R>>) => IterableIterator<any>; export function createAction<T extends string>( type: T ): { (): { (): Action<T>; TRIGGER: T; trigger: () => Action<T>; }; <P = undefined, H = undefined, R = undefined>( handleAction?: handleAction<P, R, H> ): { TRIGGER: T; trigger: actionCreator<T, P, R>; handleTrigger: actionHandler<T, P, H, R>; } & invokeSignature<T, P, R>; }; export function createAction<T extends string, ST extends string>( type: T, successType: ST ): { (): { (): Action<T>; TRIGGER: T; trigger: () => Action<T>; SUCCESS: ST; success: () => Action<ST>; }; < P = undefined, SP = undefined, H = undefined, R = undefined, SH = undefined, SR = undefined >( handleAction?: handleAction<P, R, H>, handleSuccess?: handleAction<SP, SR, SH> ): { TRIGGER: T; trigger: actionCreator<T, P, R>; handleTrigger: actionHandler<T, P, H, R>; SUCCESS: ST; success: actionCreator<ST, SP, SR>; handleSuccess: actionHandler<ST, SP, SH, SR>; } & invokeSignature<T, P, R>; }; export function createAction< T extends string, ST extends string, FT extends string >( type: T, successType: ST, failureType: FT ): { (): { (): Action<T>; TRIGGER: T; trigger: () => Action<T>; SUCCESS: ST; success: () => Action<ST>; FAILURE: FT; failure: () => Action<FT>; }; < P = undefined, SP = undefined, FP = undefined, H = undefined, R = undefined, SH = undefined, SR = undefined, FH = undefined, FR = undefined >( handleAction?: handleAction<P, R, H>, handleSuccess?: handleAction<SP, SR, SH>, handleFailure?: handleAction<FP, FR, FH> ): { TRIGGER: T; trigger: actionCreator<T, P, R>; handleTrigger: actionHandler<T, P, H, R>; SUCCESS: ST; success: actionCreator<ST, SP, SR>; handleSuccess: actionHandler<ST, SP, SH, SR>; FAILURE: FT; failure: actionCreator<FT, FP, FR>; handleFailure: actionHandler<FT, FP, FH, FR>; } & invokeSignature<T, P, R>; }; export function createAction< T extends string, ST extends string, FT extends string >(type: T, successType?: ST, failureType?: FT) { function actionPayload<P, R, SP, SR, FP, FR>( handleTrigger?: (args?: P) => IterableIterator<R>, handleSuccess?: (args?: SP) => IterableIterator<SR>, handleFailure?: (args?: FP) => IterableIterator<FR> ) { // handler return const routine = (payload?: P) => { let resolve: (value: IIReturn<R>) => void = () => { }; let reject: (reason?: any) => void = () => { }; const promise = new Promise<IIReturn<R>>((pResolve, pReject) => { resolve = pResolve; reject = pReject; }); return { type, payload, promise, resolve, reject }; }; routine.TRIGGER = type; routine.trigger = actionCreator<T, P, R>(type); if (handleTrigger) { routine.handleTrigger = promiseHandler<T, P, R>(handleTrigger); } if (successType) { routine.SUCCESS = successType; routine.success = actionCreator<ST, SP, SR>(successType); if (handleSuccess) { routine.handleSuccess = promiseHandler<ST, SP, SR>(handleSuccess); } } if (failureType) { routine.FAILURE = failureType; routine.failure = actionCreator<FT, FP, FR>(failureType); if (handleFailure) { routine.handleFailure = promiseHandler<FT, FP, FR>(handleFailure); } } return routine; } return actionPayload; } /** * transform return type of redux-saga's put to any * @param action Action */ interface Iput { test: any; } export const put = (action: Ia) => (sagaPut(action) as unknown) as Iput; interface Ia { type: string; payload?: any; [key: string]: any; } |