Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclassing/extending Observable - is it currently feasible? #1757

Closed
halhenke opened this issue Jun 9, 2016 · 5 comments
Closed

Subclassing/extending Observable - is it currently feasible? #1757

halhenke opened this issue Jun 9, 2016 · 5 comments

Comments

@halhenke
Copy link

halhenke commented Jun 9, 2016

Hey,

Apologies for making this an issue - dont mean to abuse the system - but had asked on Gitter etc and not gotten a response.

I just wanted to get an idea if creating a subclass and overriding lift was a viable approach at this point in order to get a Custom Observable that will always return the same Custom Observable.

From trying to follow this approach though it seems that operators like merge, concat etc call ArrayObservable.lift and not the overridden lift method and thus return an ordinary observable.

Basically what I was interested in was creating an Observable that kept a reference to some Immutable state and would pass it along to the next Observer if that helps at all.

re:
#60
#1124

@benlesh
Copy link
Member

benlesh commented Jun 9, 2016

@halhenke yes, it's a viable solution. In fact our docs outline it as a way to add operators to a custom observable: https://github.com/ReactiveX/rxjs/blob/master/doc/operator-creation.md#adding-the-operator-to-observable

@benlesh benlesh closed this as completed Jun 9, 2016
@benlesh
Copy link
Member

benlesh commented Jun 9, 2016

I hope you find that helpful. I'm closing for now, as I think I've answered your question.

@halhenke
Copy link
Author

halhenke commented Jun 9, 2016

Thanks for responding @Blesh

the problem seems to be that some operators will use the custom lift function and thus return the custom Observable and some operators wont.

e.g. the map operators uses this.lift:

export function map<T, R>(project: (value: T, index: number) => R, thisArg?: any): Observable<R> {
 //...
  return this.lift(new MapOperator(project, thisArg));
}

https://github.com/ReactiveX/rxjs/blob/master/src/operator/map.ts#L42

while the concat, merge operators etc call the ArrayObservable.lift operator directly and thus will not return Custom Observables:

export function concatStatic<T, R>(...observables: Array<ObservableInput<any> | Scheduler>): Observable<R> {
  // ...
  return new ArrayObservable(observables, scheduler).lift(new MergeAllOperator<R>(1));
}

https://github.com/ReactiveX/rxjs/blob/master/src/operator/concat.ts#L122

I may be missing something (I hope so) but it would seem that this means its not currently possible to extend Observables in a consistent way (always getting back the Custom Observable) without rewriting/overriding lots of stuff.

@istarkov
Copy link

istarkov commented Jul 1, 2016

Have the same issue,
having that :: operator can be never accepted,
what is the idea how to create a subset of rxjs without polluting global observable object.
I use something like this

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operator/map';

class ObservableExt extends Observable {
  static of= (...args) => new ObservableExt(subs => of(...args).subscribe(subs));
  lift(operator) {
    const observable = new ObservableExt();
    observable.source = this;
    observable.operator = operator;
    return observable;
  }
  map=map;
}
export default ObservableExt;

And looks like for each creation operator we should do the trick like for of operator above?

@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants