xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
549 stars 25 forks source link

Is it complicated to support @actionAsync/task syntax for mobx-keystone? #148

Closed swayf closed 4 years ago

swayf commented 4 years ago

subj =)

I am asking second time, because I liked much more then current with typescript workarounds =) The code will be much nicer, I think with async/await without type conversion to and from generators

xaviergonz commented 4 years ago

I still need to take a look to see if it is possible. The best case however would be if TS improved its support for generator functions rather than having to resort to people having to use "await task".

That being said, I usually find it is easier to have mutations in synchronous actions and a plain async method that calls these sync actions. For example:

@model(....)
class M extends Model({
  x: prop(0, { setterAction: true }),
  y: prop(0)
}) {
  @modelAction
  setY(y: number) { this.y = y }

  async something() {
    this.x = 10; // this is called/recorded as a sync action
    await delay(100)
    this.setY(20); // this is called/recorded as a sync action
  }
}

The only case were this would not be OK is if you were to do server/client action replication and you wanted the client to also have a delay as well (as it would happen in the server), or if you wanted to undo the async action as a single unit, but in any other case the pattern above should yield the same mutations.

swayf commented 4 years ago

Ok, I see. Thank you =)