From 960731fb8045094ad5d376f68fb1e9e41baaf843 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sun, 25 Mar 2018 18:24:34 +0300 Subject: [PATCH] feat(top): p2p top search --- src/app/top-page.js | 12 ++++++++++++ src/background/api.js | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/app/top-page.js b/src/app/top-page.js index 81b1448..f0ed1ef 100644 --- a/src/app/top-page.js +++ b/src/app/top-page.js @@ -36,6 +36,18 @@ export default class TopPage extends Page { this.forceUpdate() })) } + this.remoteTopTorrents = ({torrents, type}) => { + if(!torrents) + return + this.topTorrents[type] = _.orderBy(_.unionBy(this.topTorrents[type], torrents, 'hash'), ['seeders'], ['desc']) + this.forceUpdate(); + } + window.torrentSocket.on('remoteTopTorrents', this.remoteTopTorrents); + } + componentWillUnmount() + { + if(this.remoteTopTorrents) + window.torrentSocket.off('remoteTopTorrents', this.remoteTopTorrents); } render() { return ( diff --git a/src/background/api.js b/src/background/api.js index fbbc6c3..888019b 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -389,8 +389,7 @@ module.exports = ({ updateTorrentTrackers(hash); }); - recive('topTorrents', function(type, callback) - { + const topTorrentsCall = (type, callback) => { let where = ''; let max = 20; if(type && type.length > 0) @@ -430,8 +429,27 @@ module.exports = ({ topCache[query] = rows; callback(rows); }); + } + + recive('topTorrents', (type, callback) => + { + topTorrentsCall(type, callback) + p2p.emit('topTorrents', {type}, (remote, socketObject) => { + console.log('remote top results', remote && remote.length) + if(remote && remote.length > 0) + { + const { _socket: socket } = socketObject + const peer = { address: socket.remoteAddress, port: socket.remotePort } + remote = remote.map(torrent => Object.assign(torrent, {peer})) + } + send('remoteTopTorrents', {torrents: remote, type}) + }) }); + p2p.on('topTorrents', ({type} = {}, callback) => { + topTorrentsCall(type, (data) => callback(data)) + }) + recive('peers', (callback) => { if(typeof callback != 'function')