Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.TestRebaseClient.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel TestRebaseClient class
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Rebase client used for testing
9 *
10 * @class
11 * @extends ve.dm.RebaseClient
12 *
13 * @constructor
14 * @param {ve.dm.TestRebaseServer} server Rebase server
15 * @param {Array} initialData Document
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 * Compact, potentially ambiguous summary of insertions/removals, disregarding location
33 *
34 * Example: insertion1insertion2-(removal3)insertion3/uncommittedinsertion4?/-(unsentremoval5)!
35 *
36 * Only insertions and removals are represented. An insertion is represented by the toString
37 * content inserted; a removal by '-(' then the toString content removed then ')'. The offset
38 * in the document of the insertion/removal is not represented at all. Consecutive transaction
39 * representations are concatenated.
40 *
41 * Therefore this format is ambiguous unless the test transactions are chosen very carefully.
42 * The summary 'abc' could represent any of the following changes (amongst others):
43 *
44 * - Append 'abc'
45 * - Append 'a', then append 'bc'
46 * - Append 'a' then prepend 'bc'
47 * - Append 'a' then prepend 'b' then append 'c'
48 *
49 * @param {ve.dm.Change} change The document history
50 * @param {number} [commitLength] The point above which the transactions are uncommitted
51 * @param {number} [sentLength] The point above which the transactions are unsent
52 * @return {string} Compact summary of the history
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 };