(...params: TParams)
| 43 | } |
| 44 | |
| 45 | async runAsync(...params: TParams): Promise<TData> { |
| 46 | this.count += 1; |
| 47 | const currentCount = this.count; |
| 48 | |
| 49 | const { |
| 50 | stopNow = false, |
| 51 | returnNow = false, |
| 52 | ...state |
| 53 | } = this.runPluginHandler('onBefore', params); |
| 54 | |
| 55 | // stop request |
| 56 | if (stopNow) { |
| 57 | return Promise.resolve(state.data); |
| 58 | } |
| 59 | |
| 60 | this.setState({ |
| 61 | loading: true, |
| 62 | params, |
| 63 | ...state, |
| 64 | }); |
| 65 | |
| 66 | // return now |
| 67 | if (returnNow) { |
| 68 | return Promise.resolve(state.data); |
| 69 | } |
| 70 | |
| 71 | this.options.onBefore?.(params); |
| 72 | |
| 73 | try { |
| 74 | // replace service |
| 75 | let { servicePromise } = this.runPluginHandler('onRequest', this.serviceRef.current, params); |
| 76 | |
| 77 | if (!servicePromise) { |
| 78 | servicePromise = this.serviceRef.current(...params); |
| 79 | } |
| 80 | |
| 81 | const res = await servicePromise; |
| 82 | |
| 83 | if (currentCount !== this.count) { |
| 84 | // prevent run.then when request is canceled |
| 85 | return new Promise(() => {}); |
| 86 | } |
| 87 | |
| 88 | // const formattedResult = this.options.formatResultRef.current ? this.options.formatResultRef.current(res) : res; |
| 89 | |
| 90 | this.setState({ |
| 91 | data: res, |
| 92 | error: undefined, |
| 93 | loading: false, |
| 94 | }); |
| 95 | |
| 96 | this.options.onSuccess?.(res, params); |
| 97 | this.runPluginHandler('onSuccess', res, params); |
| 98 | |
| 99 | this.options.onFinally?.(params, res, undefined); |
| 100 | |
| 101 | if (currentCount === this.count) { |
| 102 | this.runPluginHandler('onFinally', params, res, undefined); |
no test coverage detected