Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Added hook for the Document#applyOperation.
Browse files Browse the repository at this point in the history
ma2ciek committed Apr 4, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 55197d0 commit e0145f2
Showing 2 changed files with 29 additions and 18 deletions.
43 changes: 27 additions & 16 deletions src/dev-utils/enableenginedebug.js
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ export default function enableEngineDebug( logger ) {

enableLoggingTools();
enableDocumentTools();
enableReplayerTools();
}

return DebugPlugin;
@@ -455,6 +456,32 @@ function enableLoggingTools() {
};
}

function enableReplayerTools() {
const _modelDocumentApplyOperation = ModelDocument.prototype.applyOperation;

ModelDocument.prototype.applyOperation = function( operation ) {
if ( !this._lastDelta ) {
this._appliedDeltas = [];
this._lastDelta = operation.delta;
} else if ( this._lastDelta !== operation.delta ) {
this._appliedDeltas.push( this._lastDelta.toJSON() );
this._lastDelta = operation.delta;
}

_modelDocumentApplyOperation.call( this, operation );
};

ModelDocument.prototype.getAppliedDeltas = function() {
const appliedDeltas = this._appliedDeltas.concat( this._lastDelta.toJSON() );

return appliedDeltas.map( JSON.stringify ).join( LOG_SEPARATOR );
};

ModelDocument.prototype.createReplayer = function( stringifiedDeltas ) {
return new DeltaReplayer( this, LOG_SEPARATOR, stringifiedDeltas );
};
}

function enableDocumentTools() {
const _modelDocumentApplyOperation = ModelDocument.prototype.applyOperation;

@@ -476,22 +503,6 @@ function enableDocumentTools() {
logDocument( this, version );
};

ModelDocument.prototype.initializeDebugging = function() {
this._appliedDeltas = [];
};

ModelDocument.prototype.addAppliedDelta = function( delta ) {
this._appliedDeltas.push( delta.toJSON() );
};

ModelDocument.prototype.getAppliedDeltas = function() {
return this._appliedDeltas.map( JSON.stringify ).join( LOG_SEPARATOR );
};

ModelDocument.prototype.createReplayer = function( stringifiedDeltas ) {
return new DeltaReplayer( this, LOG_SEPARATOR, stringifiedDeltas );
};

ViewDocument.prototype.log = function( version ) {
logDocument( this, version );
};
4 changes: 2 additions & 2 deletions tests/dev-utils/enableenginedebug.js
Original file line number Diff line number Diff line change
@@ -730,8 +730,8 @@ describe( 'debug tools', () => {
delta.addOperation( move );
delta.addOperation( remove );

modelDoc.initializeDebugging();
modelDoc.addAppliedDelta( delta );
modelDoc.applyOperation( move );
modelDoc.applyOperation( remove );

const stringifiedDeltas = modelDoc.getAppliedDeltas();

0 comments on commit e0145f2

Please sign in to comment.