withBufferAsync<T> method

Future<T> withBufferAsync<T>(
  1. Buffer newBuffer,
  2. Future<T> action(), {
  3. bool clearBuffer = true,
})

Runs an async action using the provided buffer, restoring the previous buffer afterward.

Implementation

Future<T> withBufferAsync<T>(
  Buffer newBuffer,
  Future<T> Function() action, {
  bool clearBuffer = true,
}) async {
  final previousBuffer = buffer;
  final previousBlocks = List<Buffer>.from(_blockBuffers);
  buffer = newBuffer;
  _blockBuffers.clear();
  try {
    final result = await action();
    if (clearBuffer) {
      buffer.clear();
    }
    return result;
  } finally {
    buffer = previousBuffer;
    _blockBuffers
      ..clear()
      ..addAll(previousBlocks);
  }
}