run method

Future<bool> run(
  1. BuildContext context, {
  2. IFastErrorReporter? errorReporter,
})
inherited

Implementation

Future<bool> run(
  BuildContext context, {
  IFastErrorReporter? errorReporter,
}) {
  _logger.debug('Job started: $debugLabel');

  final completer = Completer<bool>();
  var operationAsync = initialize(context, errorReporter: errorReporter);

  if (!requestUserInteraction) {
    operationAsync = operationAsync.timeout(timeout);
  } else {
    // Wait for the next frame to ensure that
    // the next job can request a user interaction.
    operationAsync = operationAsync.then((value) {
      final userInteractionCompleter = Completer<void>();

      WidgetsBinding.instance.scheduleFrameCallback((_) {
        userInteractionCompleter.complete();
      });

      return userInteractionCompleter.future;
    });
  }

  final blocInitializationOperation = CancelableOperation.fromFuture(
    operationAsync,
  );

  operationAsync.catchError((dynamic error, StackTrace? stackTrace) {
    _logger.error('Job failed: $debugLabel => $error', stackTrace);
    blocInitializationOperation.cancel();

    if (blockStartupOnFailure) {
      completer.completeError(_transformError(error, stackTrace), stackTrace);
    } else {
      completer.complete(true);
    }
  }).whenComplete(() {
    if (!completer.isCompleted) {
      _logger.debug('Job completed: $debugLabel');
      completer.complete(true);
    }
  });

  return completer.future;
}