fix issues trying to use WebTorrent before it was properly loaded/initialized

This commit is contained in:
Miguel Freitas 2016-05-09 22:43:33 -03:00
parent 758f18fa73
commit b762c645ee
2 changed files with 12 additions and 3 deletions

View File

@ -962,6 +962,15 @@ function applyShortenedURI(short, uriAndMimetype) {
} }
function startTorrentDownloadAndPreview(torrentId, previewContainer, isMedia) { function startTorrentDownloadAndPreview(torrentId, previewContainer, isMedia) {
if (typeof WebTorrentClient !== 'undefined') {
_startTorrentDownloadAndPreview(torrentId, previewContainer, isMedia);
} else {
// delay execution until WebTorrent is loaded/initialized
setTimeout(_startTorrentDownloadAndPreview,1000,torrentId, previewContainer, isMedia)
}
}
function _startTorrentDownloadAndPreview(torrentId, previewContainer, isMedia) {
var torrent = WebTorrentClient.get(torrentId); var torrent = WebTorrentClient.get(torrentId);
if( torrent === null ) if( torrent === null )
torrent = WebTorrentClient.add(torrentId); torrent = WebTorrentClient.add(torrentId);

View File

@ -309,6 +309,9 @@ function initWebTorrent() {
//localStorage.debug = '*' //localStorage.debug = '*'
localStorage.removeItem('debug') localStorage.removeItem('debug')
if ($.localStorage.isSet('torrentIds'))
twister.torrentIds = $.localStorage.get('torrentIds');
WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/) WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/)
$.getScript('js/webtorrent.min.js', function() { $.getScript('js/webtorrent.min.js', function() {
WebTorrentClient = new WebTorrent(); WebTorrentClient = new WebTorrent();
@ -320,9 +323,6 @@ function initWebTorrent() {
console.error('WARNING: ' + err.message); console.error('WARNING: ' + err.message);
}); });
if ($.localStorage.isSet('torrentIds'))
twister.torrentIds = $.localStorage.get('torrentIds');
$.getScript('js/localforage.min.js', function() { $.getScript('js/localforage.min.js', function() {
localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() { localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() {
for (var torrentId in twister.torrentIds) { for (var torrentId in twister.torrentIds) {