Skip to content

Commit

Permalink
Wrapped the update processing of AsyncSignal.value in a batch
Browse files Browse the repository at this point in the history
When AsyncSignal.value is updated, the value propagates to the monitoring side while the state of _completer is not synchronized, so it has been fixed to ensure that the state of _completer is synchronized.
  • Loading branch information
rizumita committed Mar 26, 2024
1 parent dd1d3f4 commit 1bb7243
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/signals_core/lib/src/async/signal.dart
Expand Up @@ -190,29 +190,37 @@ class AsyncSignal<T> extends ValueSignal<AsyncState<T>> {

/// Set the error with optional stackTrace to [AsyncError]
void setError(Object error, [StackTrace? stackTrace]) {
value = AsyncState.error(error, stackTrace);
if (_completer.isCompleted) _completer = Completer<bool>();
_completer.complete(true);
batch(() {
value = AsyncState.error(error, stackTrace);
if (_completer.isCompleted) _completer = Completer<bool>();
_completer.complete(true);
});
}

/// Set the value to [AsyncData]
void setValue(T value) {
this.value = AsyncState.data(value);
if (_completer.isCompleted) _completer = Completer<bool>();
_completer.complete(true);
batch(() {
this.value = AsyncState.data(value);
if (_completer.isCompleted) _completer = Completer<bool>();
_completer.complete(true);
});
}

/// Set the loading state to [AsyncLoading]
void setLoading([AsyncState<T>? state]) {
value = state ?? AsyncState.loading();
_completer = Completer<bool>();
batch(() {
value = state ?? AsyncState.loading();
_completer = Completer<bool>();
});
}

/// Reset the signal to the initial value
void reset([AsyncState<T>? value]) {
this.value = value ?? _initialValue;
_initialized = false;
if (_completer.isCompleted) _completer = Completer<bool>();
batch(() {
this.value = value ?? _initialValue;
_initialized = false;
if (_completer.isCompleted) _completer = Completer<bool>();
});
}

/// Initialize the signal
Expand Down Expand Up @@ -436,4 +444,4 @@ AsyncSignal<T> asyncSignal<T>(
equality: equality,
autoDispose: autoDispose,
);
}
}

0 comments on commit 1bb7243

Please sign in to comment.