Extend Observable and set external source

Viewed 653

I want to extend Observable and set another source (e.g. another Behaviour Subject)

E.g. NgRx is doing it here: https://github.com/ngrx/platform/blob/9.0.0/modules/store/src/store.ts#L20

But RxJS Observable gives a deprecation warning on ´Observable.source´: https://github.com/ReactiveX/rxjs/blob/6.5.5/src/internal/Observable.ts#L25

This code is using the deprecated ´Observable.source´ - and it works:

const source: BehaviorSubject<any> = new BehaviorSubject(1);

class ObsStore extends Observable<any> {
    constructor() {
        super();
        this.source = source; // Observable.source is deprecated
    }

    // Implement custom methods here
    customMethod1() {}
}

export const obsStore$ = new ObsStore();
obsStore$.subscribe(data => console.log('output', data));

source.next(2);

// output 1
// output 2

Still I wonder if it is safe to use source. Is there maybe an alternative way to set an external source for an Observable?

NgRx also implements the lift method: https://github.com/ngrx/platform/blob/9.0.0/modules/store/src/store.ts#L90-L95 Not sure if that is needed.

Here is a stackblitz example: https://stackblitz.com/edit/extend-observable

Note: I want to extend Observable and add some custom public methods to it. The Custom Observable should behave like an BehaviorSubject but it should not expose next or getValue. Actually only pipe and the custom methods should be public.

1 Answers
Related