until<T> static method

DVCancellation until<T>(
  1. DVLifecycleSignal<T> owner,
  2. bool ended(
    1. T state
    )
)

A token that cancels itself when owner reaches a state ended accepts -- a page disposing, a request completing, the app shutting down -- so a result is never delivered to something that is gone.

An owner already in such a state cancels the token at once.

Implementation

static DVCancellation until<T>(
  DVLifecycleSignal<T> owner,
  bool Function(T state) ended,
) {
  final DVCancellation token = DVCancellation();
  if (ended(owner.value)) {
    token.cancel('owner already ${_stateName(owner.value)}');
    return token;
  }
  token._subscription = owner.changes.listen((T state) {
    if (ended(state)) token.cancel('owner reached ${_stateName(state)}');
  });
  return token;
}