From b762c645eec937ee0a0f342c62efa927a689b150 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Mon, 9 May 2016 22:43:33 -0300 Subject: [PATCH] fix issues trying to use WebTorrent before it was properly loaded/initialized --- js/interface_common.js | 9 +++++++++ js/interface_home.js | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/js/interface_common.js b/js/interface_common.js index 83e4dbc..dd2eab9 100644 --- a/js/interface_common.js +++ b/js/interface_common.js @@ -962,6 +962,15 @@ function applyShortenedURI(short, uriAndMimetype) { } 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); if( torrent === null ) torrent = WebTorrentClient.add(torrentId); diff --git a/js/interface_home.js b/js/interface_home.js index 4e5c421..ba96b88 100644 --- a/js/interface_home.js +++ b/js/interface_home.js @@ -309,6 +309,9 @@ function initWebTorrent() { //localStorage.debug = '*' localStorage.removeItem('debug') + if ($.localStorage.isSet('torrentIds')) + twister.torrentIds = $.localStorage.get('torrentIds'); + WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/) $.getScript('js/webtorrent.min.js', function() { WebTorrentClient = new WebTorrent(); @@ -320,9 +323,6 @@ function initWebTorrent() { console.error('WARNING: ' + err.message); }); - if ($.localStorage.isSet('torrentIds')) - twister.torrentIds = $.localStorage.get('torrentIds'); - $.getScript('js/localforage.min.js', function() { localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() { for (var torrentId in twister.torrentIds) {