renderAndWait method

Future<RenderJobView> renderAndWait(
  1. ApiRenderRequest request, {
  2. void onUpdate(
    1. RenderJobView job
    )?,
  3. Duration pollInterval = const Duration(seconds: 1),
  4. Duration timeout = const Duration(minutes: 10),
  5. Future<void> wait(
    1. Duration
    ) = _realWait,
})

Creates a render and polls until it succeeds or fails, calling onUpdate with each status. Throws ApiClientException on failure or when timeout elapses first.

Implementation

Future<RenderJobView> renderAndWait(
  ApiRenderRequest request, {
  void Function(RenderJobView job)? onUpdate,
  Duration pollInterval = const Duration(seconds: 1),
  Duration timeout = const Duration(minutes: 10),
  Future<void> Function(Duration) wait = _realWait,
}) async {
  final created = await createRender(request);
  onUpdate?.call(created);
  var waited = Duration.zero;
  var latest = created;
  while (latest.isPending) {
    if (waited >= timeout) {
      throw const ApiClientException('Timed out waiting for the render to finish');
    }
    await wait(pollInterval);
    waited += pollInterval;
    latest = await getJob(created.id);
    onUpdate?.call(latest);
  }
  if (latest.isFailed) {
    throw ApiClientException(latest.error ?? 'Render failed');
  }
  return latest;
}