next method

Operation next([
  1. num length = double.infinity
])

Consumes and returns next operation.

Optional length specifies maximum length of operation to return. Note that actual length of returned operation may be less than specified value.

Implementation

Operation next([num length = double.infinity]) {
  if (_modificationCount != delta._modificationCount) {
    throw ConcurrentModificationError(delta);
  }

  if (_index < delta.length) {
    final op = delta.elementAt(_index);
    final opKey = op.key;
    final opAttributes = op.attributes;
    final _currentOffset = _offset;
    final actualLength = math.min(op.length - _currentOffset, length);
    if (actualLength == op.length - _currentOffset) {
      _index++;
      _offset = 0;
    } else {
      _offset += actualLength;
    }
    final opData = op.isInsert && op.data is String
        ? (op.data as String).substring(_currentOffset as int, _currentOffset + (actualLength as int))
        : op.data;
    final opIsNotEmpty = opData is String ? opData.isNotEmpty : true; // embeds are never empty
    final opLength = opData is String ? opData.length : 1;
    final opActualLength = opIsNotEmpty ? opLength : actualLength as int;
    return Operation._(opKey, opActualLength, opData, opAttributes);
  }
  return Operation.retain(length as int);
}