-
Notifications
You must be signed in to change notification settings - Fork 80
fix: don't notify signals tracking AsyncSignal.future when transitioning to loading state #434
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
Open
Yegair
wants to merge
1
commit into
rodydavis:main
Choose a base branch
from
Yegair:fix/433-future-signal-executed-too-often
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import '../core/signals.dart'; | ||
| import '../mixins/event_sink.dart'; | ||
| import 'state.dart'; | ||
|
|
@@ -173,60 +171,77 @@ class AsyncSignal<T> extends Signal<AsyncState<T>> | |
| super.value, { | ||
| super.debugLabel, | ||
| super.autoDispose, | ||
| }) : _initialValue = value; | ||
| }) : _initialValue = value, | ||
| _completion = _AsyncCompletionSignal<T>( | ||
| initialValue: value, | ||
| debugLabel: debugLabel != null ? '${debugLabel}_completion' : null, | ||
| ); | ||
|
|
||
| @override | ||
| void dispose() { | ||
| _completion.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| final AsyncState<T> _initialValue; | ||
| bool _initialized = false; | ||
|
|
||
| /// Internal Completer for values | ||
| @internal | ||
| Completer<bool> completer = Completer<bool>(); | ||
| final _AsyncCompletionSignal<T> _completion; | ||
|
|
||
| /// Tracks the async completion of this signal. | ||
| /// | ||
| /// Notifies if the value of this signal changes to [AsyncData] or [AsyncError], | ||
| /// but not if it changes to [AsyncLoading]. | ||
| /// | ||
| /// Intended to be used for tracking dependencies across async gaps, | ||
| /// when awaiting the [future] of this signal. | ||
| /// | ||
| /// ```dart | ||
| /// computedFrom([someAsyncSignal.completion], (_) async { | ||
| /// await Future.delayed(const Duration(seconds: 1)); | ||
| /// return await someAsyncSignal.future; | ||
| /// }); | ||
| /// ``` | ||
| ReadonlySignal<Future<T>> get completion => _completion; | ||
|
|
||
| /// The future of the signal completer | ||
| Future<T> get future async { | ||
| value; | ||
| await completer.future; | ||
| return value.requireValue; | ||
| } | ||
| Future<T> get future => completion.value; | ||
|
|
||
| /// Returns true if the signal is completed an error or data | ||
| bool get isCompleted { | ||
| value; | ||
| return completer.isCompleted; | ||
| future; | ||
| return _completion.isCompleted; | ||
| } | ||
|
|
||
| @override | ||
| bool set(AsyncState<T> val, {bool force = false}) { | ||
| return batch(() { | ||
| _completion.setValue(val); | ||
| return super.set(val, force: force); | ||
| }); | ||
| } | ||
|
|
||
| /// Set the error with optional stackTrace to [AsyncError] | ||
| void setError(Object error, [StackTrace? stackTrace]) { | ||
| batch(() { | ||
| value = AsyncState.error(error, stackTrace); | ||
| if (completer.isCompleted) completer = Completer<bool>(); | ||
| completer.complete(true); | ||
| }); | ||
| set(AsyncState.error(error, stackTrace)); | ||
| } | ||
|
|
||
| /// Set the value to [AsyncData] | ||
| void setValue(T value) { | ||
| batch(() { | ||
| this.value = AsyncState.data(value); | ||
| if (completer.isCompleted) completer = Completer<bool>(); | ||
| completer.complete(true); | ||
| }); | ||
| set(AsyncState.data(value)); | ||
| } | ||
|
|
||
| /// Set the loading state to [AsyncLoading] | ||
| void setLoading([AsyncState<T>? state]) { | ||
| batch(() { | ||
| value = state ?? AsyncState.loading(); | ||
| completer = Completer<bool>(); | ||
| }); | ||
| set(state ?? AsyncState.loading()); | ||
| } | ||
|
|
||
| /// Reset the signal to the initial value | ||
| void reset([AsyncState<T>? value]) { | ||
| batch(() { | ||
| this.value = value ?? _initialValue; | ||
| super.value = value ?? _initialValue; | ||
| _initialized = false; | ||
| if (completer.isCompleted) completer = Completer<bool>(); | ||
| _completion.setValue(const AsyncLoading()); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -435,3 +450,69 @@ AsyncSignal<T> asyncSignal<T>( | |
| autoDispose: autoDispose, | ||
| ); | ||
| } | ||
|
|
||
| class _AsyncCompletionSignal<T> extends Signal<Future<T>> { | ||
| _AsyncCompletionSignal._( | ||
| Completer<T> completer, { | ||
| required super.debugLabel, | ||
| }) : _completer = completer, | ||
| super(_ignoreUncaughtErrors(completer.future)); | ||
|
|
||
| factory _AsyncCompletionSignal({ | ||
| required AsyncState<T> initialValue, | ||
| required String? debugLabel, | ||
| }) { | ||
| final completer = Completer<T>(); | ||
|
|
||
| switch (initialValue) { | ||
| case AsyncLoading(): | ||
| break; | ||
|
|
||
| case AsyncData(:final value): | ||
| completer.complete(value); | ||
| break; | ||
|
|
||
| case AsyncError(:final error, :final stackTrace): | ||
| completer.completeError(error, stackTrace); | ||
| break; | ||
| } | ||
|
|
||
| return _AsyncCompletionSignal._(completer, debugLabel: debugLabel); | ||
| } | ||
|
|
||
| Completer<T> _completer; | ||
|
|
||
| bool _initialized = false; | ||
| bool get isCompleted => _initialized && _completer.isCompleted; | ||
|
|
||
| void setValue(AsyncState<T> value) { | ||
| if (_completer.isCompleted) { | ||
| _completer = Completer<T>(); | ||
| this.value = _ignoreUncaughtErrors(_completer.future); | ||
| } | ||
|
|
||
| switch (value) { | ||
| case AsyncLoading(): | ||
| return; | ||
|
|
||
| case AsyncData(:final value): | ||
| _initialized = true; | ||
| _completer.complete(value); | ||
| return; | ||
|
|
||
| case AsyncError(:final error, :final stackTrace): | ||
| _initialized = true; | ||
| _completer.completeError(error, stackTrace); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| static Future<T> _ignoreUncaughtErrors<T>(Future<T> future) { | ||
| // Makes sure the future never reports an uncaught error to | ||
| // the current zone. Seems to be necessary to avoid uncaught | ||
| // errors to be reported when async signals are being used | ||
| // synchronously, i.e. when their .future is not used. | ||
| future.ignore(); | ||
| return future; | ||
| } | ||
|
Comment on lines
+510
to
+517
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I'm really not sure if this is a good idea, but IMHO we have no other choice. Otherwise users might see a lot of uncaught errors even when they are handled synchronously through |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _initialized flag is needed to make an existing test for simple AsyncSignals work, in case they are created in a completed state.
signals.dart/packages/signals_core/test/async/signal_test.dart
Lines 16 to 21 in 5ac7164
Didn't manage to figure out why an async signal is considered not completed after creating with data, so added this edge case in order to not break things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that sounds good!