| 66 | }; |
| 67 | |
| 68 | function lazyInitializer<T>(payload: Payload<T>): T { |
| 69 | if (payload._status === Uninitialized) { |
| 70 | if (__DEV__ && enableAsyncDebugInfo) { |
| 71 | const ioInfo = payload._ioInfo; |
| 72 | if (ioInfo != null) { |
| 73 | class="cm">// Mark when we first kicked off the lazy request. |
| 74 | class="cm">// $FlowFixMe[cannot-write] |
| 75 | ioInfo.start = ioInfo.end = performance.now(); |
| 76 | } |
| 77 | } |
| 78 | const ctor = payload._result; |
| 79 | const thenable = ctor(); |
| 80 | class="cm">// Transition to the next state. |
| 81 | class="cm">// This might throw either because it's missing or throws. If so, we treat it |
| 82 | class="cm">// as still uninitialized and try again next time. Which is the same as what |
| 83 | class="cm">// happens if the ctor or any wrappers processing the ctor throws. This might |
| 84 | class="cm">// end up fixing it if the resolution was a concurrency bug. |
| 85 | thenable.then( |
| 86 | moduleObject => { |
| 87 | if ( |
| 88 | (payload: Payload<T>)._status === Pending || |
| 89 | payload._status === Uninitialized |
| 90 | ) { |
| 91 | class="cm">// Transition to the next state. |
| 92 | const resolved: ResolvedPayload<T> = (payload: any); |
| 93 | resolved._status = Resolved; |
| 94 | resolved._result = moduleObject; |
| 95 | if (__DEV__) { |
| 96 | const ioInfo = payload._ioInfo; |
| 97 | if (ioInfo != null) { |
| 98 | class="cm">// Mark the end time of when we resolved. |
| 99 | class="cm">// $FlowFixMe[cannot-write] |
| 100 | ioInfo.end = performance.now(); |
| 101 | } |
| 102 | class="cm">// Make the thenable introspectable |
| 103 | if (thenable.status === undefined) { |
| 104 | const fulfilledThenable: FulfilledThenable<{default: T, ...}> = |
| 105 | (thenable: any); |
| 106 | fulfilledThenable.status = class="st">'fulfilled'; |
| 107 | fulfilledThenable.value = moduleObject; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | }, |
| 112 | error => { |
| 113 | if ( |
| 114 | (payload: Payload<T>)._status === Pending || |
| 115 | payload._status === Uninitialized |
| 116 | ) { |
| 117 | class="cm">// Transition to the next state. |
| 118 | const rejected: RejectedPayload = (payload: any); |
| 119 | rejected._status = Rejected; |
| 120 | rejected._result = error; |
| 121 | if (__DEV__ && enableAsyncDebugInfo) { |
| 122 | const ioInfo = payload._ioInfo; |
| 123 | if (ioInfo != null) { |
| 124 | class="cm">// Mark the end time of when we rejected. |
| 125 | class="cm">// $FlowFixMe[cannot-write] |