compose method

void compose(
  1. Delta change,
  2. ChangeSource source
)

Composes change Delta into this document.

Use this method with caution as it does not apply heuristic rules to the change.

It is callers responsibility to ensure that the change conforms to the document model semantics and can be composed with the current state of this document.

In case the change is invalid, behavior of this method is unspecified.

Implementation

void compose(Delta change, ChangeSource source) {
  _checkMutable();
  change.trim();
  assert(change.isNotEmpty);

  var offset = 0;
  final before = toDelta();
  change = _migrateDelta(change);
  for (final op in change.toList()) {
    final attributes =
        op.attributes != null ? NotusStyle.fromJson(op.attributes) : null;
    if (op.isInsert) {
      // Must normalize data before inserting into the document, makes sure
      // that any embedded objects are converted into EmbeddableObject type.
      final data = _normalizeData(op.data);
      _root.insert(offset, data, attributes);
    } else if (op.isDelete) {
      _root.delete(offset, op.length);
    } else if (op.attributes != null) {
      _root.retain(offset, op.length, attributes!);
    }
    if (!op.isDelete) offset += op.length;
  }
  _delta = _delta.compose(change);

  if (_delta != _root.toDelta()) {
    throw StateError('Compose produced inconsistent results. '
        'This is likely due to a bug in the library. Tried to compose change $change from $source.');
  }
  _controller.add(NotusChange(before, change, source));
}