Debian Codegraph @

extensions/VisualEditor/lib/ve/collab/ve.dm.CollabTransportServer.js

raw symbols SCIP
1 /*!
2 * VisualEditor collab transport server class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 'use strict';
8
9 /**
10 * Transport server for Socket IO transport
11 *
12 * @constructor
13 * @param {number} startHeight Length of shared completeHistory
14 */
15 ve.dm.CollabTransportServer = function VeDmCollabTransportServer( startHeight ) {
16 const startTimestamp = Date.now();
17 this.startHeight = startHeight;
18 this.protocolServer = new ve.dm.ProtocolServer(
19 {
20 startHeight: startHeight,
21 // The server ID is arbitrary
22 serverId: 've-collab-server',
23 load: function () {
24 return Promise.resolve(
25 ve.dm.Change.static.deserialize( { transactions: [] } )
26 );
27 },
28 onNewChange: function () {
29 return Promise.resolve();
30 }
31 },
32 {
33 /* eslint-disable-next-line no-console */
34 logServerEvent: ( x ) => console.log( x ),
35 /* eslint-disable-next-line no-console */
36 logEvent: ( x ) => console.log( x ),
37 getRelativeTimestamp: () => Date.now() - startTimestamp
38 }
39 );
40 this.connections = [];
41 };
42
43 OO.initClass( ve.dm.CollabTransportServer );
44
45 /**
46 * Generic connection handler
47 *
48 * @param {Object} conn The connection
49 */
50 ve.dm.CollabTransportServer.prototype.onConnection = function ( conn ) {
51 const context = this.protocolServer.authenticate( 've-collab-doc', null, null ),
52 connections = this.connections,
53 server = this.protocolServer,
54 startHeight = this.startHeight;
55
56 connections.push( conn );
57
58 context.broadcast = function ( type, data ) {
59 const serialized = ve.collab.serialize( data );
60 connections.forEach( ( connection ) => {
61 connection.send( { type: type, data: serialized } );
62 } );
63 };
64 context.sendAuthor = function ( type, data ) {
65 const serialized = ve.collab.serialize( data );
66 conn.send( { type: type, data: serialized } );
67 };
68 conn.on( 'data', ( data ) => {
69 const type = data.type;
70 if ( type === 'submitChange' ) {
71 server.onSubmitChange( context, data.data );
72 } else if ( type === 'changeAuthor' ) {
73 server.onChangeAuthor( context, data.data );
74 } else if ( type === 'disconnect' ) {
75 server.onDisconnect( context, data.data );
76 } else if ( type === 'logEvent' ) {
77 // do nothing
78 } else {
79 throw new Error( 'Unknown type "' + type + '"' );
80 }
81 } );
82 conn.on( 'open', () => {
83 server.welcomeClient(
84 context,
85 startHeight,
86 ( authorId ) => ve.msg( 'visualeditor-collab-user-placeholder', authorId )
87 );
88 } );
89 };