webrtc-signal-http
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -0,5 +1,8 @@ | ||
const Emitter = require('events').EventEmitter | ||
const Peer = require('./peer') | ||
module.exports = class PeerList { | ||
module.exports = class PeerList extends Emitter { | ||
constructor() { | ||
super() | ||
this._peers = {} | ||
@@ -10,2 +13,4 @@ this._nextPeerId = 1 | ||
addPeer(name, res) { | ||
this.emit('addPeer:pre', name) | ||
const peer = new Peer(name, this._nextPeerId) | ||
@@ -15,5 +20,8 @@ | ||
this.emit('addPeer', peer) | ||
this._peers[peer.id] = peer | ||
this._nextPeerId += 1 | ||
this.emit('addPeer:post', peer) | ||
return peer.id | ||
@@ -23,4 +31,10 @@ } | ||
removePeer(id) { | ||
this.emit('removePeer:pre', id) | ||
if (this._peers[id]) { | ||
const cpy = this._peers[id] | ||
this.emit('removePeer', cpy) | ||
delete this._peers[id] | ||
this.emit('removePeer:post', cpy) | ||
} | ||
@@ -27,0 +41,0 @@ |
{ | ||
"name": "webrtc-signal-http", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "opinionated webrtc signal provider using http as a protocol", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -155,2 +155,31 @@ # webrtc-signal-http | ||
#### events | ||
These events will be emitted from the instance, and can be caught with `on`, `once`, `off`, etc. For more information, see [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). | ||
##### addPeer:pre | ||
Fired just before a peer is added, with the argument `name` - it indicates the peer name. | ||
##### addPeer | ||
Fired when a peer is being added, with the argument `peer` - it is the fully formed peer object. | ||
##### addPeer:post | ||
Fired after a peer is added, with the argument `peer` - it is the fully formed peer object that has been inserted into the peer list. | ||
##### removePeer:pre | ||
Fired just before a peer is removed, with the argument `id` - it indicates the peer id. | ||
##### removePeer | ||
Fired when a peer is being removed, with the argument `peer` - it is the fully formed peer object. | ||
##### removePeer:post | ||
Fired after a peer is removed, with the argument `peer` - it is the fully formed peer object that has been removed from the peer list. | ||
#### addPeer | ||
@@ -157,0 +186,0 @@ |
@@ -236,2 +236,71 @@ const assert = require('assert') | ||
it('should emit addPeer:pre events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('addPeer:pre', (name) => { | ||
assert.ok(typeof name === 'string') | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
}) | ||
it('should emit addPeer events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('addPeer', (peer) => { | ||
assert.ok(peer instanceof Peer) | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
}) | ||
it('should emit addPeer:post events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('addPeer:post', (peer) => { | ||
assert.ok(peer instanceof Peer) | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
}) | ||
it('should emit removePeer:pre events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('removePeer:pre', (id) => { | ||
assert.ok(typeof id === 'number') | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
instance.removePeer(id) | ||
}) | ||
it('should emit removePeer events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('removePeer', (peer) => { | ||
assert.ok(peer instanceof Peer) | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
instance.removePeer(id) | ||
}) | ||
it('should emit removePeer:post events', (done) => { | ||
const instance = new PeerList() | ||
instance.once('removePeer:post', (peer) => { | ||
assert.ok(peer instanceof Peer) | ||
done() | ||
}) | ||
const id = instance.addPeer('test', {obj: true}) | ||
instance.removePeer(id) | ||
}) | ||
it('should support socket replacement', () => { | ||
@@ -238,0 +307,0 @@ const expectedSocket = {obj: true} |
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
2117366
526
270