Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ For example extensions, see the following:

[Class] - Represents a collection of WebRTC peers on which signaling operations are possible.

#### 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

[Function] - takes `name` (a string), and `res` (a http.Response object). Creates a representation of the peer for signaling. __Returns__ a `Number` that shall be used as a unique id for the peer.
Expand Down
16 changes: 15 additions & 1 deletion lib/peer-list.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
const Emitter = require('events').EventEmitter
const Peer = require('./peer')

module.exports = class PeerList {
module.exports = class PeerList extends Emitter {
constructor() {
super()

this._peers = {}
this._nextPeerId = 1
}

addPeer(name, res) {
this.emit('addPeer:pre', name)

const peer = new Peer(name, this._nextPeerId)

peer.res = res

this.emit('addPeer', peer)
this._peers[peer.id] = peer
this._nextPeerId += 1

this.emit('addPeer:post', peer)

return peer.id
}

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)
}

}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webrtc-signal-http",
"version": "1.4.0",
"version": "1.5.0",
"description": "opinionated webrtc signal provider using http as a protocol",
"main": "lib/index.js",
"directories": {
Expand Down
69 changes: 69 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,75 @@ describe('webrtc-signal-http', () => {
assert.equal(Object.keys(internalMap), 0)
})

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', () => {
const expectedSocket = {obj: true}
const expectedSocket2 = {obj: false}
Expand Down