Mobx: Introduce `firstrun` param for autorun functions

Created on 21 Apr 2016  ·  8Comments  ·  Source: mobxjs/mobx

See #207

var myData = observable({ some data })

autorun((firstrun) => {
  // start observing..
  var data = serializeStuff(myData)
  // .. but don't apply effects on the first run
  if (!firstrun)
    sendDataToServer(data)
})
🍗 enhancement

All 8 comments

wouldn't this be trivial to implement without an extra param to the autorun function?

firstrun = true
autorun(() => {
  if (firstrun) {
    // ...
    firstrun = false
  }
  // ...
})

Yep :)

Then I would forego this addition. Keep the official API as simple & small as possible.

A related question: can an autorun dispose itself? Like:

const dispose = autorun(() => {
  if (relevantEvent) {
    doSomething();
    dispose();
  }
});

see when

Op za 21 mei 2016 10:50 schreef Tim Meyer [email protected]:

A related question: can an autorun dispose itself? Like:

const dispose = autorun(() => {
if (relevantEvent) {
doSomething();
dispose();
}
});


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/mobxjs/mobx/issues/209#issuecomment-220766719

@timeyr the idiomatic way to do that is const dispose = when(() => relevantEvent, () => doSomething).

After running doSomething when will be automatically disposed. The returned dispose method is only need to _cancel_ the thing entirely.

Ah, I should RTFM, thanks for the hint!

Closing this for now. The feature still makes sense, but reaction does this already by default so maybe reaction is favorable for these patterns. Let's await how people use that in practice :)

Was this page helpful?
0 / 5 - 0 ratings