Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active October 28, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/6e109bc1ac0f9ec87592 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/6e109bc1ac0f9ec87592 to your computer and use it in GitHub Desktop.
const noop = () => { };
const throwError = (e) => { throw e; };
Observable.prototype.interceptOnNext = (onNext, thisArg) => {
const source = this;
return Observable.create(o => {
let i = 0;
return source.subscribe(
x => {
try {
return onNext.call(thisArg, x, i++, source);
} catch (e) {
o.onError(e);
}
o.onNext(x);
}
);
});
};
Observable.prototype.interceptOnError = (onError, thisArg) => {
const source = this;
return Observable.create(o => {
return source.subscribe(
noop,
err => {
try {
return onError.call(thisArg, err, source);
} catch (e) {
o.onError(e);
}
o.onError(err);
}
);
});
};
Observable.prototype.interceptOnCompleted = (onCompleted, thisArg) => {
const source = this;
return Observable.create(o => {
return source.subscribe(
noop,
throwError,
() => {
try {
return onCompleted.call(thisArg, source);
} catch (e) {
o.onError(e);
}
o.onCompleted();
}
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment