wana icon indicating copy to clipboard operation
wana copied to clipboard

Shorthand syntax for "Auto" reactions

Open aleclarson opened this issue 6 years ago • 4 comments

"Reactions" are unobserved side effects in response to an observed computation.

The current way to do this:

auto(() => {
  // Observed computation here
  noto(() => {
    // Unobserved side effects here
  })
})

The proposed way to do this:

auto(
  () => {
    // Observed computation here
  },
  result => {
    // Unobserved side effects here
  }
)

The bonus (of course) being that you don't need to import noto.

aleclarson avatar Oct 25 '19 22:10 aleclarson

Another option is to add a then method to the Auto class:

auto(() => {
  // Observed computation here
}).then(result => {
  // Unobserved side effects here
})

The bonus being that then callbacks could potentially be chained together.

Naming it then is both good and (mostly) bad. Familiar but probably confusing.

And it's similar to when(() => { ... }).then(() => { ... }) which already exists. 😮

aleclarson avatar Oct 25 '19 23:10 aleclarson

What would be you thoughts on just passing an optional desired observables array? And eliminating no() and noto() - a bit like in React's useEffect. That way you can mix observed and nonObserved in the one function instead of splitting it into to two.

auto(
  () => {
    // Observed computation here
  },
  [
    // desired observables here
  ]
)

ryanking1809 avatar Nov 16 '19 16:11 ryanking1809

@ryanking1809 I think that introduces unnecessary maintenance burden, and avoiding an extra function isn't worth that cost. For example, if you start using another observable value, you need to also add it to the array argument. If you forget, you'll get potentially confusing bugs.

Also, you'd have to repeat yourself quite a bit if you're reaching into a deeply nested structure.

Lastly, how would you specify observed properties in the array argument? I think it's all a bit too much trickery. :)

aleclarson avatar Nov 16 '19 16:11 aleclarson

@aleclarson Yeah that's fair, you've won me over ;)

ryanking1809 avatar Nov 16 '19 16:11 ryanking1809