Skip to content

Commit

Permalink
Merge pull request #219 from rizumita/async-signal-value-batch-update
Browse files Browse the repository at this point in the history
Wrapped the update processing of AsyncSignal.value in a batch
  • Loading branch information
rodydavis committed Mar 27, 2024
2 parents dd1d3f4 + 1bb7243 commit 12359e8
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 12359e8

Please sign in to comment.