dy / spect

Observable selectors in DOM
https://dy.github.io/spect
MIT License
76 stars 8 forks source link

v → v, t/f? #178

Closed dy closed 4 years ago

dy commented 4 years ago

Revert #173.

Transform observable is different from simple value holder. The initial problem is 2-way binding: if prev observable has mapper function but no unmapper, then next observable won't be able to setup setter:

let v1 = v(2, v => v + 1)
let v2 = v(v1) // subscription obtains value `3`, which subscribes/sets back to v1, which sets it to `4` etc.

So setter-mapper is possible for subscriptions with defined unmapper.

let v1 = v(2, v => v + 1, v => v - 1)
let v2 = v1()

That is also mathematically correct, but raises question of how to differentiate v1.set → map (internal) from v2.set → unmap → v1.set (external). v1.unset()?

Or instead have t(src, map, unmap)? Does that solve the issue? Not exactly. unmap doesn't match self map here - it attempts to unmap to prev observer.

dy commented 4 years ago

From the other point, having a separate effect gives:

Ok, if you want - call it f. But v is just a particular case of f, it is not needed then)

dy commented 4 years ago

done as v

dy commented 4 years ago

Although value is particular case of from, it has advantages.

dy commented 4 years ago

f also allows dotprop-like observable creation f(obj, 'x.y.z') which is... same as prop actually.

dy commented 4 years ago

Ok, with #182 sub-props are exposed as observable props on root observable.

dy commented 4 years ago

f has significant use-case: create observable from something. What about v.from?

dy commented 4 years ago

The problem of v.from is that it is more important than single-constant init, which is useful but rare. What if instead use react-like initializer v(() => value) ? We guess that wrapper over another observable does not make much sense v(v1) - without mapper. So we can safely consider it a mapper v(map)

dy commented 4 years ago

Ok, done as initializer.