ccorcos / reactive-magic

Magical reactivity at your fingertips that helps you build complex applications quickly
40 stars 0 forks source link

'this' scope can get lost when chaining value.update #3

Open tony-sull opened 7 years ago

tony-sull commented 7 years ago

I'm trying out reactive-magic with a TodoMVC app locally and trying to stick to FP paradigms when possible. I ran into a scoping issue related to how I was calling value.update

See this gist for a complete example of how I was trying to write my store, https://gist.github.com/tonyfsullivan/16e47357f130d05796d215eb33a6c05f

Not sure if there's a clean way to keep Value and DerivedValue as classes and avoid the scoping issues, but I did try implementing them as functions rather than classes and it worked like a charm, https://gist.github.com/tonyfsullivan/60c9e1de255524e466e12aa9ec7c0dfb

Any thoughts on this approach, or a better way to handle it?

ccorcos commented 7 years ago

Your problem is function binding. You would have to use bind .then(todos.update.bind(todos)) or just forget trying to write pointfree.

I'd also mention that this library is the antithesis of pure functional code. I would suggest giving it a fresh mind and see where it takes you.

ccorcos commented 7 years ago

Also, thanks for checking it out! :)

ccorcos commented 7 years ago

To answer your second question, I'm using classes because they're easier to work with in Typescript because you don't need to have a discriminating property value to distinguish the type. You can use classes in a pure functional way btw. Just think of them like a type.

And I could define prototype methods with bound functions as well which would help you there: update = () => {}

tony-sull commented 7 years ago

Ah ha, exactly what I was looking for! I actually prefer the class design as well, forgot about Typescript's arrow function trick to get around binding on a class's public method

Mind if I file a PR with the change? Seems like a small thing, but its convenient being able to chain update/set in a promise