justin-schroeder / arrow-js

Reactivity without the framework
https://arrow-js.com
MIT License
2.38k stars 49 forks source link

w: not printing all outputs? #19

Closed milkoqq closed 1 year ago

milkoqq commented 1 year ago

Hey, awesome work! Excuse my ignorance, I am a newbie with frameworks, proxies etc..! I've got a couple of questions.. 1) Why doesn't the watcher log all the values in example below and just logs the first and the last one? I thought it would do something like $on/$off but with just a one-liner.. 2) Since the watcher watches, why log immediately the function (for 25*10) since the data hasn't changed? 3) From the docs, I couldn't understand the use of $off in the example! Thanks!! Great work!

const data = r({
  price: 25,
  quantity: 10,
  logTotal: true
})

function total () {
  if (data.logTotal) {
    console.log(`Total: ${data.price * data.quantity}`);
  }
}

w(total)

data.price = 35
data.price = 66
data.price = 150
kyfex-uwu commented 1 year ago

For 2, generally you'd want a "starting state". In this case it would be helpful to know the total at the start of the script.

kyfex-uwu commented 1 year ago

And .$off is scheduled to be put in the documentation, see #13

justin-schroeder commented 1 year ago

Watchers in arrow will always run eagerly, otherwise they don’t know what their dependencies are. You can provide a second argument which is a second callback that receives the result of the watcher, and then you can choose to do something else with that value.

milkoqq commented 1 year ago

Thanks a lot! Will try out, I think I got it!