Mobx: Repeated When

Created on 29 Jan 2019  ·  6Comments  ·  Source: mobxjs/mobx

I have a Idea. There is common pattern in my reactions:
reaction(() => client.online, online => online && ts.sync()).
Basically, it's just when, which is not autodisposes itself on first run. So, it can be done via when option named (e.g.) repeated. With it, my code will look as:
when(() => client.online, () => ts.sync(), {repeated: true})

  • [X] What problem would it solve for you?
    Code readability and complexity.
  • [X] Do you think others will benefit from this change as well and it should in core package (see also mobx-utils)?
    Yep, it just additional when option, which can be useful for many cases.
  • [X] Are you willing to (attempt) a PR yourself?
    Maybe, it shouldn't be difficult.

Most helpful comment

I think the syntactical sugar here is so straight-forward that it doesn't
justify expanding the api for it. Rather, it confuses two concepts which
are now clearly deliniated: one time? -> when. Multiple times? -> reaction

Op di 29 jan. 2019 om 14:44 schreef Иван Плесских <[email protected]

:

I have a Idea. There is common pattern in my reactions:
reaction(() => client.online, online => online && ts.sync()).
Basically, it's just when, which is not autodisposes itself on first run.
So, it can be done via when option named (e.g.) repeated. With it, my
code will look as:
when(() => client.online, () => ts.sync(), {repeated: true})

  • What problem would it solve for you?
    Code readability and complexity.
  • Do you think others will benefit from this change as well and it
    should in core package (see also mobx-utils)?
    Yep, it just additional when option, which can be useful for many
    cases.
  • Are you willing to (attempt) a PR yourself?
    Maybe, it shouldn't be difficult.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/1886, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhNAAf-KaEzD8wiGc6CgvoVXIBrp3ks5vIFAlgaJpZM4aYE2i
.

All 6 comments

I think the syntactical sugar here is so straight-forward that it doesn't
justify expanding the api for it. Rather, it confuses two concepts which
are now clearly deliniated: one time? -> when. Multiple times? -> reaction

Op di 29 jan. 2019 om 14:44 schreef Иван Плесских <[email protected]

:

I have a Idea. There is common pattern in my reactions:
reaction(() => client.online, online => online && ts.sync()).
Basically, it's just when, which is not autodisposes itself on first run.
So, it can be done via when option named (e.g.) repeated. With it, my
code will look as:
when(() => client.online, () => ts.sync(), {repeated: true})

  • What problem would it solve for you?
    Code readability and complexity.
  • Do you think others will benefit from this change as well and it
    should in core package (see also mobx-utils)?
    Yep, it just additional when option, which can be useful for many
    cases.
  • Are you willing to (attempt) a PR yourself?
    Maybe, it shouldn't be difficult.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/1886, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhNAAf-KaEzD8wiGc6CgvoVXIBrp3ks5vIFAlgaJpZM4aYE2i
.

Hmm. My second approach was reactionWhen, which is can be added to mobx-utils.

My two cents: call it whenever and just declare it yourself:

function whenever(whenFn: () => boolean, doFn: () => void): () => void {
  return reaction(whenFn, val => val && doFn());
}

Example usage:

 whenever(() => client.online, () => ts.sync());

Closing, the pattern is hopefully so straight forward I don't see any need to standardize this

Thanks for sharing the idea nonetheless!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

Was this page helpful?
0 / 5 - 0 ratings