batch method

  1. @override
Future<List<RunOutput>> batch(
  1. List<RunInput> inputs, {
  2. List<RunnableOptions>? options,
})
override

Batches the invocation of the Runnable on the given inputs.

If the underlying provider supports batching, this method will try to batch the calls to the provider. Otherwise, it will just call invoke on each input concurrently.

You can configure the concurrency limit by setting the concurrencyLimit field in the options parameter.

  • inputs - the inputs to invoke the Runnable on concurrently.
  • options - the options to use when invoking the Runnable. It can be:
    • null: the default options are used.
    • List with 1 element: the same options are used for all inputs.
    • List with the same length as the inputs: each input gets its own options.

Implementation

@override
Future<List<RunOutput>> batch(
  List<RunInput> inputs, {
  List<RunnableOptions>? options,
}) async {
  Object? firstError;
  for (final runnable in [mainRunnable, ...fallbacks]) {
    List<RunnableOptions>? currentOptions;
    if (firstError == null) {
      currentOptions = options;
    } else {
      final compatibleOptions =
          options?.map(runnable.getCompatibleOptions).toList(growable: false);
      final hasNullOptions =
          compatibleOptions?.any((o) => o == null) ?? false;
      if (!hasNullOptions) {
        currentOptions = compatibleOptions?.cast();
      }
    }

    try {
      return await runnable.batch(
        inputs,
        options: currentOptions,
      );
    } catch (e) {
      firstError ??= e;
    }
  }
  throw Exception('All runnables failed. First error: $firstError');
}