Last active
January 11, 2019 14:04
-
-
Save ThomasBurleson/b39185f330a75ebe869ed862b3e10f19 to your computer and use it in GitHub Desktop.
Using custom RxJS log() operator
This file contains hidden or 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
export function log(task: string) { | |
const announceCompleted = () => { | |
console.log(`${task} %ccomplete`, 'color: green'); | |
}; | |
return function<T>(source: Observable<T>) { | |
return source.pipe( | |
tap( | |
console.log, | |
console.error, | |
announceCompleted | |
) | |
); | |
}; | |
} |
Can also use long form:
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {
const subscription = source.subscribe(
(val:T) => {
console.log(val);
observer.next(val);
},
(err) => {
console.error(err);
observer.error(err);
},
() => {
console.log('%ccomplete', 'color: green');
observer.complete();
}
);
return () => {
subscription.unsubscribe();
};
});
return output;
};
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
See: