MCPcopy
hub / github.com/mobxjs/mobx / _when

Function _when

packages/mobx/src/api/when.ts:36–70  ·  view source on GitHub ↗
(predicate: () => boolean, effect: Lambda, opts: IWhenOptions)

Source from the content-addressed store, hash-verified

34}
35
36function _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
72function whenPromise(
73 predicate: () => boolean,

Callers 2

whenFunction · 0.85
whenPromiseFunction · 0.85

Calls 5

createActionFunction · 0.85
autorunFunction · 0.70
disposeMethod · 0.65
getNextIdFunction · 0.50
allowStateChangesFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…