From 5e02b0df73e67d8994df80a843965be19e2ea22c Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Wed, 18 Apr 2018 17:12:25 +0300 Subject: [PATCH] fix(votes): actual votes display works now --- src/app/torrent-page.js | 15 +++++++------ src/app/waitObject.js | 15 +++++++++++++ src/background/api.js | 48 +++++++++++++++++++++++++---------------- 3 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 src/app/waitObject.js diff --git a/src/app/torrent-page.js b/src/app/torrent-page.js index 7fbf1a0..92f1d20 100644 --- a/src/app/torrent-page.js +++ b/src/app/torrent-page.js @@ -20,6 +20,7 @@ import LinearProgress from 'material-ui/LinearProgress'; import FlatButton from 'material-ui/FlatButton'; import {fileTypeDetect} from './content' import {contentIcon} from './torrent' +import waitObject from './waitObject' let buildFilesTree = (filesList) => { let rootTree = { @@ -182,7 +183,7 @@ export default class TorrentPage extends Page { window.torrentSocket.emit('torrent', this.props.hash, {files: true, peer: this.props.peer}, window.customLoader((data) => { if(data) { this.torrent = data - this.setTitle(this.torrent.name + ' - Rats On TheBoat'); + this.setTitle(this.torrent.name + ' - Rats On The Boat'); if(this.torrent.contentCategory == 'xxx') { this.setMetaTag('robots', 'noindex'); } @@ -226,18 +227,18 @@ export default class TorrentPage extends Page { } window.torrentSocket.on('trackerTorrentUpdate', this.trackerUpdate); - this.onVote = ({hash, good, bad}) => { + this.onVotes = async ({hash, good, bad}) => { if(this.props.hash != hash) return; - if(!this.torrent) - return; + // in some cases torrent object can be resolve late + await waitObject(this, 'torrent') this.torrent.good = good; this.torrent.bad = bad; this.forceUpdate(); } - window.torrentSocket.on('vote', this.onVote); + window.torrentSocket.on('votes', this.onVotes); this.downloading = (hash) => { if(this.props.hash != hash) @@ -273,8 +274,8 @@ export default class TorrentPage extends Page { window.torrentSocket.off('filesReady', this.filesUpdated); if(this.trackerUpdate) window.torrentSocket.off('trackerTorrentUpdate', this.trackerUpdate); - if(this.onVote) - window.torrentSocket.off('vote', this.onVote); + if(this.onVotes) + window.torrentSocket.off('votes', this.onVotes); if(this.torrent && this.torrent.contentCategory == 'xxx') { this.removeMetaTag('robots'); } diff --git a/src/app/waitObject.js b/src/app/waitObject.js new file mode 100644 index 0000000..455642c --- /dev/null +++ b/src/app/waitObject.js @@ -0,0 +1,15 @@ +export default (obj, val) => new Promise((resolve) => { + if(typeof obj[val] === 'object') + { + resolve() + return + } + + const w = setInterval(() => { + if(typeof obj[val] === 'object') + { + clearInterval(w) + resolve() + } + }, 3) +}) \ No newline at end of file diff --git a/src/background/api.js b/src/background/api.js index 3083d00..788d65d 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -97,7 +97,7 @@ module.exports = ({ return; } - sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) { + sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, async function (error, rows, fields) { if(!rows || rows.length == 0) { callback(undefined) return; @@ -119,11 +119,17 @@ module.exports = ({ if(torrentClientHashMap[hash]) { const torrent = torrentClient.get(torrentClientHashMap[hash]) - if(!torrent) - return - - send('downloading', torrent.infoHash) + if(torrent) + { + send('downloading', torrent.infoHash) + } } + + // get votes + const {good, bad, selfVote} = await getVotes(hash) + send('votes', { + hash, good, bad + }); }); } @@ -630,18 +636,7 @@ module.exports = ({ }, done) }) - recive('vote', async (hash, isGood, callback) => - { - if(hash.length != 40) - return; - - if(typeof callback != 'function') - return; - - isGood = !!isGood; - - const action = isGood ? 'good' : 'bad'; - + const getVotes = async (hash) => { const votes = await p2pStore.find(`vote:${hash}`) let good = 0 let bad = 0 @@ -658,7 +653,22 @@ module.exports = ({ good++ }) } - console.log('votes before', bad, good) + + return {good, bad, selfVote} + } + + recive('vote', async (hash, isGood, callback) => + { + if(hash.length != 40) + return; + + if(typeof callback != 'function') + return; + + isGood = !!isGood; + + const action = isGood ? 'good' : 'bad'; + let {good, bad, selfVote} = await getVotes(hash) if(!selfVote) { @@ -672,7 +682,7 @@ module.exports = ({ bad += !isGood ? 1 : 0 } - send('vote', { + send('votes', { hash, good, bad }); callback(true)