asObserver

Hides the identity of an observer.

Returns

(Observer): An observer that hides the identity of the specified observer.

function SampleObserver () {
    Rx.Observer.call(this);
    this.isStopped = false;
}

SampleObserver.prototype = Object.create(Rx.Observer.prototype);
SampleObserver.prototype.constructor = SampleObserver;

Object.defineProperties(SampleObserver.prototype, {
    onNext: {
        value: x => {
            if (!this.isStopped) {
                console.log(`Next: ${x}`);
            }
        }
    },
    onError: {
        value: err => {
            if (!this.isStopped) {
                this.isStopped = true;
                console.log(`Error: ${err}`);
            }
        }
    },
    onCompleted: {
        value: () => {
            if (!this.isStopped) {
                this.isStopped = true;
                console.log('Completed');
            }
        }
    } 
});

var sampleObserver = new SampleObserver();

var source = sampleObserver.asObserver();

console.log(source === sampleObserver);
// => false

Example

Last updated