until method
Waits until the value satisfies a condition.
Creates an effect that monitors the value and completes when the
predicate returns true. The effect is automatically disposed.
Parameters:
predicate: Function that returnstruewhen condition is met
Returns: A Future that completes with the value when condition is satisfied
Example:
final count = Signal(0);
final result = await count.until((value) => value >= 5);
// result is 5
Implementation
Future<T> until(bool Function(T value) predicate) {
if (predicate(value)) {
return Future.value(value);
}
final completer = Completer<T>();
final effect = Effect.lazy(() {
if (completer.isCompleted) {
return;
}
if (predicate(value)) {
completer.complete(value);
}
});
trackWithEffect(() => value, effect);
completer.future.whenComplete(effect.dispose);
return completer.future;
}