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. Also, if the property is 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.
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) {
bool removeIt = true;
if (predicate == null) {
bool ifClosed = _closeTimerFutureStream(value);
if (!ifClosed) removeIt = false;
}
//
// A predicate was provided,
else {
var removeIt = predicate(key: key, value: value);
if (removeIt) _closeTimerFutureStream(value);
}
if (removeIt) keysToRemove.add(key);
}
// After the iteration, remove all keys at the same time.
keysToRemove.forEach((key) => _props.remove(key));
}