Created
June 16, 2020 00:25
-
-
Save benoitjadinon/32b83ee07901e5ae1c80ff36433ef797 to your computer and use it in GitHub Desktop.
debugger breaks on any Stream event
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
extension StreamExtensions<T> on Stream<T> { | |
/// usage: | |
/// userStream | |
/// //.breakpoint() // to break on ALL events of the stream | |
/// .breakpoint(prefix:'user', whenKind(k) => k == EventKind.OnResume) | |
/// .listen(); | |
Stream<T> breakpoint({ | |
String prefix, | |
String message, | |
bool Function(EventKind kind) whenKind, | |
bool Function(StackTrace stacktrace) whenStack, | |
bool Function(T value) whenValue, | |
}){ | |
void _break(EventKind kind, String prefix, String message, StackTrace stacktrace, {T value}) { | |
if (!(prefix?.isNullOrEmpty ?? true)) { | |
prefix += ' '; | |
} | |
if (!(message?.isNullOrEmpty ?? true)) { | |
message = ' ' + message; | |
} | |
message = '${(prefix??'')}${EnumUtils.toNameString(kind)}$message : $value'; | |
final when = | |
(whenKind?.call(kind) ?? true) && | |
(whenStack?.call(stacktrace) ?? true) && | |
(whenValue?.call(value) ?? true); | |
debugger(when: when, message: message); | |
} | |
const kindsMap = { | |
Kind.OnData : EventKind.OnData, | |
Kind.OnDone : EventKind.OnDone, | |
Kind.OnError : EventKind.OnError, | |
}; | |
return doOnEach((Notification<T> notification) { // onData, onDone, onError | |
_break( | |
kindsMap[notification.kind], | |
prefix, | |
message, | |
notification.stackTrace ?? StackTrace.current, | |
value: notification.value, | |
); | |
}) | |
.doOnCancel(() => _break(EventKind.OnCancel, prefix, message, StackTrace.current)) | |
.doOnListen(() => _break(EventKind.OnListen, prefix, message, StackTrace.current)) | |
.doOnResume(() => _break(EventKind.OnResume, prefix, message, StackTrace.current)) | |
.doOnPause((_) => _break(EventKind.OnPause, prefix, message, StackTrace.current)) | |
; | |
} | |
} | |
enum EventKind { OnData, OnDone, OnError, OnCancel, OnListen, OnResume, OnPause } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment