when<R> method

R when<R>({
  1. required R unauthenticated(),
  2. required R authenticating(),
  3. required R authenticated(
    1. T user
    ),
  4. required R error(
    1. Object error,
    2. StackTrace stackTrace
    ),
})

Exhaustively pattern-matches all four auth states.

Implementation

R when<R>({
  required R Function() unauthenticated,
  required R Function() authenticating,
  required R Function(T user) authenticated,
  required R Function(Object error, StackTrace stackTrace) error,
}) {
  final onUnauth = unauthenticated;
  final onAuthng = authenticating;
  final onAuthed = authenticated;
  final onError = error;

  return switch (this) {
    AuthUnauthenticated<T>() => onUnauth(),
    AuthAuthenticating<T>() => onAuthng(),
    AuthAuthenticated<T>(:final user) => onAuthed(user),
    AuthError<T>(:final error, :final stackTrace) => onError(
      error,
      stackTrace,
    ),
  };
}