next method
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);
}