disposeProps method
The disposeProps
method is used to clean up resources associated with the store's
properties, by stopping, closing, ignoring and removing timers, streams, sinks, and futures
that are saved as properties in the store.
In more detail: This method accepts an optional predicate function that takes a prop key
and a value
as an argument and returns a boolean.
-
If you don't provide a predicate function, all properties which are
Timer
,Future
, orStream
related will be closed/cancelled/ignored as appropriate, and then removed from the props. Other properties will not be removed. -
If the predicate function is provided and returns
true
for a given property, that property will be removed from the props and, if the property is also aTimer
,Future
, orStream
related, it will be closed/cancelled/ignored as appropriate. -
If the predicate function is provided and returns
false
for a given property, that property will not be removed from the props, and it will not be closed/cancelled/ignored.
This method is particularly useful when the store is being shut down, right before or after
you called the shutdown
method.
Example usage:
// Dispose of all Timers, Futures, Stream related etc.
store.disposeProps();
// Dispose only Timers.
store.disposeProps(({Object? key, Object? value}) => value is Timer);
Implementation
void disposeProps([bool Function({Object? key, Object? value})? predicate]) {
var keysToRemove = [];
for (var MapEntry(key: key, value: value) in _props.entries) {
final removeIt = predicate?.call(key: key, value: value) ?? true;
if (removeIt) {
final ifTimerFutureStream = _closeTimerFutureStream(value);
// Removes the key if the predicate was provided and returned true,
// or it was not provided but the value is Timer/Future/Stream.
if ((predicate != null) || ifTimerFutureStream) keysToRemove.add(key);
}
}
// After the iteration, remove all keys at the same time.
keysToRemove.forEach((key) => _props.remove(key));
}