diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-06-05 06:00:07 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-06-05 06:00:07 +0400 |
commit | aadfbe7276641294df18c4371d5b9cced2bb905e (patch) | |
tree | 52ff29de423a48504c467f721037544cbbfd91d6 | |
parent | c320adc2557fb22223bfcad58acb6867da65d7be (diff) |
add destroy()
-rw-r--r-- | index.js | 41 | ||||
-rw-r--r-- | package.json | 1 |
2 files changed, 42 insertions, 0 deletions
@@ -11,6 +11,7 @@ var http = require('http') var inherits = require('inherits') var mime = require('mime') var once = require('once') +var parallel = require('run-parallel') var pump = require('pump') var rangeParser = require('range-parser') var url = require('url') @@ -42,6 +43,20 @@ function WebTorrent (opts) { }) } +/** + * Add a new torrent to the client. `torrentId` can be one of: + * + * - a magnet uri (utf8 string) + * - a torrent file (buffer) + * - an info hash (hex string or buffer) + * - an http/https url to a .torrent file (string) + * - a filesystem path to a .torrent file (string) + * + * @override + * @param {string|Buffer} torrentId torrent (choose from above list) + * @param {Object} opts optional torrent-specific options + * @param {function=} cb called when the torrent is ready and has metadata + */ WebTorrent.prototype.add = function (torrentId, opts, cb) { var self = this if (!self.ready) { @@ -101,6 +116,32 @@ WebTorrent.prototype.add = function (torrentId, opts, cb) { return self } +/** + * Destroy the client, including all torrents and connections to peers. + * + * @override + * @param {function} cb + */ +WebTorrent.prototype.destroy = function (cb) { + var self = this + + var tasks = [ + Client.prototype.destroy.bind(self) + ] + + if (self.server) { + tasks.push(function (cb) { + try { + self.server.close(cb) + } catch (err) { + cb(null) // ignore error, server was either already closed / not yet listening + } + }) + } + + parallel(tasks, cb) +} + WebTorrent.prototype._onTorrent = function (torrent) { var self = this diff --git a/package.json b/package.json index 163cc09..41a948c 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "random-access-file": "^0.3.1", "range-parser": "^1.0.0", "rimraf": "^2.2.5", + "run-parallel": "^1.0.0", "thunky": "^0.1.0", "windows-no-runnable": "~0.0.6" }, |