replayCommandsCancellable function

Future<void> replayCommandsCancellable(
  1. List<PdfRenderCommand> commands,
  2. PdfDevice device, {
  3. PdfCancellationToken? cancellation,
  4. int checkInterval = 1024,
})

replayCommands in cooperative chunks: every checkInterval commands it checks cancellation and yields to the event loop, so a message (e.g. a render worker's cancel port) can preempt a long replay mid-walk by throwing PdfCancelledException - the same scheme the interpreter's async walk uses. Soft-mask groups replay atomically inside their enclosing command.

Implementation

Future<void> replayCommandsCancellable(
    List<PdfRenderCommand> commands, PdfDevice device,
    {PdfCancellationToken? cancellation, int checkInterval = 1024}) async {
  for (var i = 0; i < commands.length; i += checkInterval) {
    if (cancellation != null) {
      // Yield first so a cancel that arrived during the previous chunk gets
      // its listener run before the next chunk starts.
      await Future<void>.delayed(Duration.zero);
      if (cancellation.cancelled) throw const PdfCancelledException();
    }
    final end = i + checkInterval < commands.length
        ? i + checkInterval
        : commands.length;
    replayCommands(commands, device, start: i, end: end);
  }
}