replaceElements method

ResolvedScene replaceElements(
  1. int revision,
  2. Iterable<ResolvedElement> replacements, {
  3. FrameCollapseProjection? collapseProjection,
})

Publishes stable-identity replacements without sorting or scanning the scene. The scene owns both paint and source order; callers cannot supply indexes.

Implementation

ResolvedScene replaceElements(
  int revision,
  Iterable<ResolvedElement> replacements, {
  FrameCollapseProjection? collapseProjection,
}) {
  final paintUpdates = <int, ResolvedElement>{};
  final sourceUpdates = <int, ResolvedElement>{};
  final hiddenUpdates = <int, ResolvedElement>{};
  final replacementIds = <String>{};
  for (final element in replacements) {
    if (!replacementIds.add(element.id)) {
      throw ArgumentError('Scene replacements must have unique identities');
    }
    final index = _elementIndexes[element.id];
    final hidden = _hiddenIndexes[element.id];
    if (index == null && hidden != null) {
      if (_hiddenElements[hidden].zIndex != element.zIndex) {
        throw ArgumentError('Scene replacements must preserve paint order');
      }
      hiddenUpdates[hidden] = element;
      continue;
    }
    if (index == null || elements[index].zIndex != element.zIndex) {
      throw ArgumentError(
        'Scene replacements must preserve visible identity and paint order',
      );
    }
    if (identical(elements[index], element)) continue;
    paintUpdates[index] = element;
    sourceUpdates[_paintOrder[index]] = element;
  }
  var boundsIndex = _boundsIndex;
  for (final update in paintUpdates.entries) {
    boundsIndex = boundsIndex!.replace(
      _paintOrder[update.key],
      update.value.bounds,
    );
  }
  final next = DiagramPersistentList<ResolvedElement>.snapshot(elements)
      .replaceAllAt(paintUpdates);
  return ResolvedScene._(
    _elementIndexes,
    DiagramPersistentList<ResolvedElement>.snapshot(_sourceElements)
        .replaceAllAt(sourceUpdates),
    _paintOrder,
    documentId: documentId,
    revision: revision,
    textLayoutRevision: textLayoutRevision,
    defaultFontFamily: defaultFontFamily,
    elements: next,
    boundsIndex: boundsIndex,
    spatialIndex: DiagramSpatialIndex.incremental(
      spatialIndex,
      next,
      changedIndexes: paintUpdates.keys,
    ),
    collapseProjection: collapseProjection ?? this.collapseProjection,
    hiddenElements: _hiddenElements.replaceAllAt(hiddenUpdates),
    hiddenIndexes: _hiddenIndexes,
  );
}