move webtorrent client stuff to separate file
This commit is contained in:
@@ -3,16 +3,13 @@
|
|||||||
// var prettyBytes = require('pretty-bytes')
|
// var prettyBytes = require('pretty-bytes')
|
||||||
var airplay = require('airplay-js')
|
var airplay = require('airplay-js')
|
||||||
var chromecasts = require('chromecasts')()
|
var chromecasts = require('chromecasts')()
|
||||||
var createTorrent = require('create-torrent')
|
|
||||||
var dragDrop = require('drag-drop')
|
var dragDrop = require('drag-drop')
|
||||||
var electron = require('electron')
|
var electron = require('electron')
|
||||||
var networkAddress = require('network-address')
|
var networkAddress = require('network-address')
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
var throttle = require('throttleit')
|
var throttle = require('throttleit')
|
||||||
var thunky = require('thunky')
|
|
||||||
var torrentPoster = require('./lib/torrent-poster')
|
var torrentPoster = require('./lib/torrent-poster')
|
||||||
var WebTorrent = require('webtorrent')
|
var getClient = require('./lib/get-client')
|
||||||
var xhr = require('xhr')
|
|
||||||
|
|
||||||
var createElement = require('virtual-dom/create-element')
|
var createElement = require('virtual-dom/create-element')
|
||||||
var diff = require('virtual-dom/diff')
|
var diff = require('virtual-dom/diff')
|
||||||
@@ -22,14 +19,6 @@ var HEADER_HEIGHT = 38
|
|||||||
|
|
||||||
var App = require('./views/app')
|
var App = require('./views/app')
|
||||||
|
|
||||||
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
|
|
||||||
.map(function (arr) {
|
|
||||||
return arr[0]
|
|
||||||
})
|
|
||||||
.filter(function (url) {
|
|
||||||
return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0
|
|
||||||
})
|
|
||||||
|
|
||||||
var state = global.state = {
|
var state = global.state = {
|
||||||
torrents: [],
|
torrents: [],
|
||||||
server: null,
|
server: null,
|
||||||
@@ -47,7 +36,7 @@ var state = global.state = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentVDom, rootElement, getClient, updateThrottled
|
var currentVDom, rootElement, updateThrottled
|
||||||
|
|
||||||
function init () {
|
function init () {
|
||||||
currentVDom = App(state, dispatch)
|
currentVDom = App(state, dispatch)
|
||||||
@@ -56,19 +45,13 @@ function init () {
|
|||||||
|
|
||||||
updateThrottled = throttle(update, 250)
|
updateThrottled = throttle(update, 250)
|
||||||
|
|
||||||
getClient = thunky(function (cb) {
|
getClient(function (err, client) {
|
||||||
getRtcConfig('https://instant.io/rtcConfig', function (err, rtcConfig) {
|
if (err) return onError(err)
|
||||||
if (err) console.error(err)
|
global.client = client
|
||||||
var client = global.client = new WebTorrent({ rtcConfig: rtcConfig })
|
|
||||||
state.torrents = client.torrents // internal webtorrent array -- do not modify!
|
state.torrents = client.torrents // internal webtorrent array -- do not modify!
|
||||||
client.on('warning', onWarning)
|
client.on('warning', onWarning)
|
||||||
client.on('error', onError)
|
client.on('error', onError)
|
||||||
cb(null, client)
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
// For performance, create the client immediately
|
|
||||||
getClient(function () {})
|
|
||||||
|
|
||||||
dragDrop('body', onFiles)
|
dragDrop('body', onFiles)
|
||||||
|
|
||||||
@@ -152,23 +135,6 @@ function isNotTorrentFile (file) {
|
|||||||
return !isTorrentFile(file)
|
return !isTorrentFile(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRtcConfig (url, cb) {
|
|
||||||
xhr(url, function (err, res) {
|
|
||||||
if (err || res.statusCode !== 200) {
|
|
||||||
cb(new Error('Could not get WebRTC config from server. Using default (without TURN).'))
|
|
||||||
} else {
|
|
||||||
var rtcConfig
|
|
||||||
try { rtcConfig = JSON.parse(res.body) } catch (err) {}
|
|
||||||
if (rtcConfig) {
|
|
||||||
console.log('got rtc config: %o', rtcConfig)
|
|
||||||
cb(null, rtcConfig)
|
|
||||||
} else {
|
|
||||||
cb(new Error('Got invalid WebRTC config from server: ' + res.body))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function addTorrent (torrentId) {
|
function addTorrent (torrentId) {
|
||||||
getClient(function (err, client) {
|
getClient(function (err, client) {
|
||||||
if (err) return onError(err)
|
if (err) return onError(err)
|
||||||
|
|||||||
39
main/lib/get-client.js
Normal file
39
main/lib/get-client.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
var createTorrent = require('create-torrent')
|
||||||
|
var thunky = require('thunky')
|
||||||
|
var WebTorrent = require('webtorrent')
|
||||||
|
var xhr = require('xhr')
|
||||||
|
|
||||||
|
module.exports = thunky(getClient)
|
||||||
|
|
||||||
|
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
|
||||||
|
.map(function (arr) {
|
||||||
|
return arr[0]
|
||||||
|
})
|
||||||
|
.filter(function (url) {
|
||||||
|
return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0
|
||||||
|
})
|
||||||
|
|
||||||
|
function getClient (cb) {
|
||||||
|
getRtcConfig('https://instant.io/rtcConfig', function (err, rtcConfig) {
|
||||||
|
if (err) console.error(err)
|
||||||
|
var client = new WebTorrent({ rtcConfig: rtcConfig })
|
||||||
|
cb(null, client)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRtcConfig (url, cb) {
|
||||||
|
xhr(url, function (err, res) {
|
||||||
|
if (err || res.statusCode !== 200) {
|
||||||
|
cb(new Error('Could not get WebRTC config from server. Using default (without TURN).'))
|
||||||
|
} else {
|
||||||
|
var rtcConfig
|
||||||
|
try { rtcConfig = JSON.parse(res.body) } catch (err) {}
|
||||||
|
if (rtcConfig) {
|
||||||
|
console.log('got rtc config: %o', rtcConfig)
|
||||||
|
cb(null, rtcConfig)
|
||||||
|
} else {
|
||||||
|
cb(new Error('Got invalid WebRTC config from server: ' + res.body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user