Observable functions?
See original GitHub issueIs there any reason to not allow observable function to be used with most.from
?
From what I read from the spec, I cannot find where the Observable has to be an object. Just that it needs to implement the Observer interface. Being able to observe a function would clear up a lot of boiler plate when working with virtual-DOM implementations that allow state changes on some app state to re-render when that state has been changed.
Yes I know this is very imperative in practice, but by allowing it, the imperative bits could be squirreled away in a lib and allow state changes by making an update
observable function.
Thinking something like this, please do not mind the nasty mutations and what not, just something to throw together for this issue. Also error handling and making sure we were passed an obserserver are not implemented here:
function observeFunc(f) {
const subs = []
const done = false
function subscribe(o) {
if(subs.indexOf(o) === -1) {
subs.push(o)
}
return {
closed: () => done,
unsubscribe() {
const idx = subs.indexOf(o)
if(idx !== -1) { subs.splice(idx, 1) }
}
}
}
function BigBoy(x) {
const y = f(x)
subs.forEach(s => s.next(y))
}
BigBoy[Symbol.observable] = () => {
return { subscribe }
}
return BigBoy
}
const x = observeFunc(x => x)
const x$ = most.from(x)
x$.observe(console.log.bind(console, 'observed x: '))
x(2)
x(10)
so if x
was an updater function we use it as eventHandlers that dispatch the new state through the stream.
This could be accomplished by changing this line: https://github.com/cujojs/most/blob/master/lib/observable/getObservable.js#L11
from:
if(o != null && typeof o === 'object')
to:
if(o != null && (typeof o === 'object' || typeof o === 'function'))
If you have no objections to this, I can get over my feelz about tab indentation and the use of semi-colons 😜 and submit a PR with this change and update specs.
_Edit_ Forgot to add the ability to pass the observer function in. 😧 and name to something a bit more appropriate.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Merged related PR. closing this out.
Looks like it is 😎, any object can be used. Gonna get this going, will PR.