jeffijoe / mobx-task

Makes async function state management in MobX fun.
MIT License
238 stars 6 forks source link

Can I use mobx-task with mobx-state-tree? #10

Closed greeksolid closed 5 years ago

greeksolid commented 5 years ago

I am trying to use mobx-task to wrap a mobx-state-tree action with mobx-task like this :

const validateNewCustomer = task(flow(function*(pin) {
   const response = yield validateNewCustomerWithPinApi(
    self.customer.id,
    pin
  )
  if (response.data === true) {
    return true
  }
  return false
}))

But when I try to access the task status from within a component I get undefined?

Has anyone tried something similar? I would really love to avoid all the boilerplate of tracking the promise state manually if possible

jeffijoe commented 5 years ago

I don't know how MST wraps actions, but mobx-task works by creating a new function and tacking on the state observables. If MST wraps it in another function, that state is lost. This is why task().wrap(fn => yourWrapper(fn)) exists. task().bind() is also automatically wrapped for you.

greeksolid commented 5 years ago

Yes it seems I have to learn/understand how MST wraps actions to make it work with task. (I am very new to MST). I tried to use task().wrap around the action before exporting it but got an error so I thought I ask here in hopes that you have worked with MST before. 😃 Thanks a lot for your time, really appreciate it.

jeffijoe commented 5 years ago

No problem, I hope you find a solution!

Shameless plug: I use LibX 😏