-
Notifications
You must be signed in to change notification settings - Fork 70
/
debug.ts
29 lines (27 loc) · 965 Bytes
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { tap } from 'rxjs/operators';
export type ExtraNotifications = {
subscribe?: boolean;
unsubscribe?: boolean;
finalize?: boolean;
};
//INSPIRED BY @netbasal ARTICLE https://netbasal.com/creating-custom-operators-in-rxjs-32f052d69457
export function debug<T>(tag: string, extraNotifications?: ExtraNotifications) {
const formatNotif = (notif: string, data?: unknown) => [
new Date().toISOString(),
`[${tag}: ${notif}]`,
data,
];
return tap<T>({
next: (value) => console.log(...formatNotif('Next', value)),
error: (err) => console.error(...formatNotif('Error', err)),
complete: () => console.warn(...formatNotif('Completed')),
subscribe: () =>
extraNotifications?.subscribe &&
console.info(...formatNotif('Subscribed')),
unsubscribe: () =>
extraNotifications?.unsubscribe &&
console.info(...formatNotif(`Unsubscribed`)),
finalize: () =>
extraNotifications?.finalize && console.info(...formatNotif(`Finalized`)),
});
}