Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapped the update processing of AsyncSignal.value in a batch #219

Merged
merged 1 commit into from Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
);
}
}