waitUntilRunnable method

Future<void> waitUntilRunnable(
  1. QueryClient client,
  2. Mutation mutation
)

Waits until mutation can run under scope and network constraints.

Implementation

Future<void> waitUntilRunnable(
  QueryClient client,
  Mutation<dynamic, dynamic, dynamic> mutation,
) async {
  bool ready() =>
      mutation._destroyed ||
      (canRun(mutation) &&
          (mutation.options.networkMode != QueryNetworkMode.online ||
              client.onlineManager.isOnline));
  if (ready()) {
    if (mutation._destroyed) throw const MutationCancelledException();
    return;
  }
  final completer = Completer<void>();
  void check([Object? _]) {
    if (ready() && !completer.isCompleted) completer.complete();
  }

  final removeCache = subscribe((_) => check());
  final removeOnline = client.onlineManager.subscribe(check);
  try {
    await Future.any<void>(<Future<void>>[
      completer.future,
      mutation._whenDestroyed,
    ]);
    if (mutation._destroyed) throw const MutationCancelledException();
  } finally {
    removeCache();
    removeOnline();
  }
}