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
This commit is contained in:
Paul Sharypov 2022-06-01 18:23:38 +03:00 committed by GitHub
parent a048097ab4
commit ef76b3f3b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -11,18 +11,20 @@ exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/
exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 }
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
exports.EVENT_IDS = {
0: 'update',
1: 'completed',
2: 'started',
3: 'stopped'
3: 'stopped',
4: 'paused'
}
exports.EVENT_NAMES = {
update: 'update',
completed: 'complete',
started: 'start',
stopped: 'stop'
stopped: 'stop',
paused: 'pause'
}
/**

View File

@ -47,6 +47,8 @@ class Swarm {
self._onAnnounceCompleted(params, peer, id)
} else if (params.event === 'update') {
self._onAnnounceUpdate(params, peer, id)
} else if (params.event === 'paused') {
self._onAnnouncePaused(params, peer, id)
} else {
cb(new Error('invalid event'))
return
@ -132,6 +134,15 @@ class Swarm {
this.peers.set(id, peer)
}
_onAnnouncePaused (params, peer, id) {
if (!peer) {
debug('unexpected `paused` event from peer that is not in swarm')
return this._onAnnounceStarted(params, peer, id) // treat as a start
}
this._onAnnounceUpdate(params, peer, id)
}
_getPeers (numwant, ownPeerId, isWebRTC) {
const peers = []
const ite = randomIterate(this.peers.keys)