| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.dm.TestRebaseClient = function VeDmTestRebaseClient( server, initialData ) { |
| 18 |
ve.dm.RebaseClient.apply( this ); |
| 19 |
|
| 20 |
this.server = server; |
| 21 |
this.incomingPointer = 0; |
| 22 |
this.outgoing = []; |
| 23 |
this.outgoingPointer = 0; |
| 24 |
this.doc = new ve.dm.Document( OO.copy( initialData ) ); |
| 25 |
this.surface = new ve.dm.Surface( this.doc ); |
| 26 |
}; |
| 27 |
|
| 28 |
OO.initClass( ve.dm.TestRebaseClient ); |
| 29 |
OO.mixinClass( ve.dm.TestRebaseClient, ve.dm.RebaseClient ); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
ve.dm.TestRebaseClient.static.historySummary = function ( change, commitLength, sentLength ) { |
| 55 |
const parts = []; |
| 56 |
if ( commitLength === undefined ) { |
| 57 |
commitLength = change.transactions.length; |
| 58 |
} |
| 59 |
if ( sentLength === undefined ) { |
| 60 |
sentLength = change.transactions.length; |
| 61 |
} |
| 62 |
const committed = change.transactions.slice( 0, commitLength ); |
| 63 |
const sent = change.transactions.slice( commitLength, sentLength ); |
| 64 |
const unsent = change.transactions.slice( sentLength ); |
| 65 |
|
| 66 |
function joinText( transactions ) { |
| 67 |
return transactions.map( ( transaction ) => transaction.operations.filter( ( op ) => op.type === 'replace' ).map( ( op ) => { |
| 68 |
const text = []; |
| 69 |
if ( op.remove.length ) { |
| 70 |
text.push( '-(' + op.remove.map( ( item ) => item[ 0 ] ).join( '' ) + ')' ); |
| 71 |
} |
| 72 |
if ( op.insert.length ) { |
| 73 |
text.push( op.insert.map( ( item ) => item[ 0 ] ).join( '' ) ); |
| 74 |
} |
| 75 |
return text.join( '' ); |
| 76 |
} ).join( '' ) ).join( '' ); |
| 77 |
} |
| 78 |
if ( committed.length ) { |
| 79 |
parts.push( joinText( committed ) ); |
| 80 |
} |
| 81 |
if ( sent.length ) { |
| 82 |
parts.push( joinText( sent ) + '?' ); |
| 83 |
} |
| 84 |
if ( unsent.length ) { |
| 85 |
parts.push( joinText( unsent ) + '!' ); |
| 86 |
} |
| 87 |
return parts.join( '/' ); |
| 88 |
}; |
| 89 |
|
| 90 |
ve.dm.TestRebaseClient.prototype.getHistorySummary = function () { |
| 91 |
return this.constructor.static.historySummary( this.getChangeSince( 0 ), this.commitLength, this.sentLength ); |
| 92 |
}; |
| 93 |
|
| 94 |
ve.dm.TestRebaseClient.prototype.getChangeSince = function ( start ) { |
| 95 |
return this.doc.getChangeSince( start ); |
| 96 |
}; |
| 97 |
|
| 98 |
ve.dm.TestRebaseClient.prototype.sendChange = function ( backtrack, change ) { |
| 99 |
this.outgoing.push( { backtrack: backtrack, change: change } ); |
| 100 |
}; |
| 101 |
|
| 102 |
ve.dm.TestRebaseClient.prototype.applyChange = function ( change ) { |
| 103 |
change.applyTo( this.surface ); |
| 104 |
}; |
| 105 |
|
| 106 |
ve.dm.TestRebaseClient.prototype.applyTransactions = function ( txs ) { |
| 107 |
const authorId = this.getAuthorId(); |
| 108 |
txs.forEach( ( transaction ) => { |
| 109 |
if ( transaction.authorId === null ) { |
| 110 |
transaction.authorId = authorId; |
| 111 |
} |
| 112 |
} ); |
| 113 |
this.surface.change( txs ); |
| 114 |
}; |
| 115 |
|
| 116 |
ve.dm.TestRebaseClient.prototype.unapplyChange = function ( change ) { |
| 117 |
change.unapplyTo( this.surface ); |
| 118 |
}; |
| 119 |
|
| 120 |
ve.dm.TestRebaseClient.prototype.addToHistory = function ( change ) { |
| 121 |
change.addToHistory( this.doc ); |
| 122 |
}; |
| 123 |
|
| 124 |
ve.dm.TestRebaseClient.prototype.removeFromHistory = function ( change ) { |
| 125 |
change.removeFromHistory( this.doc ); |
| 126 |
}; |
| 127 |
|
| 128 |
ve.dm.TestRebaseClient.prototype.deliverOne = function () { |
| 129 |
const item = this.outgoing[ this.outgoingPointer++ ]; |
| 130 |
const rebased = this.server.applyChange( |
| 131 |
ve.dm.TestRebaseServer.static.fakeDocName, |
| 132 |
this.getAuthorId(), |
| 133 |
item.backtrack, |
| 134 |
item.change |
| 135 |
); |
| 136 |
if ( !rebased.isEmpty() ) { |
| 137 |
this.server.incoming.push( rebased ); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
ve.dm.TestRebaseClient.prototype.receiveOne = function () { |
| 142 |
this.acceptChange( this.server.incoming[ this.incomingPointer++ ] ); |
| 143 |
}; |