Memoize event handlers
Stop virtualdom from swapping out every event handler on every update
This commit is contained in:
@@ -23,6 +23,8 @@ var config = require('../config')
|
|||||||
var TorrentPlayer = require('./lib/torrent-player')
|
var TorrentPlayer = require('./lib/torrent-player')
|
||||||
var torrentPoster = require('./lib/torrent-poster')
|
var torrentPoster = require('./lib/torrent-poster')
|
||||||
var util = require('./util')
|
var util = require('./util')
|
||||||
|
var {setDispatch} = require('./lib/dispatcher')
|
||||||
|
setDispatch(dispatch)
|
||||||
|
|
||||||
// These two dependencies are the slowest-loading, so we lazy load them
|
// These two dependencies are the slowest-loading, so we lazy load them
|
||||||
// This cuts time from icon click to rendered window from ~550ms to ~150ms on my laptop
|
// This cuts time from icon click to rendered window from ~550ms to ~150ms on my laptop
|
||||||
@@ -170,7 +172,7 @@ function initWebtorrent () {
|
|||||||
// This is the (mostly) pure function from state -> UI. Returns a virtual DOM
|
// This is the (mostly) pure function from state -> UI. Returns a virtual DOM
|
||||||
// tree. Any events, such as button clicks, will turn into calls to dispatch()
|
// tree. Any events, such as button clicks, will turn into calls to dispatch()
|
||||||
function render (state) {
|
function render (state) {
|
||||||
return App(state, dispatch)
|
return App(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls render() to go from state -> UI, then applies to vdom to the real DOM.
|
// Calls render() to go from state -> UI, then applies to vdom to the real DOM.
|
||||||
@@ -216,32 +218,23 @@ function dispatch (action, ...args) {
|
|||||||
if (action === 'seed') {
|
if (action === 'seed') {
|
||||||
seed(args[0] /* files */)
|
seed(args[0] /* files */)
|
||||||
}
|
}
|
||||||
if (action === 'play') {
|
|
||||||
state.location.go({
|
|
||||||
url: 'player',
|
|
||||||
onbeforeload: function (cb) {
|
|
||||||
openPlayer(args[0] /* torrentSummary */, args[1] /* index */, cb)
|
|
||||||
},
|
|
||||||
onbeforeunload: closePlayer
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (action === 'openFile') {
|
if (action === 'openFile') {
|
||||||
openFile(args[0] /* torrentSummary */, args[1] /* index */)
|
openFile(args[0] /* infoHash */, args[1] /* index */)
|
||||||
}
|
}
|
||||||
if (action === 'openFolder') {
|
if (action === 'openFolder') {
|
||||||
openFolder(args[0] /* torrentSummary */)
|
openFolder(args[0] /* infoHash */)
|
||||||
}
|
}
|
||||||
if (action === 'toggleTorrent') {
|
if (action === 'toggleTorrent') {
|
||||||
toggleTorrent(args[0] /* torrentSummary */)
|
toggleTorrent(args[0] /* infoHash */)
|
||||||
}
|
}
|
||||||
if (action === 'deleteTorrent') {
|
if (action === 'deleteTorrent') {
|
||||||
deleteTorrent(args[0] /* torrentSummary */)
|
deleteTorrent(args[0] /* infoHash */)
|
||||||
}
|
}
|
||||||
if (action === 'toggleSelectTorrent') {
|
if (action === 'toggleSelectTorrent') {
|
||||||
toggleSelectTorrent(args[0] /* infoHash */)
|
toggleSelectTorrent(args[0] /* infoHash */)
|
||||||
}
|
}
|
||||||
if (action === 'openTorrentContextMenu') {
|
if (action === 'openTorrentContextMenu') {
|
||||||
openTorrentContextMenu(args[0] /* torrentSummary */)
|
openTorrentContextMenu(args[0] /* infoHash */)
|
||||||
}
|
}
|
||||||
if (action === 'openChromecast') {
|
if (action === 'openChromecast') {
|
||||||
lazyLoadCast().openChromecast()
|
lazyLoadCast().openChromecast()
|
||||||
@@ -265,6 +258,13 @@ function dispatch (action, ...args) {
|
|||||||
playPause()
|
playPause()
|
||||||
}
|
}
|
||||||
if (action === 'play') {
|
if (action === 'play') {
|
||||||
|
state.location.go({
|
||||||
|
url: 'player',
|
||||||
|
onbeforeload: function (cb) {
|
||||||
|
openPlayer(args[0] /* infoHash */, args[1] /* index */, cb)
|
||||||
|
},
|
||||||
|
onbeforeunload: closePlayer
|
||||||
|
})
|
||||||
playPause(false)
|
playPause(false)
|
||||||
}
|
}
|
||||||
if (action === 'pause') {
|
if (action === 'pause') {
|
||||||
@@ -285,7 +285,7 @@ function dispatch (action, ...args) {
|
|||||||
ipcRenderer.send('unblockPowerSave')
|
ipcRenderer.send('unblockPowerSave')
|
||||||
}
|
}
|
||||||
if (action === 'toggleFullScreen') {
|
if (action === 'toggleFullScreen') {
|
||||||
ipcRenderer.send('toggleFullScreen', args[0])
|
ipcRenderer.send('toggleFullScreen', args[0] /* optional bool */)
|
||||||
}
|
}
|
||||||
if (action === 'mediaMouseMoved') {
|
if (action === 'mediaMouseMoved') {
|
||||||
state.playing.mouseStationarySince = new Date().getTime()
|
state.playing.mouseStationarySince = new Date().getTime()
|
||||||
@@ -739,8 +739,9 @@ function stopServer () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Opens the video player
|
// Opens the video player
|
||||||
function openPlayer (torrentSummary, index, cb) {
|
function openPlayer (infoHash, index, cb) {
|
||||||
var torrent = lazyLoadClient().get(torrentSummary.infoHash)
|
var torrentSummary = getTorrentSummary(infoHash)
|
||||||
|
var torrent = lazyLoadClient().get(infoHash)
|
||||||
if (!torrent || !torrent.done) playInterfaceSound('PLAY')
|
if (!torrent || !torrent.done) playInterfaceSound('PLAY')
|
||||||
torrentSummary.playStatus = 'requested'
|
torrentSummary.playStatus = 'requested'
|
||||||
update()
|
update()
|
||||||
@@ -792,16 +793,16 @@ function closePlayer (cb) {
|
|||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
|
|
||||||
function openFile (torrentSummary, index) {
|
function openFile (infoHash, index) {
|
||||||
var torrent = lazyLoadClient().get(torrentSummary.infoHash)
|
var torrent = lazyLoadClient().get(infoHash)
|
||||||
if (!torrent) return
|
if (!torrent) return
|
||||||
|
|
||||||
var filePath = path.join(torrent.path, torrent.files[index].path)
|
var filePath = path.join(torrent.path, torrent.files[index].path)
|
||||||
ipcRenderer.send('openItem', filePath)
|
ipcRenderer.send('openItem', filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
function openFolder (torrentSummary) {
|
function openFolder (infoHash) {
|
||||||
var torrent = lazyLoadClient().get(torrentSummary.infoHash)
|
var torrent = lazyLoadClient().get(infoHash)
|
||||||
if (!torrent) return
|
if (!torrent) return
|
||||||
|
|
||||||
var folderPath = path.join(torrent.path, torrent.name)
|
var folderPath = path.join(torrent.path, torrent.name)
|
||||||
@@ -815,7 +816,8 @@ function openFolder (torrentSummary) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleTorrent (torrentSummary) {
|
function toggleTorrent (infoHash) {
|
||||||
|
var torrentSummary = getTorrentSummary(infoHash)
|
||||||
if (torrentSummary.status === 'paused') {
|
if (torrentSummary.status === 'paused') {
|
||||||
torrentSummary.status = 'new'
|
torrentSummary.status = 'new'
|
||||||
startTorrentingSummary(torrentSummary)
|
startTorrentingSummary(torrentSummary)
|
||||||
@@ -827,8 +829,7 @@ function toggleTorrent (torrentSummary) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteTorrent (torrentSummary) {
|
function deleteTorrent (infoHash) {
|
||||||
var infoHash = torrentSummary.infoHash
|
|
||||||
var torrent = getTorrent(infoHash)
|
var torrent = getTorrent(infoHash)
|
||||||
if (torrent) torrent.destroy()
|
if (torrent) torrent.destroy()
|
||||||
|
|
||||||
@@ -845,7 +846,8 @@ function toggleSelectTorrent (infoHash) {
|
|||||||
update()
|
update()
|
||||||
}
|
}
|
||||||
|
|
||||||
function openTorrentContextMenu (torrentSummary) {
|
function openTorrentContextMenu (infoHash) {
|
||||||
|
var torrentSummary = getTorrentSummary(infoHash)
|
||||||
var menu = new remote.Menu()
|
var menu = new remote.Menu()
|
||||||
menu.append(new remote.MenuItem({
|
menu.append(new remote.MenuItem({
|
||||||
label: 'Save Torrent File As...',
|
label: 'Save Torrent File As...',
|
||||||
|
|||||||
36
renderer/lib/dispatcher.js
Normal file
36
renderer/lib/dispatcher.js
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
module.exports = {
|
||||||
|
setDispatch,
|
||||||
|
dispatch,
|
||||||
|
dispatcher
|
||||||
|
}
|
||||||
|
|
||||||
|
// _memoize most of our event handlers, which are functions in the form
|
||||||
|
// () => dispatch(<args>)
|
||||||
|
// ... this prevents virtual-dom from updating tons of listeners on every update()
|
||||||
|
var _dispatchers = {}
|
||||||
|
var _dispatch = () => {}
|
||||||
|
|
||||||
|
function setDispatch (dispatch) {
|
||||||
|
_dispatch = dispatch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a _memoized event handler that calls dispatch()
|
||||||
|
// All args must be JSON-able
|
||||||
|
function dispatcher (...args) {
|
||||||
|
var json = JSON.stringify(args)
|
||||||
|
var handler = _dispatchers[json]
|
||||||
|
if (!handler) {
|
||||||
|
_dispatchers[json] = (e) => {
|
||||||
|
// Don't click on whatever is below the button
|
||||||
|
e.stopPropagation()
|
||||||
|
// Don't regisiter clicks on disabled buttons
|
||||||
|
if (e.target.classList.contains('disabled')) return
|
||||||
|
_dispatch.apply(null, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
function dispatch (...args) {
|
||||||
|
_dispatch.apply(null, args)
|
||||||
|
}
|
||||||
@@ -4,7 +4,9 @@ var h = require('virtual-dom/h')
|
|||||||
var hyperx = require('hyperx')
|
var hyperx = require('hyperx')
|
||||||
var hx = hyperx(h)
|
var hx = hyperx(h)
|
||||||
|
|
||||||
function Header (state, dispatch) {
|
var {dispatcher} = require('../lib/dispatcher')
|
||||||
|
|
||||||
|
function Header (state) {
|
||||||
return hx`
|
return hx`
|
||||||
<div class='header'>
|
<div class='header'>
|
||||||
${getTitle()}
|
${getTitle()}
|
||||||
@@ -12,13 +14,13 @@ function Header (state, dispatch) {
|
|||||||
<i.icon.back
|
<i.icon.back
|
||||||
class=${state.location.hasBack() ? '' : 'disabled'}
|
class=${state.location.hasBack() ? '' : 'disabled'}
|
||||||
title='Back'
|
title='Back'
|
||||||
onclick=${() => dispatch('back')}>
|
onclick=${dispatcher('back')}>
|
||||||
chevron_left
|
chevron_left
|
||||||
</i>
|
</i>
|
||||||
<i.icon.forward
|
<i.icon.forward
|
||||||
class=${state.location.hasForward() ? '' : 'disabled'}
|
class=${state.location.hasForward() ? '' : 'disabled'}
|
||||||
title='Forward'
|
title='Forward'
|
||||||
onclick=${() => dispatch('forward')}>
|
onclick=${dispatcher('forward')}>
|
||||||
chevron_right
|
chevron_right
|
||||||
</i>
|
</i>
|
||||||
</div>
|
</div>
|
||||||
@@ -40,7 +42,7 @@ function Header (state, dispatch) {
|
|||||||
<i
|
<i
|
||||||
class='icon add'
|
class='icon add'
|
||||||
title='Add torrent'
|
title='Add torrent'
|
||||||
onclick=${() => dispatch('showOpenTorrentFile')}>
|
onclick=${dispatcher('showOpenTorrentFile')}>
|
||||||
add
|
add
|
||||||
</i>
|
</i>
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ var h = require('virtual-dom/h')
|
|||||||
var hyperx = require('hyperx')
|
var hyperx = require('hyperx')
|
||||||
var hx = hyperx(h)
|
var hx = hyperx(h)
|
||||||
|
|
||||||
function OpenTorrentAddressModal (state, dispatch) {
|
var {dispatch} = require('../lib/dispatcher')
|
||||||
|
|
||||||
|
function OpenTorrentAddressModal (state) {
|
||||||
return hx`
|
return hx`
|
||||||
<div class='open-torrent-address-modal'>
|
<div class='open-torrent-address-modal'>
|
||||||
<p><strong>Enter torrent address or magnet link</strong></p>
|
<p><strong>Enter torrent address or magnet link</strong></p>
|
||||||
@@ -15,17 +17,17 @@ function OpenTorrentAddressModal (state, dispatch) {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
|
}
|
||||||
function handleKeyPress (e) {
|
|
||||||
if (e.which === 13) handleOK() /* hit Enter to submit */
|
function handleKeyPress (e) {
|
||||||
}
|
if (e.which === 13) handleOK() /* hit Enter to submit */
|
||||||
|
}
|
||||||
function handleOK () {
|
|
||||||
dispatch('exitModal')
|
function handleOK () {
|
||||||
dispatch('addTorrent', document.querySelector('#add-torrent-url').value)
|
dispatch('exitModal')
|
||||||
}
|
dispatch('addTorrent', document.querySelector('#add-torrent-url').value)
|
||||||
|
}
|
||||||
function handleCancel () {
|
|
||||||
dispatch('exitModal')
|
function handleCancel () {
|
||||||
}
|
dispatch('exitModal')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,23 +5,24 @@ var hyperx = require('hyperx')
|
|||||||
var hx = hyperx(h)
|
var hx = hyperx(h)
|
||||||
|
|
||||||
var util = require('../util')
|
var util = require('../util')
|
||||||
|
var {dispatch, dispatcher} = require('../lib/dispatcher')
|
||||||
|
|
||||||
// Shows a streaming video player. Standard features + Chromecast + Airplay
|
// Shows a streaming video player. Standard features + Chromecast + Airplay
|
||||||
function Player (state, dispatch) {
|
function Player (state) {
|
||||||
// Show the video as large as will fit in the window, play immediately
|
// Show the video as large as will fit in the window, play immediately
|
||||||
// If the video is on Chromecast or Airplay, show a title screen instead
|
// If the video is on Chromecast or Airplay, show a title screen instead
|
||||||
var showVideo = state.playing.location === 'local'
|
var showVideo = state.playing.location === 'local'
|
||||||
return hx`
|
return hx`
|
||||||
<div
|
<div
|
||||||
class='player'
|
class='player'
|
||||||
onmousemove=${() => dispatch('mediaMouseMoved')}>
|
onmousemove=${dispatcher('mediaMouseMoved')}>
|
||||||
${showVideo ? renderMedia(state, dispatch) : renderCastScreen(state, dispatch)}
|
${showVideo ? renderMedia(state) : renderCastScreen(state)}
|
||||||
${renderPlayerControls(state, dispatch)}
|
${renderPlayerControls(state)}
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderMedia (state, dispatch) {
|
function renderMedia (state) {
|
||||||
if (!state.server) return
|
if (!state.server) return
|
||||||
|
|
||||||
// Unfortunately, play/pause can't be done just by modifying HTML.
|
// Unfortunately, play/pause can't be done just by modifying HTML.
|
||||||
@@ -54,11 +55,11 @@ function renderMedia (state, dispatch) {
|
|||||||
var mediaTag = hx`
|
var mediaTag = hx`
|
||||||
<div
|
<div
|
||||||
src='${state.server.localURL}'
|
src='${state.server.localURL}'
|
||||||
ondblclick=${() => dispatch('toggleFullScreen')}
|
ondblclick=${dispatcher('toggleFullScreen')}
|
||||||
onloadedmetadata=${onLoadedMetadata}
|
onloadedmetadata=${onLoadedMetadata}
|
||||||
onended=${onEnded}
|
onended=${onEnded}
|
||||||
onplay=${() => dispatch('mediaPlaying')}
|
onplay=${dispatcher('mediaPlaying')}
|
||||||
onpause=${() => dispatch('mediaPaused')}
|
onpause=${dispatcher('mediaPaused')}
|
||||||
autoplay>
|
autoplay>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
@@ -75,7 +76,7 @@ function renderMedia (state, dispatch) {
|
|||||||
<div
|
<div
|
||||||
class='letterbox'
|
class='letterbox'
|
||||||
style=${style}
|
style=${style}
|
||||||
onmousemove=${() => dispatch('mediaMouseMoved')}>
|
onmousemove=${dispatcher('mediaMouseMoved')}>
|
||||||
${mediaTag}
|
${mediaTag}
|
||||||
${renderAudioMetadata(state)}
|
${renderAudioMetadata(state)}
|
||||||
</div>
|
</div>
|
||||||
@@ -126,7 +127,7 @@ function renderAudioMetadata (state) {
|
|||||||
return hx`<div class='audio-metadata'>${elems}</div>`
|
return hx`<div class='audio-metadata'>${elems}</div>`
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderCastScreen (state, dispatch) {
|
function renderCastScreen (state) {
|
||||||
var isChromecast = state.playing.location.startsWith('chromecast')
|
var isChromecast = state.playing.location.startsWith('chromecast')
|
||||||
var isAirplay = state.playing.location.startsWith('airplay')
|
var isAirplay = state.playing.location.startsWith('airplay')
|
||||||
var isStarting = state.playing.location.endsWith('-pending')
|
var isStarting = state.playing.location.endsWith('-pending')
|
||||||
@@ -166,7 +167,7 @@ function getPlayingTorrentSummary (state) {
|
|||||||
return state.saved.torrents.find((x) => x.infoHash === infoHash)
|
return state.saved.torrents.find((x) => x.infoHash === infoHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderPlayerControls (state, dispatch) {
|
function renderPlayerControls (state) {
|
||||||
var positionPercent = 100 * state.playing.currentTime / state.playing.duration
|
var positionPercent = 100 * state.playing.currentTime / state.playing.duration
|
||||||
var playbackCursorStyle = { left: 'calc(' + positionPercent + '% - 8px)' }
|
var playbackCursorStyle = { left: 'calc(' + positionPercent + '% - 8px)' }
|
||||||
|
|
||||||
@@ -183,7 +184,7 @@ function renderPlayerControls (state, dispatch) {
|
|||||||
`,
|
`,
|
||||||
hx`
|
hx`
|
||||||
<i class='icon fullscreen'
|
<i class='icon fullscreen'
|
||||||
onclick=${() => dispatch('toggleFullScreen')}>
|
onclick=${dispatcher('toggleFullScreen')}>
|
||||||
${state.window.isFullScreen ? 'fullscreen_exit' : 'fullscreen'}
|
${state.window.isFullScreen ? 'fullscreen_exit' : 'fullscreen'}
|
||||||
</i>
|
</i>
|
||||||
`
|
`
|
||||||
@@ -196,18 +197,18 @@ function renderPlayerControls (state, dispatch) {
|
|||||||
if (isOnChromecast) {
|
if (isOnChromecast) {
|
||||||
chromecastClass = 'active'
|
chromecastClass = 'active'
|
||||||
airplayClass = 'disabled'
|
airplayClass = 'disabled'
|
||||||
chromecastHandler = () => dispatch('stopCasting')
|
chromecastHandler = dispatcher('stopCasting')
|
||||||
airplayHandler = undefined
|
airplayHandler = undefined
|
||||||
} else if (isOnAirplay) {
|
} else if (isOnAirplay) {
|
||||||
chromecastClass = 'disabled'
|
chromecastClass = 'disabled'
|
||||||
airplayClass = 'active'
|
airplayClass = 'active'
|
||||||
chromecastHandler = undefined
|
chromecastHandler = undefined
|
||||||
airplayHandler = () => dispatch('stopCasting')
|
airplayHandler = dispatcher('stopCasting')
|
||||||
} else {
|
} else {
|
||||||
chromecastClass = ''
|
chromecastClass = ''
|
||||||
airplayClass = ''
|
airplayClass = ''
|
||||||
chromecastHandler = () => dispatch('openChromecast')
|
chromecastHandler = dispatcher('openChromecast')
|
||||||
airplayHandler = () => dispatch('openAirplay')
|
airplayHandler = dispatcher('openAirplay')
|
||||||
}
|
}
|
||||||
if (state.devices.chromecast || isOnChromecast) {
|
if (state.devices.chromecast || isOnChromecast) {
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
@@ -233,7 +234,7 @@ function renderPlayerControls (state, dispatch) {
|
|||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
<i.icon.back
|
<i.icon.back
|
||||||
onclick=${() => dispatch('back')}>
|
onclick=${dispatcher('back')}>
|
||||||
chevron_left
|
chevron_left
|
||||||
</i>
|
</i>
|
||||||
`)
|
`)
|
||||||
@@ -241,7 +242,7 @@ function renderPlayerControls (state, dispatch) {
|
|||||||
|
|
||||||
// Finally, the big button in the center plays or pauses the video
|
// Finally, the big button in the center plays or pauses the video
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
<i class='icon play-pause' onclick=${() => dispatch('playPause')}>
|
<i class='icon play-pause' onclick=${dispatcher('playPause')}>
|
||||||
${state.playing.isPaused ? 'play_arrow' : 'pause'}
|
${state.playing.isPaused ? 'play_arrow' : 'pause'}
|
||||||
</i>
|
</i>
|
||||||
`)
|
`)
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ var prettyBytes = require('prettier-bytes')
|
|||||||
var util = require('../util')
|
var util = require('../util')
|
||||||
|
|
||||||
var TorrentPlayer = require('../lib/torrent-player')
|
var TorrentPlayer = require('../lib/torrent-player')
|
||||||
|
var {dispatcher} = require('../lib/dispatcher')
|
||||||
|
|
||||||
function TorrentList (state, dispatch) {
|
function TorrentList (state) {
|
||||||
var torrentRows = state.saved.torrents.map(
|
var torrentRows = state.saved.torrents.map(
|
||||||
(torrentSummary) => renderTorrent(torrentSummary))
|
(torrentSummary) => renderTorrent(torrentSummary))
|
||||||
return hx`
|
return hx`
|
||||||
@@ -53,8 +54,8 @@ function TorrentList (state, dispatch) {
|
|||||||
classes = classes.join(' ')
|
classes = classes.join(' ')
|
||||||
return hx`
|
return hx`
|
||||||
<div style=${style} class=${classes}
|
<div style=${style} class=${classes}
|
||||||
oncontextmenu=${() => dispatch('openTorrentContextMenu', torrentSummary)}
|
oncontextmenu=${dispatcher('openTorrentContextMenu', infoHash)}
|
||||||
onclick=${() => dispatch('toggleSelectTorrent', infoHash)}>
|
onclick=${dispatcher('toggleSelectTorrent', infoHash)}>
|
||||||
${renderTorrentMetadata(torrent, torrentSummary)}
|
${renderTorrentMetadata(torrent, torrentSummary)}
|
||||||
${renderTorrentButtons(torrentSummary)}
|
${renderTorrentButtons(torrentSummary)}
|
||||||
${isSelected ? renderTorrentDetails(torrent, torrentSummary) : ''}
|
${isSelected ? renderTorrentDetails(torrent, torrentSummary) : ''}
|
||||||
@@ -110,6 +111,8 @@ function TorrentList (state, dispatch) {
|
|||||||
// Download button toggles between torrenting (DL/seed) and paused
|
// Download button toggles between torrenting (DL/seed) and paused
|
||||||
// Play button starts streaming the torrent immediately, unpausing if needed
|
// Play button starts streaming the torrent immediately, unpausing if needed
|
||||||
function renderTorrentButtons (torrentSummary) {
|
function renderTorrentButtons (torrentSummary) {
|
||||||
|
var infoHash = torrentSummary.infoHash
|
||||||
|
|
||||||
var playIcon, playTooltip, playClass
|
var playIcon, playTooltip, playClass
|
||||||
if (torrentSummary.playStatus === 'unplayable') {
|
if (torrentSummary.playStatus === 'unplayable') {
|
||||||
playIcon = 'play_arrow'
|
playIcon = 'play_arrow'
|
||||||
@@ -141,34 +144,28 @@ function TorrentList (state, dispatch) {
|
|||||||
<i.btn.icon.play
|
<i.btn.icon.play
|
||||||
title=${playTooltip}
|
title=${playTooltip}
|
||||||
class=${playClass}
|
class=${playClass}
|
||||||
onclick=${(e) => handleButton('play', e)}>
|
onclick=${dispatcher('play', infoHash)}>
|
||||||
${playIcon}
|
${playIcon}
|
||||||
</i>
|
</i>
|
||||||
<i.btn.icon.download
|
<i.btn.icon.download
|
||||||
class=${torrentSummary.status}
|
class=${torrentSummary.status}
|
||||||
title=${downloadTooltip}
|
title=${downloadTooltip}
|
||||||
onclick=${(e) => handleButton('toggleTorrent', e)}>
|
onclick=${dispatcher('toggleTorrent', infoHash)}>
|
||||||
${downloadIcon}
|
${downloadIcon}
|
||||||
</i>
|
</i>
|
||||||
<i
|
<i
|
||||||
class='icon delete'
|
class='icon delete'
|
||||||
title='Remove torrent'
|
title='Remove torrent'
|
||||||
onclick=${(e) => handleButton('deleteTorrent', e)}>
|
onclick=${dispatcher('deleteTorrent', infoHash)}>
|
||||||
close
|
close
|
||||||
</i>
|
</i>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
|
|
||||||
function handleButton (action, e) {
|
|
||||||
// Prevent propagation so that we don't select/unselect the torrent
|
|
||||||
e.stopPropagation()
|
|
||||||
if (e.target.classList.contains('disabled')) return
|
|
||||||
dispatch(action, torrentSummary)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show files, per-file download status and play buttons, and so on
|
// Show files, per-file download status and play buttons, and so on
|
||||||
function renderTorrentDetails (torrent, torrentSummary) {
|
function renderTorrentDetails (torrent, torrentSummary) {
|
||||||
|
var infoHash = torrentSummary.infoHash
|
||||||
var filesElement
|
var filesElement
|
||||||
if (!torrentSummary.files) {
|
if (!torrentSummary.files) {
|
||||||
// We don't know what files this torrent contains
|
// We don't know what files this torrent contains
|
||||||
@@ -183,7 +180,10 @@ function TorrentList (state, dispatch) {
|
|||||||
filesElement = hx`
|
filesElement = hx`
|
||||||
<div class='files'>
|
<div class='files'>
|
||||||
<strong>Files</strong>
|
<strong>Files</strong>
|
||||||
<span class='open-folder' onclick=${handleOpenFolder}>Open folder</span>
|
<span class='open-folder'
|
||||||
|
onclick=${dispatcher('openFolder', infoHash)}>
|
||||||
|
Open folder
|
||||||
|
</span>
|
||||||
<table>
|
<table>
|
||||||
${fileRows}
|
${fileRows}
|
||||||
</table>
|
</table>
|
||||||
@@ -196,11 +196,6 @@ function TorrentList (state, dispatch) {
|
|||||||
${filesElement}
|
${filesElement}
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
|
|
||||||
function handleOpenFolder (e) {
|
|
||||||
e.stopPropagation()
|
|
||||||
dispatch('openFolder', torrentSummary)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show a single torrentSummary file in the details view for a single torrent
|
// Show a single torrentSummary file in the details view for a single torrent
|
||||||
@@ -210,15 +205,20 @@ function TorrentList (state, dispatch) {
|
|||||||
var progress = Math.round(100 * file.numPiecesPresent / (file.numPieces || 0)) + '%'
|
var progress = Math.round(100 * file.numPiecesPresent / (file.numPieces || 0)) + '%'
|
||||||
|
|
||||||
// Second, render the file as a table row
|
// Second, render the file as a table row
|
||||||
|
var infoHash = torrentSummary.infoHash
|
||||||
var icon
|
var icon
|
||||||
var rowClass = ''
|
var rowClass = ''
|
||||||
if (state.playing.infoHash === torrentSummary.infoHash && state.playing.fileIndex === index) {
|
var handleClick
|
||||||
|
if (state.playing.infoHash === infoHash && state.playing.fileIndex === index) {
|
||||||
icon = 'pause_arrow' /* playing? add option to pause */
|
icon = 'pause_arrow' /* playing? add option to pause */
|
||||||
|
handleClick = undefined // TODO: pause audio
|
||||||
} else if (TorrentPlayer.isPlayable(file)) {
|
} else if (TorrentPlayer.isPlayable(file)) {
|
||||||
icon = 'play_arrow' /* playable? add option to play */
|
icon = 'play_arrow' /* playable? add option to play */
|
||||||
|
handleClick = dispatcher('play', infoHash, index)
|
||||||
} else {
|
} else {
|
||||||
icon = 'description' /* file icon, opens in OS default app */
|
icon = 'description' /* file icon, opens in OS default app */
|
||||||
rowClass = isDone ? '' : 'disabled'
|
rowClass = isDone ? '' : 'disabled'
|
||||||
|
handleClick = dispatcher('openFile', infoHash, index)
|
||||||
}
|
}
|
||||||
return hx`
|
return hx`
|
||||||
<tr onclick=${handleClick} class='${rowClass}'>
|
<tr onclick=${handleClick} class='${rowClass}'>
|
||||||
@@ -230,17 +230,5 @@ function TorrentList (state, dispatch) {
|
|||||||
<td class='col-size'>${prettyBytes(file.length)}</td>
|
<td class='col-size'>${prettyBytes(file.length)}</td>
|
||||||
</tr>
|
</tr>
|
||||||
`
|
`
|
||||||
|
|
||||||
// Finally, let the user click on the row to play media or open files
|
|
||||||
function handleClick (e) {
|
|
||||||
e.stopPropagation()
|
|
||||||
if (icon === 'pause_arrow') {
|
|
||||||
throw new Error('Unimplemented') // TODO: pause audio
|
|
||||||
} else if (icon === 'play_arrow') {
|
|
||||||
dispatch('play', torrentSummary, index)
|
|
||||||
} else if (isDone) {
|
|
||||||
dispatch('openFile', torrentSummary, index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user