Skip to content

Commit

Permalink
feat(top): p2p top search
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Mar 25, 2018
1 parent 0e5503c commit 7f10267
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/app/top-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
22 changes: 20 additions & 2 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 7f10267

Please sign in to comment.