Created
September 24, 2020 03:29
-
-
Save apgapg/a520872c263b70bcfec52da0d7c221ec to your computer and use it in GitHub Desktop.
Extension on Behavior Subject to add data or error safely without explicitly checking for subject close
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 'package:rxdart/subjects.dart'; | |
extension BehaviorSubjectX<T> on BehaviorSubject<T> { | |
/// Add [data] only when stream is not closed | |
/// | |
/// No data is added if stream is closed | |
void addDataSafely(T data) { | |
if (!isClosed) { | |
return sink.add(data); | |
} else { | |
print('Adding data failed: Stream already closed!'); | |
} | |
} | |
/// Add [error] only when stream is not closed | |
/// | |
/// No data is added if stream is closed | |
void addErrorSafely(error) { | |
if (!isClosed) { | |
return sink.addError(error); | |
} else { | |
print('Adding error failed: Stream already closed!'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment