question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Observable functions?

See original GitHub issue

Is 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:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
evilsoftcommented, Jun 14, 2016

Merged related PR. closing this out.

0reactions
evilsoftcommented, Jun 13, 2016

Looks like it is 😎, any object can be used. Gonna get this going, will PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Observable - RxJS
An Observable is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it's invoked ......
Read more >
Using observables to pass values - Angular
Use the Observable constructor to create an observable stream of any type. The constructor takes as its argument the subscriber function to run...
Read more >
Understanding RxJS Observables and why you need them
Streams are important to understand because they are facilitated by RxJS Observables. An Observable is basically a function that can return ...
Read more >
RxJS - Observables - Tutorialspoint
An observable is a function that creates an observer and attaches it to the source where values are expected from, for example, clicks,...
Read more >
An intro to Observables and how they are different from ...
In short, you can say observables are simply a function that are able to give multiple values over time, either synchronously or asynchronously....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found