refresh method
Cancels the previous load and publishes only this generation's result.
Implementation
Future<void> refresh() async {
if (_disposed) throw StateError('Resource disposed.');
_current?.cancel('Superseded by a newer read.');
final token = CancellationToken();
_current = token;
final generation = ++_generation;
_publish(ResourceState<T>(data: _state.data, isLoading: true));
try {
final data = await token.race(Future<T>.sync(() => load(token)));
if (_disposed || generation != _generation) return;
_publish(ResourceState<T>(data: data));
} catch (error, stack) {
if (_disposed || generation != _generation) return;
_publish(
ResourceState<T>(data: _state.data, error: error, stackTrace: stack));
} finally {
token.dispose();
}
}