Last active
October 28, 2015 19:18
-
-
Save mattpodwysocki/6e109bc1ac0f9ec87592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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