-
Notifications
You must be signed in to change notification settings - Fork 279
MobXPromise
MobxPromises (MP) are integrated into application code by reference to the MP's observable status or result property in the render method of components marked as Mobx observers. Thanks to Mobx-React, the component's render function will be re-invoked whenever the referenced observable changes. The status of the MobXPromise and its resulting data will thus be reflected as a product of reference, with no other wiring necessary.
How does this happen? Reference to status (or result) invokes that property's setter. The setter first checks the status of any MobxPromises in the await collection (this causes the same process to occur inside these children). If any awaited promises are not complete (i.e. status = error || pending) then that is assumed to be the status of the parent promise. If all awaited promise are complete, then the MP calls its invoke method by requesting a new invokeId (should we name that method more descriptively?). As the invoke method may be asychronous, it must return a promise.
When that promise resolves, we move to set the MobxPromise's status to complete. But we must first make sure that the invocation promise hasn't become stale during its asychronous operation. We do that by checking to see that invokeId (passed at time of invocation) matches that of the instance at resolution time. Match failure could occur if members of the await collection (or any observable property referenced in invoke method) changed their status during the async process.
Invocation promises which become stale in this way will never set the status or result of the MobXPromise and will thus have no influence on the application.