Mobx-state-tree: Cancel async flow action

Created on 28 Feb 2018  路  6Comments  路  Source: mobxjs/mobx-state-tree

Is there a reference to the active generator that runs the flow? I need that in order to cancel node fetches before the node is destroyed.

enhancement has PR

Most helpful comment

@elite174 not in that way, as there might be many flows running, not just one. But you could do this:

If this pattern works well, we could consider building it in somehow.

.actions(self => {
   let pendingPromises = []
   const myFlow = flow(function* () {})
   return {
        beforeDestroy() {
            pendingPromises.forEach(p => p.cancel())
        },
        myFlow: function() {
             const p = myFlow.apply(null, arguments)
             pendingPromises.push(p)
             return p
        }
   }
}

All 6 comments

@elite174 would you mind taking a look at the tests in #691 and indicate whether this would a solution for your problem?

Thanks!

@mweststrate Sure, I will! Thank you!

I looked at the PR, and that's really great. However, I have one question: is there a way to get the running flow? I want this flow in order to cancel it before the node is destroyed.
Something like this:

const M = types.model({})
.actions(self => ({
    beforeDestroy(){
        self.runningFlow.cancel()
    },

    // some actions here
}))

@elite174 not in that way, as there might be many flows running, not just one. But you could do this:

If this pattern works well, we could consider building it in somehow.

.actions(self => {
   let pendingPromises = []
   const myFlow = flow(function* () {})
   return {
        beforeDestroy() {
            pendingPromises.forEach(p => p.cancel())
        },
        myFlow: function() {
             const p = myFlow.apply(null, arguments)
             pendingPromises.push(p)
             return p
        }
   }
}

Oh, wow! That's what I was looking for! Thank you very much.

Closing for now as there is an open PR, but question is answered.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mshibl picture mshibl  路  3Comments

tahv0 picture tahv0  路  3Comments

FredyC picture FredyC  路  3Comments

lostfictions picture lostfictions  路  4Comments

A-gambit picture A-gambit  路  3Comments