Created
July 12, 2021 13:35
-
-
Save vugar005/c3470cc32491dd78b244b88fa097370d to your computer and use it in GitHub Desktop.
Debug RxJS events with Redux DevTools
This file contains 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
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