Debian Codegraph @

extensions/VisualEditor/lib/ve/rebaser/src/dm/ve.dm.RebaseServer.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel rebase server class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 'use strict';
8
9 /**
10 * DataModel rebase server
11 *
12 * @class
13 *
14 * @constructor
15 * @param {Function} [logCallback]
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 /* Methods */
25
26 /**
27 * Get the state of a document by name.
28 *
29 * @param {string} doc Name of a document
30 * @return {ve.dm.RebaseDocState} Document state
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 * Update document history
41 *
42 * @param {string} doc Name of a document
43 * @param {number|null} authorId Author ID
44 * @param {ve.dm.Change} [newHistory] New history to append
45 * @param {Object} [authorDataChanges] New values for author data (modified keys only)
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 * Forget all history for a document
71 *
72 * @param {string} doc Name of a document
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 * Attempt to rebase and apply a change to a document.
84 *
85 * The change can be a new change, or a continued change. A continuated change means one that
86 * follows on immediately from the author's last submitted change, other than possibly being
87 * rebased onto some more recent committed history.
88 *
89 * @param {string} doc Document name
90 * @param {number} authorId Author ID
91 * @param {number} backtrack How many transactions are backtracked from the previous submission
92 * @param {ve.dm.Change} change Change to apply
93 * @return {ve.dm.Change} Accepted change (or initial segment thereof), as rebased
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 // Follow-on does not fully acknowledge outstanding conflicts: reject entirely
105 rejections = rejections - backtrack + change.transactions.length;
106 this.updateDocState( doc, authorId, null, { rejections: rejections } );
107 // FIXME argh this publishes an empty change, which is not what we want
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 // Remote has rebased some committed changes into its history since base was built.
114 // They are guaranteed to be equivalent to the start of base. See mathematical
115 // docs for proof (Cuius rei demonstrationem mirabilem sane deteximus hanc marginis
116 // exiguitas non caperet).
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 };