| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.dm.RebaseServer = function VeDmRebaseServer( logCallback ) { |
| 18 |
this.stateForDoc = new Map(); |
| 19 |
this.logEvent = logCallback || function () {}; |
| 20 |
}; |
| 21 |
|
| 22 |
OO.initClass( ve.dm.RebaseServer ); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ve.dm.RebaseServer.prototype.getDocState = function ( doc ) { |
| 33 |
if ( !this.stateForDoc.has( doc ) ) { |
| 34 |
this.stateForDoc.set( doc, new ve.dm.RebaseDocState() ); |
| 35 |
} |
| 36 |
return this.stateForDoc.get( doc ); |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
ve.dm.RebaseServer.prototype.updateDocState = function updateDocState( doc, authorId, newHistory, authorDataChanges ) { |
| 48 |
const state = this.getDocState( doc ); |
| 49 |
if ( newHistory ) { |
| 50 |
state.history.push( newHistory ); |
| 51 |
} |
| 52 |
if ( !authorId ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
let authorData = state.authors.get( authorId ); |
| 56 |
if ( !authorData ) { |
| 57 |
authorData = state.constructor.static.newAuthorData(); |
| 58 |
state.authors.set( authorId, authorData ); |
| 59 |
} |
| 60 |
if ( authorDataChanges ) { |
| 61 |
for ( const key in authorData ) { |
| 62 |
if ( authorDataChanges[ key ] !== undefined ) { |
| 63 |
authorData[ key ] = authorDataChanges[ key ]; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
ve.dm.RebaseServer.prototype.clearDocState = function clearDocState( doc ) { |
| 75 |
const state = this.stateForDoc.get( doc ); |
| 76 |
if ( !state ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
state.clearHistory(); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ve.dm.RebaseServer.prototype.applyChange = function applyChange( doc, authorId, backtrack, change ) { |
| 96 |
const state = this.getDocState( doc ), |
| 97 |
authorData = state.authors.get( authorId ); |
| 98 |
|
| 99 |
let appliedChange; |
| 100 |
|
| 101 |
let base = ( authorData && authorData.continueBase ) || change.truncate( 0 ); |
| 102 |
let rejections = ( authorData && authorData.rejections ) || 0; |
| 103 |
if ( rejections > backtrack ) { |
| 104 |
|
| 105 |
rejections = rejections - backtrack + change.transactions.length; |
| 106 |
this.updateDocState( doc, authorId, null, { rejections: rejections } ); |
| 107 |
|
| 108 |
appliedChange = state.history.truncate( 0 ); |
| 109 |
} else if ( rejections < backtrack ) { |
| 110 |
throw new Error( 'Backtrack=' + backtrack + ' > ' + rejections + '=rejections' ); |
| 111 |
} else { |
| 112 |
if ( change.start > base.start ) { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
base = base.mostRecent( change.start ); |
| 118 |
} |
| 119 |
base = base.concat( state.history.mostRecent( base.start + base.getLength() ) ); |
| 120 |
|
| 121 |
const result = ve.dm.Change.static.rebaseUncommittedChange( base, change ); |
| 122 |
rejections = result.rejected ? result.rejected.getLength() : 0; |
| 123 |
this.updateDocState( doc, authorId, result.rebased, { |
| 124 |
rejections: rejections, |
| 125 |
continueBase: result.transposedHistory |
| 126 |
} ); |
| 127 |
appliedChange = result.rebased; |
| 128 |
} |
| 129 |
this.logEvent( { |
| 130 |
type: 'applyChange', |
| 131 |
doc: doc, |
| 132 |
authorId: authorId, |
| 133 |
incoming: change, |
| 134 |
applied: appliedChange, |
| 135 |
backtrack: backtrack, |
| 136 |
rejections: rejections |
| 137 |
} ); |
| 138 |
return appliedChange; |
| 139 |
}; |