tc39 / proposal-observable

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

Observer's Methods should be Bound Functions #117

Closed Jamesernator closed 7 years ago

Jamesernator commented 7 years ago

As the title says, it's a little weird that I can do:

function wait(time) {
    return new Promise(resolve => setTimeout(resolve, time))
}

but not:

function ticks(time) {
    return new Observable(observer => {
        const interval = setInterval(observer.next, time);
        return _ => clearInterval(interval);
    }
}
RangerMauve commented 7 years ago

Pretty sure bound functions are a no-no given how much people are focusing on performance.

jhusain commented 7 years ago

There is nothing weird about the example above. resolve is a function, observer is an object. The expectation that methods on objects must be bound is well-established in JavaScript.

nmuldavin commented 6 years ago

Bound observer methods would allow destructuring for really concise syntax. Here's a passThrough operator written with destructuring:

const passThrough = (input, fn) =>
  new Observable(({ next, error, complete }) =>
    input.subscribe({
      next: value => {
        fn(value);
        next(value);
      },
      error,
      complete,
    }),
  );

and without:

const passThrough2 = (input, fn) =>
  new Observable(observer =>
    input.subscribe({
      next(value) {
        fn(value);
        observer.next(value);
      },
      error(e) {
        observer.error(e);
      },
      complete() {
        observer.complete();
      },
    }),
  );

Kindof nice. That said, if it's a performance issue it's a performance issue, I'd rather have performance than ultra concise syntax.