Differ
Calculates the difference between two model states.
Receives operations that are to be applied on the model document. Marks parts of the model document tree which are changed and saves the state of these elements before the change. Then, it compares saved elements with the changed elements, after all changes are applied on the model document. Calculates the diff between saved elements and new ones and returns a change set.
Properties
isEmpty : booleanreadonlymodule:engine/model/differ~Differ#isEmptyInforms whether there are any changes buffered in
Differ._cachedChanges : null | Array<DifferItem>privatemodule:engine/model/differ~Differ#_cachedChangesFor efficiency purposes,
Differstores the change set returned by the differ aftergetChangescall. Cache is reset each time a new operation is buffered. If the cache has not been reset,getChangeswill return the cached value instead of calculating it again.This property stores those changes that did not take place in graveyard root.
_cachedChangesWithGraveyard : null | Array<DifferItem>privatemodule:engine/model/differ~Differ#_cachedChangesWithGraveyardFor efficiency purposes,
Differstores the change set returned by the differ after thegetChangescall. The cache is reset each time a new operation is buffered. If the cache has not been reset,getChangeswill return the cached value instead of calculating it again.This property stores all changes evaluated by
Differ, including those that took place in the graveyard._changeCount : numberprivatemodule:engine/model/differ~Differ#_changeCountStores the number of changes that were processed. Used to order the changes chronologically. It is important when changes are sorted.
_changedMarkers : Map<string, object>privatereadonlymodule:engine/model/differ~Differ#_changedMarkersA map that stores all changed markers.
The keys of the map are marker names.
The values of the map are objects with the following properties:
oldMarkerData,newMarkerData.
_changedRoots : Map<string, DifferItemRoot>privatereadonlymodule:engine/model/differ~Differ#_changedRootsA map that stores all roots that have been changed.
The keys are the names of the roots while value represents the changes.
_changesInElement : Map<ModelElement | ModelDocumentFragment, Array<ChangeItem>>privatereadonlymodule:engine/model/differ~Differ#_changesInElementA map that stores changes that happened in a given element.
The keys of the map are references to the model elements. The values of the map are arrays with changes that were done on this element.
_elementChildrenSnapshots : Map<ModelElement | ModelDocumentFragment, Array<DifferSnapshot>>privatereadonlymodule:engine/model/differ~Differ#_elementChildrenSnapshotsFor each element or document fragment inside which there was a change, it stores a snapshot of the child nodes list (an array of children snapshots that represent the state in the element / fragment before any change has happened).
This complements
_elementsSnapshots.See also
DifferSnapshot._elementState : Map<ModelElement, 'rename' | 'move' | 'refresh'>privatereadonlymodule:engine/model/differ~Differ#_elementStateKeeps the state for a given element, describing how the element was changed so far. It is used to evaluate the
actionproperty of diff items returned bygetChanges.Possible values, in the order from the lowest priority to the highest priority:
'refresh'- element was refreshed,'rename'- element was renamed,'move'- element was moved (or, usually, removed, that is moved to the graveyard).
Element that was refreshed, may change its state to
'rename'if it was later renamed, or to'move'if it was removed. But the element cannot change its state from'move'to'rename', or from'rename'to'refresh'.Only already existing elements are registered in
_elementState. If a new element was inserted as a result of a buffered operation, it is not be registered in_elementState._elementsSnapshots : Map<ModelNode, DifferSnapshot>privatereadonlymodule:engine/model/differ~Differ#_elementsSnapshotsStores a snapshot for these model nodes that might have changed.
This complements
_elementChildrenSnapshots.See also
DifferSnapshot._markerCollection : MarkerCollectionprivatereadonlymodule:engine/model/differ~Differ#_markerCollectionReference to the model's marker collection.
_refreshedItems : Set<ModelItem>privatemodule:engine/model/differ~Differ#_refreshedItemsSet of model items that were marked to get refreshed in
_refreshItem.
Static properties
_statesPriority : Array<undefined | string>privatereadonlystaticmodule:engine/model/differ~Differ._statesPriorityPriority of the element states. States on higher indexes of the array can overwrite states on the lower indexes.
Methods
constructor( markerCollection )module:engine/model/differ~Differ#constructorbufferMarkerChange( markerName, oldMarkerData, newMarkerData ) → voidmodule:engine/model/differ~Differ#bufferMarkerChangeBuffers a marker change.
Parameters
markerName : stringThe name of the marker that changed.
oldMarkerData : MarkerDataMarker data before the change.
newMarkerData : MarkerDataMarker data after the change.
Returns
void
bufferOperation( operationToBuffer ) → voidmodule:engine/model/differ~Differ#bufferOperationBuffers the given operation. An operation has to be buffered before it is executed.
Parameters
operationToBuffer : OperationAn operation to buffer.
Returns
void
getChangedMarkers() → Array<object>module:engine/model/differ~Differ#getChangedMarkersgetChangedRoots() → Array<DifferItemRoot>module:engine/model/differ~Differ#getChangedRootsReturns all roots that have changed (either were attached, or detached, or their attributes changed).
Returns
Array<DifferItemRoot>Diff between the old and the new roots state.
getChanges( options = { [options.includeChangesInGraveyard] } ) → Array<DifferItem>module:engine/model/differ~Differ#getChangesCalculates the diff between the old model tree state (the state before the first buffered operations since the last
resetcall) and the new model tree state (actual one). It should be called after all buffered operations are executed.The diff set is returned as an array of diff items, each describing a change done on the model. The items are sorted by the position on which the change happened. If a position is before another one, it will be on an earlier index in the diff set.
Note: Elements inside inserted element will not have a separate diff item, only the top most element change will be reported.
Because calculating the diff is a costly operation, the result is cached. If no new operation was buffered since the previous
getChangescall, the next call will return the cached value.Parameters
options : objectAdditional options.
Properties[ options.includeChangesInGraveyard ] : booleanIf set to
true, also changes that happened in the graveyard root will be returned. By default, changes in the graveyard root are not returned.
Defaults to
{}
Returns
Array<DifferItem>Diff between the old and the new model tree state.
getMarkersToAdd() → Array<object>module:engine/model/differ~Differ#getMarkersToAddReturns all markers which should be added as a result of buffered changes.
Returns
Array<object>Markers to add. Each array item is an object containing the
nameandrangeproperties.
getMarkersToRemove() → Array<object>module:engine/model/differ~Differ#getMarkersToRemoveReturns all markers that should be removed as a result of buffered changes.
Returns
Array<object>Markers to remove. Each array item is an object containing the
nameandrangeproperties.
getRefreshedItems() → Set<ModelItem>module:engine/model/differ~Differ#getRefreshedItemshasDataChanges() → booleanmodule:engine/model/differ~Differ#hasDataChangesChecks whether some of the buffered changes affect the editor data.
Types of changes which affect the editor data:
- model structure changes,
- attribute changes,
- a root is added or detached,
- changes of markers which were defined as
affectsData, - changes of markers'
affectsDataproperty.
Returns
boolean
reset() → voidmodule:engine/model/differ~Differ#reset_bufferRootLoad( root ) → voidinternalmodule:engine/model/differ~Differ#_bufferRootLoadBuffers all the data related to given root like it was all just added to the editor.
Following changes are buffered:
- root is attached,
- all root content is inserted,
- all root attributes are added,
- all markers inside the root are added.
Parameters
root : ModelRootElement
Returns
void
_refreshItem( item ) → voidinternalmodule:engine/model/differ~Differ#_refreshItemMarks the given
itemin differ to be "refreshed". It means that the item will be marked as removed and inserted in the differ changes set, so it will be effectively re-converted when the differ changes are handled by a dispatcher.Parameters
item : ModelItemItem to refresh.
Returns
void
_bufferRootAttributeChange( rootName, key, oldValue, newValue ) → voidprivatemodule:engine/model/differ~Differ#_bufferRootAttributeChangeBuffers a root attribute change.
Parameters
rootName : stringkey : stringoldValue : unknownnewValue : unknown
Returns
void
_bufferRootStateChange( rootName, isAttached ) → voidprivatemodule:engine/model/differ~Differ#_bufferRootStateChangeBuffers the root state change after the root was attached or detached
Parameters
rootName : stringisAttached : boolean
Returns
void
_getAttributesDiff( range, oldAttributes, newAttributes ) → Array<object>privatemodule:engine/model/differ~Differ#_getAttributesDiffReturns an array of objects where each one is a single attribute change description.
Parameters
range : ModelRangeThe range where the change happened.
oldAttributes : Map<string, unknown>A map, map iterator or compatible object that contains attributes before the change.
newAttributes : Map<string, unknown>A map, map iterator or compatible object that contains attributes after the change.
Returns
Array<object>An array containing one or more diff items.
_getChangesForElement( element ) → Array<ChangeItem>privatemodule:engine/model/differ~Differ#_getChangesForElementGets an array of changes that have already been saved for a given element.
Parameters
element : ModelElement | ModelDocumentFragment
Returns
Array<ChangeItem>
_getDiffActionForNode( node, diffItemType ) → DifferItemActionprivatemodule:engine/model/differ~Differ#_getDiffActionForNodeReturns a value for
actionproperty for diff items returned bygetChanges. This method aims to return'rename'or'refresh'when it should, anddiffItemType("default action") in all other cases.It bases on a few factors:
- for text nodes, the method always returns
diffItemType, - for newly inserted element, the method returns
diffItemType, - if element state was not recorded, the method returns
diffItemType, - if state was recorded, and it was
'move'(default action), the method returnsdiffItemType, - finally, if state was
'refresh'or'rename', the method returns the state value.
Parameters
node : ModelNodediffItemType : 'insert' | 'remove'
Returns
- for text nodes, the method always returns
_getInsertDiff( parent, offset, action, elementSnapshot, [ elementSnapshotBefore ] ) → objectprivatemodule:engine/model/differ~Differ#_getInsertDiffReturns an object with a single insert change description.
Parameters
parent : ModelElement | ModelDocumentFragmentThe element in which the change happened.
offset : numberThe offset at which change happened.
action : DifferItemActionFurther specifies what kind of action led to generating this change.
elementSnapshot : DifferSnapshotSnapshot of the inserted node after changes.
[ elementSnapshotBefore ] : DifferSnapshotSnapshot of the inserted node before changes.
Returns
objectThe diff item.
_getRemoveDiff( parent, offset, action, elementSnapshot ) → objectprivatemodule:engine/model/differ~Differ#_getRemoveDiffReturns an object with a single remove change description.
Parameters
parent : ModelElement | ModelDocumentFragmentThe element in which change happened.
offset : numberThe offset at which change happened.
action : DifferItemActionFurther specifies what kind of action led to generating this change.
elementSnapshot : DifferSnapshotThe snapshot of the removed node before changes.
Returns
objectThe diff item.
_handleChange( inc, changes ) → voidprivatemodule:engine/model/differ~Differ#_handleChangeFor a given newly saved change, compares it with a change already done on the element and modifies the incoming change and/or the old change.
Parameters
inc : ChangeItemIncoming (new) change.
changes : Array<ChangeItem>An array containing all the changes done on that element.
Returns
void
_isInInsertedElement( element ) → booleanprivatemodule:engine/model/differ~Differ#_isInInsertedElementChecks whether given element or any of its parents is an element that is buffered as an inserted element.
Parameters
element : ModelElement | ModelDocumentFragment
Returns
boolean
_makeSnapshots( element ) → voidprivatemodule:engine/model/differ~Differ#_makeSnapshotsCreates and saves a snapshot for all children of the given element.
Parameters
element : ModelElement | ModelDocumentFragment
Returns
void
_markAttribute( item ) → voidprivatemodule:engine/model/differ~Differ#_markAttribute_markChange( parent, changeItem ) → voidprivatemodule:engine/model/differ~Differ#_markChangeSaves and handles a model change.
Parameters
parent : ModelElement | ModelDocumentFragmentchangeItem : ChangeItem
Returns
void
_markInsert( parent, offset, howMany ) → voidprivatemodule:engine/model/differ~Differ#_markInsertSaves and handles an insert change.
Parameters
parent : ModelElement | ModelDocumentFragmentoffset : numberhowMany : number
Returns
void
_markRemove( parent, offset, howMany ) → voidprivatemodule:engine/model/differ~Differ#_markRemoveSaves and handles a remove change.
Parameters
parent : ModelElement | ModelDocumentFragmentoffset : numberhowMany : number
Returns
void
_removeAllNestedChanges( parent, offset, howMany ) → voidprivatemodule:engine/model/differ~Differ#_removeAllNestedChangesRemoves deeply all buffered changes that are registered in elements from range specified by
parent,offsetandhowMany.Parameters
parent : ModelElement | ModelDocumentFragmentoffset : numberhowMany : number
Returns
void
_setElementState( node, state ) → voidprivatemodule:engine/model/differ~Differ#_setElementStateTries to set given state for given item.
This method does simple validation (it sets the state only for model elements, not for text proxy nodes). It also follows state setting rules, that is,
'refresh'cannot overwrite'rename', and'rename'cannot overwrite'move'.Parameters
node : ModelItemstate : 'rename' | 'move' | 'refresh'
Returns
void