Skip to content

Commit

Permalink
[major] Remove Sender inheritance from EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Oct 13, 2016
1 parent 98a9121 commit fc28f5a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 6 additions & 9 deletions lib/Sender.js
Expand Up @@ -6,24 +6,22 @@

'use strict';

const EventEmitter = require('events');
const ErrorCodes = require('./ErrorCodes');
const bufferUtil = require('./BufferUtil').BufferUtil;
const PerMessageDeflate = require('./PerMessageDeflate');

/**
* HyBi Sender implementation, Inherits from EventEmitter.
*/
class Sender extends EventEmitter {
class Sender {
constructor (socket, extensions) {
super();

this._socket = socket;
this.extensions = extensions || {};
this.firstFragment = true;
this.compress = false;
this.messageHandlers = [];
this.processing = false;
this.onerror = null;
}

/**
Expand All @@ -32,9 +30,8 @@ class Sender extends EventEmitter {
* @api public
*/
close (code, data, mask, cb) {
if (typeof code !== 'undefined') {
if (typeof code !== 'number' ||
!ErrorCodes.isValidErrorCode(code)) throw new Error('first argument must be a valid error code number');
if (code !== undefined && (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code))) {
throw new Error('first argument must be a valid error code number');
}
code = code || 1000;
var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0));
Expand Down Expand Up @@ -148,7 +145,7 @@ class Sender extends EventEmitter {
this.applyExtensions(data, finalFragment, this.compress, (err, data) => {
if (err) {
if (cb) cb(err);
else this.emit('error', err);
else this.onerror(err);
return;
}
this.frameAndSend(opcode, data, finalFragment, mask, compress, cb);
Expand Down Expand Up @@ -317,6 +314,6 @@ function sendFramedData (outputBuffer, data, cb) {
}
} catch (e) {
if (cb) cb(e);
else this.emit('error', e);
else this.onerror(e);
}
}
7 changes: 3 additions & 4 deletions lib/WebSocket.js
Expand Up @@ -850,10 +850,10 @@ function establishConnection (ReceiverClass, SenderClass, socket, upgradeHead) {

// finalize the client
this._sender = new SenderClass(socket, this.extensions);
this._sender.on('error', (error) => {
this._sender.onerror = (error) => {
this.close(1002, '');
this.emit('error', error);
});
};

this.readyState = WebSocket.OPEN;
this.emit('open');
Expand Down Expand Up @@ -939,8 +939,7 @@ function cleanupWebsocketResources (error) {
}

if (this._sender) {
this._sender.removeAllListeners();
this._sender = null;
this._sender = this._sender.onerror = null;
}

if (this._receiver) {
Expand Down

0 comments on commit fc28f5a

Please sign in to comment.