promise-ws
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -343,10 +343,23 @@ 'use strict'; | ||
} else { | ||
const handleClose = closeEvent => { | ||
const handleCloseComplete = closeEvent => { | ||
/* istanbul ignore else */ | ||
if (closeEvent && closeEvent.code === 1006) { | ||
this.removeListener('message', handleClose); | ||
removeCloseListeners(); | ||
resolve(); | ||
} | ||
}; | ||
ws.addEventListener('close', handleClose); | ||
const handleCloseRejection = messageEvent => { | ||
const message = messageEvent && messageEvent.data; | ||
/* istanbul ignore else */ | ||
if (message === 'REJECT TO CLOSE') { | ||
removeCloseListeners(); | ||
reject(new Error(message)); | ||
} | ||
}; | ||
const removeCloseListeners = () => { | ||
this.removeListener('close', handleCloseComplete); | ||
this.removeListener('message', handleCloseRejection); | ||
}; | ||
ws.addEventListener('close', handleCloseComplete); | ||
ws.addEventListener('message', handleCloseRejection); | ||
} | ||
@@ -353,0 +366,0 @@ }); |
@@ -47,4 +47,2 @@ 'use strict'; | ||
constructor(options, callback) { | ||
var _this; | ||
if (!(0, _utils.isObject)(options)) { | ||
@@ -54,3 +52,3 @@ throw new Error('Missing option argument, expected an object.'); | ||
_this = super(); | ||
super(); | ||
@@ -66,3 +64,4 @@ const server = options.server, | ||
this._wss = wss; | ||
this._names = new Map(); | ||
this._emitter = new _events2.default(); | ||
this._closeEmitter = new _events2.default(); | ||
@@ -75,2 +74,5 @@ /* istanbul ignore next */ | ||
wss.on('connection', ws => { | ||
const client = new _Client2.default(ws); | ||
this.clients.set(ws, client); | ||
ws.isAlive = true; | ||
@@ -83,22 +85,23 @@ ws.on('pong', heartbeat); | ||
ws.on('message', (() => { | ||
var _ref = _asyncToGenerator(function* (message) { | ||
if (message === _utils.CLOSE_SIGNAL) { | ||
yield _this.close(); | ||
} | ||
}); | ||
ws.on('message', message => { | ||
if (message === _utils.CLOSE_SIGNAL) { | ||
this.emit('requestClose', client); | ||
this._closeEmitter.emit(_utils.CLOSE_SIGNAL, client); | ||
} | ||
}); | ||
return function (_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})()); | ||
const emitter = this._emitter; | ||
const client = new _Client2.default(ws); | ||
this.clients.set(ws, client); | ||
/* istanbul ignore next */ | ||
const types = emitter.eventNames() || Object.keys(emitter._events); | ||
this._names.forEach((listeners, name) => { | ||
listeners.forEach(listener => { | ||
client.addReply(name, listener); | ||
types.forEach(type => { | ||
emitter.listeners(type).forEach(listener => { | ||
client.addReply(type, listener); | ||
}); | ||
}); | ||
// this._closeEmitter.listeners(CLOSE_SIGNAL).forEach((listener) => { | ||
// client.requestClose(listener); | ||
// }); | ||
}); | ||
@@ -118,5 +121,3 @@ | ||
const forward = eventType => { | ||
wss.on(eventType, this.emit.bind(this, eventType)); | ||
}; | ||
const forward = type => wss.on(type, this.emit.bind(this, type)); | ||
@@ -142,16 +143,6 @@ this.on('error', _utils.noop); | ||
onReply(name, listener) { | ||
const listeners = function (names) { | ||
if (names.has(name)) { | ||
return names.get(name); | ||
} | ||
const newListeners = new Set(); | ||
names.set(name, newListeners); | ||
return newListeners; | ||
}(this._names); | ||
listeners.add(listener); | ||
onReply(type, listener) { | ||
this._emitter.addListener(type, listener); | ||
this._forEach(client => { | ||
client.onReply(name, listener); | ||
client.onReply(type, listener); | ||
}); | ||
@@ -161,2 +152,20 @@ return this; | ||
onReplyClose(listener) { | ||
var _this = this; | ||
this._closeEmitter.addListener(_utils.CLOSE_SIGNAL, (() => { | ||
var _ref = _asyncToGenerator(function* (client) { | ||
const shouldClose = yield listener(client); | ||
if (shouldClose) yield _this.close();else { | ||
client.ws().send('REJECT TO CLOSE'); | ||
} | ||
}); | ||
return function (_x) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})()); | ||
return this; | ||
} | ||
reply() { | ||
@@ -170,5 +179,4 @@ return this.onReply.apply(this, arguments); | ||
replyCount(name) { | ||
const names = this._names; | ||
return names.has(name) ? names.get(name).size : 0; | ||
replyCount(type) { | ||
return this._emitter.listenerCount(type); | ||
} | ||
@@ -198,14 +206,7 @@ | ||
removeReply(name, listener) { | ||
removeReply(type, listener) { | ||
/* istanbul ignore else */ | ||
if (this._names.has(name)) { | ||
const listeners = this._names.get(name); | ||
listeners.delete(listener); | ||
if (!listeners.size) { | ||
this._names.delete(name); | ||
} | ||
} | ||
this._emitter.removeListener(type, listener); | ||
this._forEach(client => { | ||
client.removeReply(name, listener); | ||
client.removeReply(type, listener); | ||
}); | ||
@@ -212,0 +213,0 @@ return this; |
{ | ||
"name": "promise-ws", | ||
"description": "A Promise-Based WebSocket implementation for Node.js", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"main": "lib/index", | ||
@@ -6,0 +6,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30257
517