Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.RebaseClient.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel rebase client class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel rebase client
9 *
10 * @class
11 */
12 ve.dm.RebaseClient = function VeDmRebaseClient() {
13 /**
14 * @property {number} authorId Author ID
15 */
16 this.authorId = null;
17
18 /**
19 * @property {number} commitLength Offset up to which we know we have no differences with the server
20 */
21 this.commitLength = 0;
22
23 /**
24 * @property {number} sentLength Offset up to which we have no unsent changes
25 */
26 this.sentLength = 0;
27
28 /**
29 * @property {number} backtrack Number of transactions backtracked (i.e. rejected) since the last send
30 */
31 this.backtrack = 0;
32 };
33
34 /* Inheritance */
35
36 OO.initClass( ve.dm.RebaseClient );
37
38 /* Abstract methods */
39
40 /**
41 * @abstract
42 * @param {number} start Start point for the change
43 * @param {boolean} toSubmit If true, mark current selection as sent
44 * @return {ve.dm.Change} The change since start in the client's local history
45 */
46 ve.dm.RebaseClient.prototype.getChangeSince = null;
47
48 /**
49 * @abstract
50 * @param {number} backtrack Number of rejected changes backtracked immediately before this change
51 * @param {ve.dm.Change} change The change to send
52 */
53 ve.dm.RebaseClient.prototype.sendChange = null;
54
55 /**
56 * Apply a change to the surface, and add it to the history
57 *
58 * @abstract
59 * @param {ve.dm.Change} change The change to apply
60 */
61 ve.dm.RebaseClient.prototype.applyChange = null;
62
63 /**
64 * Unapply a change from the surface, and remove it from the history
65 *
66 * @abstract
67 * @param {ve.dm.Change} change The change to unapply
68 */
69 ve.dm.RebaseClient.prototype.unapplyChange = null;
70
71 /**
72 * Add a change to history, without applying it to the surface
73 *
74 * @abstract
75 * @param {ve.dm.Change} change The change to add
76 */
77 ve.dm.RebaseClient.prototype.addToHistory = null;
78
79 /**
80 * Remove a change from history, without unapplying it to the surface
81 *
82 * @abstract
83 * @param {ve.dm.Change} change The change to remove
84 */
85 ve.dm.RebaseClient.prototype.removeFromHistory = null;
86
87 /**
88 * Add an event to the log. Default implementation does nothing; subclasses should override this
89 * if they want to collect logs.
90 *
91 * @param {Object} event Event data
92 */
93 ve.dm.RebaseClient.prototype.logEvent = function () {};
94
95 /* Methods */
96
97 /**
98 * @return {number} Author ID
99 */
100 ve.dm.RebaseClient.prototype.getAuthorId = function () {
101 return this.authorId;
102 };
103
104 /**
105 * @param {number} authorId Author ID
106 */
107 ve.dm.RebaseClient.prototype.setAuthorId = function ( authorId ) {
108 this.authorId = authorId;
109 };
110
111 /**
112 * Submit all outstanding changes
113 *
114 * This will submit all transactions that exist in local history but have not been broadcast
115 * by the server.
116 */
117 ve.dm.RebaseClient.prototype.submitChange = function () {
118 const change = this.getChangeSince( this.sentLength, true );
119 if ( change.isEmpty() ) {
120 return;
121 }
122 // logEvent before sendChange, for the case where the log entry is sent to the server
123 // over the same tunnel as the change, so that there's no way the server will log
124 // receiving the change before it receives the submitChange message.
125 this.logEvent( {
126 type: 'submitChange',
127 change: change,
128 backtrack: this.backtrack
129 } );
130 this.sendChange( this.backtrack, change );
131 this.backtrack = 0;
132 this.sentLength += change.getLength();
133 };
134
135 /**
136 * Accept a committed change from the server
137 *
138 * If the committed change is by the local author, then it is already applied to the document
139 * and at the correct point in history: just move the commitLength pointer.
140 *
141 * If the commited change is by a different author, then:
142 * - Rebase local uncommitted changes over the committed change
143 * - If there is a rejected tail, then apply its inverse to the document
144 * - Apply the rebase-transposed committed change to the document
145 * - Rewrite history to have the committed change followed by rebased uncommitted changes
146 *
147 * @param {ve.dm.Change} change The committed change from the server
148 */
149 ve.dm.RebaseClient.prototype.acceptChange = function ( change ) {
150 const authorId = change.firstAuthorId(),
151 logResult = {};
152 if ( !authorId ) {
153 return;
154 }
155
156 let result;
157 const unsent = this.getChangeSince( this.sentLength, false );
158 if (
159 authorId !== this.getAuthorId() ||
160 change.start + change.getLength() > this.sentLength
161 ) {
162 let uncommitted = this.getChangeSince( this.commitLength, false );
163 result = ve.dm.Change.static.rebaseUncommittedChange( change, uncommitted );
164 if ( result.rejected ) {
165 // Undo rejected tail, and mark unsent and backtracked if necessary
166 this.unapplyChange( result.rejected );
167 uncommitted = uncommitted.truncate( result.rejected.start - uncommitted.start );
168 if ( this.sentLength > result.rejected.start ) {
169 this.backtrack += this.sentLength - result.rejected.start;
170 }
171 this.sentLength = result.rejected.start;
172 }
173 // We are already right by definition about our own selection
174 delete result.transposedHistory.selections[ this.getAuthorId() ];
175 this.applyChange( result.transposedHistory );
176 // Rewrite history
177 this.removeFromHistory( result.transposedHistory );
178 this.removeFromHistory( uncommitted );
179 this.addToHistory( change );
180 this.addToHistory( result.rebased );
181
182 this.sentLength += change.getLength();
183 }
184 this.commitLength += change.getLength();
185
186 // Only log the result if it's "interesting", i.e. we rebased or rejected something of nonzero length
187 if ( result ) {
188 if ( result.rebased.getLength() > 0 || result.rejected ) {
189 logResult.rebased = result.rebased;
190 logResult.transposedHistory = result.transposedHistory;
191 }
192 if ( result.rejected ) {
193 logResult.rejected = result.rejected;
194 }
195 }
196 if ( unsent.getLength() > 0 ) {
197 logResult.unsent = unsent;
198 }
199 this.logEvent( ve.extendObject( {
200 type: 'acceptChange',
201 authorId: authorId,
202 change: [ change.start, change.getLength() ]
203 }, logResult ) );
204 };