tc39 / proposal-observable

Observables for ECMAScript
https://tc39.github.io/proposal-observable/
3.06k stars 90 forks source link

Shouldn't we make Observer interface same as Generator object #174

Closed psxcode closed 6 years ago

psxcode commented 6 years ago

Generators can be used as

Generator object interface

interface Observer {
    next(value? : any) : void;
    return(value? : any) : void;
    throw(error) : void;
}

This will allow to use Generators as consumers of Observable stream

function* observer() {
  while(true)
    console.log(`observable value: ${yield}`)
}

observable.subscribe(observer())
RangerMauve commented 6 years ago

That's an awesome idea!

benjamingr commented 6 years ago

It's a nice concept, so nice in fact that's how we started several years ago 😆

If you look at the first iterations of this proposal this is how they worked. @jhusain has a talk about it.

The problem is that people found it confusing at the end of the day.

benjamingr commented 6 years ago

Found it https://github.com/tc39/proposal-observable/issues/33

zenparsing commented 6 years ago

That's the interface that we started with, but we ran into trouble with the semantics of return. For generators, we expect return to be called on both completion and cancellation, because return has cleanup semantics (it allows a generator function to execute finally blocks). But for Observable, we did not want to conflate cancellation with completion.

There were some other concerns as well.

Eventually, we were content with the fact that it is easy to adapt a generator object (or generator function) to an observer.