All files / src ve.FakePeer.js

94.23% Statements 49/52
50% Branches 5/10
91.66% Functions 11/12
94.11% Lines 48/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                  1x 2x 2x 2x 2x 2x         1x       1x       1x 3x 3x 3x 3x   3x     1x 3x 3x       1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 2x 2x 2x 2x     1x   1x 4x 4x 4x 4x   4x     1x 3x 3x       1x         1x 1x    
/*!
 * VisualEditor fake PeerJS class.
 *
 * For convenient debugging. Create two FakePeers in one browser window. Then a single
 * debugger can see all the communication within its call stack.
 *
 * @copyright See AUTHORS.txt
 */
 
ve.FakePeer = function veFakePeer() {
	this.id = 'p' + this.constructor.static.peers.length;
	this.constructor.static.peers.push( this );
	this.connections = [];
	this.handlers = new Map();
	Promise.resolve( this.id ).then( this.callHandlers.bind( this, 'open' ) );
};
 
/* Initialization */
 
OO.initClass( ve.FakePeer );
 
/* Static properties */
 
ve.FakePeer.static.peers = [];
 
/* Methods */
 
ve.FakePeer.prototype.on = function ( ev, f ) {
	let handlers = this.handlers.get( ev );
	Eif ( !handlers ) {
		handlers = [];
		this.handlers.set( ev, handlers );
	}
	handlers.push( f );
};
 
ve.FakePeer.prototype.callHandlers = function ( type, ...args ) {
	( this.handlers.get( type ) || [] ).forEach( ( handler ) => {
		handler( ...args );
	} );
};
 
ve.FakePeer.prototype.connect = function ( id ) {
	const peer = this.constructor.static.peers.find( ( peerI ) => peerI.id === id );
	Iif ( !peer ) {
		throw new Error( 'Unknown id: ' + id );
	}
	const thisConn = new ve.FakePeerConnection( peer.id + '-' + this.id, peer );
	const peerConn = new ve.FakePeerConnection( this.id + '-' + peer.id, this );
	thisConn.other = peerConn;
	peerConn.other = thisConn;
	this.connections.push( thisConn );
	peer.connections.push( peerConn );
	Promise.resolve( peerConn ).then( peer.callHandlers.bind( peer, 'connection' ) );
	Promise.resolve( thisConn.id ).then( thisConn.callHandlers.bind( thisConn, 'open' ) );
	Promise.resolve( peerConn.id ).then( peerConn.callHandlers.bind( peerConn, 'open' ) );
	return thisConn;
};
 
ve.FakePeerConnection = function VeFakePeerConnection( id, peer ) {
	this.id = id;
	this.peer = peer;
	this.other = null;
	this.handlers = new Map();
};
 
OO.initClass( ve.FakePeerConnection );
 
ve.FakePeerConnection.prototype.on = function ( ev, f ) {
	let handlers = this.handlers.get( ev );
	Eif ( !handlers ) {
		handlers = [];
		this.handlers.set( ev, handlers );
	}
	handlers.push( f );
};
 
ve.FakePeerConnection.prototype.callHandlers = function ( type, ...args ) {
	( this.handlers.get( type ) || [] ).forEach( ( handler ) => {
		handler( ...args );
	} );
};
 
ve.FakePeerConnection.prototype.setOther = function ( other ) {
	this.other = other;
	Promise.resolve( this.id ).then( this.callHandlers.bind( this, 'open' ) );
};
 
ve.FakePeerConnection.prototype.send = function ( data ) {
	Promise.resolve( data ).then( this.other.callHandlers.bind( this.other, 'data' ) );
};