Skip to content

Instantly share code, notes, and snippets.

@vugar005
Created July 12, 2021 13:35
Show Gist options
  • Save vugar005/c3470cc32491dd78b244b88fa097370d to your computer and use it in GitHub Desktop.
Save vugar005/c3470cc32491dd78b244b88fa097370d to your computer and use it in GitHub Desktop.
Debug RxJS events with Redux DevTools
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { DebugStreamActionType } from './debug-stream.constants';
import { DevToolsEvent } from './dev-tools';
/**
* Shows log on dev mode.
* @usageNotes
* @params message => message to log. Should be [Component Name] Event Name
* Example : of(100).pipe(debugStream('[Component Name] Event name'))
*/
/** TODO: unable to mock environment to test environment condition */
/* istanbul ignore next */
export const debugStream = (action: string) => (source: Observable<any>) =>
source.pipe(
tap({
next: (value) => {
logEvent(action, DebugStreamActionType.NEXT, value);
},
error: (value) => {
logEvent(action, DebugStreamActionType.ERROR, value);
},
complete: () => {
logEvent(action, DebugStreamActionType.COMPLETE);
}
})
);
function logEvent(action: string, actionType: DebugStreamActionType, value?: any): void {
if (!(window as any).DEBUG_STREAM) {
return;
}
const event: CustomEvent = new CustomEvent(DevToolsEvent.DISPATCH, {
detail: {
action,
actionType,
value,
},
});
document.dispatchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment