From ef38b87ef86a0ec60401fa71b7b0f74b9dcabe7d Mon Sep 17 00:00:00 2001 From: Perreal Guillaume <guillaume.perreal@irstea.fr> Date: Wed, 9 Oct 2019 16:07:19 +0200 Subject: [PATCH] Put back the spy operator. --- src/lib/spy/spy.operator.ts | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/lib/spy/spy.operator.ts diff --git a/src/lib/spy/spy.operator.ts b/src/lib/spy/spy.operator.ts new file mode 100644 index 0000000..bf3416d --- /dev/null +++ b/src/lib/spy/spy.operator.ts @@ -0,0 +1,51 @@ +import { MonoTypeOperatorFunction, Observable, ReplaySubject } from 'rxjs'; + +export type NotificationType = + | 'next' + | 'error' + | 'complete' + | 'subscribed' + | 'unsubscribed'; + +export interface Notification { + tag: string; + timestamp: number; + type: NotificationType; + payload?: any; +} + +export const notifications$ = new ReplaySubject<Notification>(20); + +const timeOrigin = Date.now(); + +export function spy<T>(tag = 'spy'): MonoTypeOperatorFunction<T> { + function log(type: NotificationType, payload?: any) { + notifications$.next({ + tag, + type, + payload, + timestamp: Date.now() - timeOrigin, + }); + } + + return (source$: Observable<T>): Observable<T> => + new Observable(observer => { + log('subscribed'); + + const sub = source$.subscribe({ + next: value => { + log('next', value); + observer.next(value); + }, + error: err => { + log('error', err); + observer.error(err); + }, + complete: () => log('complete'), + }); + + sub.add(() => log('unsubscribed')); + + return sub; + }); +} -- GitLab