|
| 1 | +import * as is from "@redux-saga/is"; |
| 2 | +import { Effect } from "@redux-saga/types"; |
| 3 | + |
| 4 | +import * as effectTypes from "../constants"; |
| 5 | + |
| 6 | +const getEffectDescription = ( |
| 7 | + effect: Effect | any[] | IterableIterator<any> | Promise<any>, |
| 8 | +): any => { |
| 9 | + if (!effect) return effectTypes.UNKNOWN; |
| 10 | + |
| 11 | + if ((effect as any)?.root) return (effect as any)?.saga?.name; // TODO: Better typing |
| 12 | + if (is.iterator(effect)) { |
| 13 | + return (effect as any)?.name || effectTypes.UNKNOWN; |
| 14 | + } |
| 15 | + if (is.array(effect)) return null; |
| 16 | + if (is.promise(effect)) { |
| 17 | + let display: string; |
| 18 | + if ((effect as any)?.name) { |
| 19 | + // a promise object with a manually set name prop for display reasons |
| 20 | + display = `${effectTypes.PROMISE}(${(effect as any)?.name})`; |
| 21 | + } else if (effect?.constructor instanceof Promise.constructor) { |
| 22 | + // an anonymous promise |
| 23 | + display = effectTypes.PROMISE; |
| 24 | + } else { |
| 25 | + // class which extends Promise, so output the name of the class to precise |
| 26 | + display = `${effectTypes.PROMISE}(${effect?.constructor?.name})`; |
| 27 | + } |
| 28 | + return display; |
| 29 | + } |
| 30 | + if (is.effect(effect)) { |
| 31 | + const { type, payload: data } = (effect || {}) as Effect; |
| 32 | + if (type === effectTypes.TAKE) { |
| 33 | + return data?.pattern || "channel"; |
| 34 | + } else if (type === effectTypes.PUT) { |
| 35 | + return data?.channel ? data?.action : data?.action?.type; |
| 36 | + } else if (type === effectTypes.ALL) { |
| 37 | + return null; |
| 38 | + } else if (type === effectTypes.RACE) { |
| 39 | + return null; |
| 40 | + } else if (type === effectTypes.CALL) { |
| 41 | + return !data?.fn?.name || data?.fn?.name?.trim() === "" |
| 42 | + ? "(anonymous)" |
| 43 | + : data?.fn?.name; |
| 44 | + } else if (type === effectTypes.CPS) { |
| 45 | + return data?.fn?.name; |
| 46 | + } else if (type === effectTypes.FORK) { |
| 47 | + return data?.fn?.name; |
| 48 | + } else if (type === effectTypes.JOIN) { |
| 49 | + return data?.name; |
| 50 | + } else if (type === effectTypes.CANCEL) { |
| 51 | + return data?.name; |
| 52 | + } else if (type === effectTypes.SELECT) { |
| 53 | + return data?.selector?.name; |
| 54 | + } else if (type === effectTypes.ACTION_CHANNEL) { |
| 55 | + return data?.buffer == null ? data?.pattern : data; |
| 56 | + } else if (type === effectTypes.CANCELLED) { |
| 57 | + return null; |
| 58 | + } else if (type === effectTypes.FLUSH) { |
| 59 | + return data; |
| 60 | + } else if (type === effectTypes.GET_CONTEXT) { |
| 61 | + return data; |
| 62 | + } else if (type === effectTypes.SET_CONTEXT) { |
| 63 | + return data; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return effectTypes.UNKNOWN; |
| 68 | +}; |
| 69 | + |
| 70 | +export default getEffectDescription; |
0 commit comments