(predicate: () => boolean, effect: Lambda, opts: IWhenOptions)
| 34 | } |
| 35 | |
| 36 | function _when(predicate: () => boolean, effect: Lambda, opts: IWhenOptions): IReactionDisposer { |
| 37 | let timeoutHandle: any |
| 38 | if (typeof opts.timeout === "number") { |
| 39 | const error = new Error("WHEN_TIMEOUT") |
| 40 | timeoutHandle = setTimeout(() => { |
| 41 | if (!disposer[$mobx].isDisposed) { |
| 42 | disposer() |
| 43 | if (opts.onError) { |
| 44 | opts.onError(error) |
| 45 | } else { |
| 46 | throw error |
| 47 | } |
| 48 | } |
| 49 | }, opts.timeout) |
| 50 | } |
| 51 | |
| 52 | opts.name = __DEV__ ? opts.name || "When@" + getNextId() : "When" |
| 53 | const effectAction = createAction( |
| 54 | __DEV__ ? opts.name + "-effect" : "When-effect", |
| 55 | effect as Function |
| 56 | ) |
| 57 | // eslint-disable-next-line |
| 58 | var disposer = autorun(r => { |
| 59 | // predicate should not change state |
| 60 | let cond = allowStateChanges(false, predicate) |
| 61 | if (cond) { |
| 62 | r.dispose() |
| 63 | if (timeoutHandle) { |
| 64 | clearTimeout(timeoutHandle) |
| 65 | } |
| 66 | effectAction() |
| 67 | } |
| 68 | }, opts) |
| 69 | return disposer |
| 70 | } |
| 71 | |
| 72 | function whenPromise( |
| 73 | predicate: () => boolean, |
no test coverage detected
searching dependent graphs…