dispose method
Dipose Resources
This method will dispose all the Stream Subscriptions and Sinks that were added to the StreamDisposable.
If there is already a dispose
action in progress, this method will
throw an exception
In the same way, if the disposable is already disposed, this method will throw an exception
className: Class name to appear in the debugPrint statement
Implementation
Future<void> dispose({String className = ""}) async {
if (_isDisposing) {
throw Exception("Already disposing");
}
if (isDisposed) {
throw Exception("Already disposed");
}
_isDisposing = true;
Future<void>? subscriptionFuture;
Future<void>? sinkFuture;
if (_streamsSubscriptions.isNotEmpty) {
subscriptionFuture =
Future.wait(_streamsSubscriptions.map((sub) => sub.cancel()));
}
if (_sinks.isNotEmpty) {
sinkFuture = Future.sync(() => _sinks.map((sink) => sink.close()));
}
return Future.wait([
subscriptionFuture ?? Future.value(null),
sinkFuture ?? Future.value(null),
]).then((_) {
_didDispose.complete();
_isDisposing = false;
});
}