Skip to content

Instantly share code, notes, and snippets.

@alexeden
Created July 12, 2019 18:07
Show Gist options
  • Select an option

  • Save alexeden/45187c0c5678f2d878b44e5a00daf65f to your computer and use it in GitHub Desktop.

Select an option

Save alexeden/45187c0c5678f2d878b44e5a00daf65f to your computer and use it in GitHub Desktop.
Tag RxJS Operator
import { Observable, Subscriber } from 'rxjs';
const nextCss = `background-color: cyan; color: #000000; font-weight: bold;`;
const errorCss = `background-color: #f64040; color: #ffffff; font-weight: bold;`;
const completeCss = `background-color: rgb(24, 255, 148); color: #000000; font-weight: bold;`;
export const tag = (tagText: string, stringify = false) => {
return <T>(source: Observable<T>) =>
new Observable((observer: Subscriber<T>) => {
let count = 0;
return source.subscribe({
next(x) {
console.log(`%c${tagText} #${++count} `, nextCss, stringify ? JSON.stringify(x, null, 2) : x);
observer.next(x);
},
error(err) {
console.log(`%c${tagText} error after #${count} emissions `, errorCss, err);
observer.error(err);
},
complete() {
console.log(`%c${tagText} completed after #${count} emissions `, completeCss);
observer.complete();
},
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment