next method

Operation next([
  1. int length = maxLength
])

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.

If this iterator reached the end of the Delta then returns a retain operation with its length set to maxLength.

Implementation

// TODO: Note that we used double.infinity as the default value for length here
//       but this can now cause a type error since operation length is
//       expected to be an int. Changing default length to [maxLength] is
//       a workaround to avoid breaking changes.
Operation next([int length = maxLength]) {
  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, _currentOffset + (actualLength))
        : 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;
    return Operation._(opKey, opActualLength, opData, opAttributes);
  }
  return Operation.retain(length);
}