Created
June 26, 2017 11:41
-
-
Save amoerie/2b7db64c4a2f0e88c0aaa483af8d15f7 to your computer and use it in GitHub Desktop.
A nice, simple debug operator for RxJs 5.
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
import {Observable} from 'rxjs/Observable'; | |
const isProduction: boolean = process && process.env && process.env.NODE_ENV === "production"; | |
const debug = function <T>(this: Observable<T>, name: string) { | |
if (isProduction) | |
return this; | |
return this | |
.do({ | |
next: (value: T) => { | |
console.group("Next : " + name); | |
console.dir(value); | |
console.groupEnd(); | |
}, | |
error: (error: Error) => { | |
console.group("Error : " + name); | |
console.dir(error); | |
console.groupEnd(); | |
}, | |
complete: () => console.log("Complete : " + name) | |
}); | |
}; | |
Observable.prototype.debug = debug; | |
declare module 'rxjs/Observable' { | |
interface Observable<T> { | |
debug: typeof debug; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment