update method
Runs the provided asynchronous function and updates the phase.
The phase is updated to AsyncWaiting when the callback starts, and to AsyncComplete or AsyncError according to success or failure when the callback ends.
Implementation
Future<AsyncPhase<T>> update(Future<T> Function() func) async {
value = value.copyAsWaiting();
final phase = await AsyncPhase.from(
func,
// Avoids using data as of this moment as fallback because
// it becomes stale if `value.data` is updated externally
// while the callback is executed.
fallbackData: null,
);
if (phase is AsyncError) {
value = phase.copyWith(data);
} else {
value = phase;
}
return value;
}