Skip to content

Commit ef76b3f

Browse files
authored
feat(events): Support of paused client event (#411)
* feat: Added `paused` client event * fix(events): fixed 'invalid event' response on 'paused' request from client * fix(styles): fixed extra semicolon
1 parent a048097 commit ef76b3f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/common-node.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/
1111

1212
exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
1313
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
14-
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 }
14+
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
1515
exports.EVENT_IDS = {
1616
0: 'update',
1717
1: 'completed',
1818
2: 'started',
19-
3: 'stopped'
19+
3: 'stopped',
20+
4: 'paused'
2021
}
2122
exports.EVENT_NAMES = {
2223
update: 'update',
2324
completed: 'complete',
2425
started: 'start',
25-
stopped: 'stop'
26+
stopped: 'stop',
27+
paused: 'pause'
2628
}
2729

2830
/**

lib/server/swarm.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Swarm {
4747
self._onAnnounceCompleted(params, peer, id)
4848
} else if (params.event === 'update') {
4949
self._onAnnounceUpdate(params, peer, id)
50+
} else if (params.event === 'paused') {
51+
self._onAnnouncePaused(params, peer, id)
5052
} else {
5153
cb(new Error('invalid event'))
5254
return
@@ -132,6 +134,15 @@ class Swarm {
132134
this.peers.set(id, peer)
133135
}
134136

137+
_onAnnouncePaused (params, peer, id) {
138+
if (!peer) {
139+
debug('unexpected `paused` event from peer that is not in swarm')
140+
return this._onAnnounceStarted(params, peer, id) // treat as a start
141+
}
142+
143+
this._onAnnounceUpdate(params, peer, id)
144+
}
145+
135146
_getPeers (numwant, ownPeerId, isWebRTC) {
136147
const peers = []
137148
const ite = randomIterate(this.peers.keys)

0 commit comments

Comments
 (0)