until method
Waits until the reactive value satisfies a predicate condition.
This method creates an effect that monitors the reactive value and
completes the returned future when the predicate returns true for
the current value. The effect is automatically disposed when the
future completes or is cancelled.
Behavior:
- The effect runs immediately and whenever the value changes
- The future completes with the value that satisfied the predicate
- The effect is automatically cleaned up when the future completes
Parameters:
predicate: A function that returnstruewhen the condition is met
Returns: A Future that completes with the value when the predicate is satisfied
Example:
final count = Signal(0);
// Wait until count reaches 5
final future = count.until((value) => value >= 5);
count.value = 1; // Still waiting
count.value = 3; // Still waiting
count.value = 5; // Future completes with value 5
final result = await future; // result is 5
Example with async/await:
final isLoading = Signal(true);
// Wait until loading completes
final data = await isLoading.until((value) => !value);
print('Loading finished');
Implementation
Future<T> until(bool Function(T value) predicate) {
final completer = Completer<T>();
final effect = Effect(() {
if (predicate(value)) {
completer.complete(value);
}
});
completer.future.whenComplete(effect.dispose);
return completer.future;
}