feat(top): p2p top search

This commit is contained in:
Alexey Kasyanchuk 2018-03-25 18:24:34 +03:00
parent 5c777cb629
commit 960731fb80
2 changed files with 32 additions and 2 deletions

View File

@ -36,6 +36,18 @@ export default class TopPage extends Page {
this.forceUpdate() 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() { render() {
return ( return (

View File

@ -389,8 +389,7 @@ module.exports = ({
updateTorrentTrackers(hash); updateTorrentTrackers(hash);
}); });
recive('topTorrents', function(type, callback) const topTorrentsCall = (type, callback) => {
{
let where = ''; let where = '';
let max = 20; let max = 20;
if(type && type.length > 0) if(type && type.length > 0)
@ -430,8 +429,27 @@ module.exports = ({
topCache[query] = rows; topCache[query] = rows;
callback(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) => recive('peers', (callback) =>
{ {
if(typeof callback != 'function') if(typeof callback != 'function')