withBuffer<T> method

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

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

Implementation

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