Skip to content

Commit

Permalink
fix(search): fix slow query wrong remote response on search
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Feb 22, 2021
1 parent a871aed commit 2329b5a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
15 changes: 14 additions & 1 deletion src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ if(typeof WEB !== 'undefined')
{
const io = require("socket.io-client");
window.torrentSocket = io(document.location.protocol + '//' + document.location.hostname + (process.env.NODE_ENV === 'production' ? '/' : ':8095/'));
const emit = window.torrentSocket.emit.bind(window.torrentSocket);
window.torrentSocket.emit = (...data) => {
let id;
if(typeof data[data.length - 1] === 'function')
{
id = Math.random().toString(36).substring(5)
}
data.splice(1,0,id);
emit(...data)
return id
}
}
else
{
Expand Down Expand Up @@ -50,13 +61,15 @@ else
}
}
window.torrentSocket.emit = (name, ...data) => {
let id;
if(typeof data[data.length - 1] === 'function')
{
const id = Math.random().toString(36).substring(5)
id = Math.random().toString(36).substring(5)
window.torrentSocket.callbacks[id] = data[data.length - 1];
data[data.length - 1] = {callback: id}
}
ipcRenderer.send(name, data)
return id
}
ipcRenderer.on('callback', (event, id, data) => {
const callback = window.torrentSocket.callbacks[id]
Expand Down
24 changes: 17 additions & 7 deletions src/app/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Search extends Component {
if(this.state.advancedSearch && this.advanced)
searchTorrentsParams = Object.assign(searchTorrentsParams, this.advanced);

window.torrentSocket.emit('searchTorrent', oldSearch ? this.currentSearch : this.searchValue, searchTorrentsParams, window.customLoader((torrents) => {
this.searchTorrentId = window.torrentSocket.emit('searchTorrent', oldSearch ? this.currentSearch : this.searchValue, searchTorrentsParams, window.customLoader((torrents) => {
if(torrents) {
this.searchTorrents = torrents;
if(torrents.length != this.searchLimit)
Expand All @@ -86,7 +86,7 @@ class Search extends Component {
if(this.state.advancedSearch && this.advanced)
searchFilesParams = Object.assign(searchFilesParams, this.advanced);

window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, searchFilesParams, window.customLoader((torrents) => {
this.searchFilesId = window.torrentSocket.emit('searchFiles', oldSearch ? this.currentSearch : this.searchValue, searchFilesParams, window.customLoader((torrents) => {
if(torrents) {
this.searchFiles = torrents;
let files = 0;
Expand All @@ -113,7 +113,7 @@ class Search extends Component {
this.setState({moreTorrentsIndicator: true});
this.onSearchUpdate('indicator')

window.torrentSocket.emit('searchTorrent', this.currentSearch, {
this.searchTorrentId = window.torrentSocket.emit('searchTorrent', this.currentSearch, {
index: this.searchTorrents.length,
limit: this.searchLimit,
safeSearch: !this.notSafeSearch,
Expand Down Expand Up @@ -149,7 +149,7 @@ class Search extends Component {
this.setState({moreFilesIndicator: true});
this.onSearchUpdate('indicator')

window.torrentSocket.emit('searchFiles', this.currentSearch, {
this.searchFilesId = window.torrentSocket.emit('searchFiles', this.currentSearch, {
index: index,
limit: this.searchLimit,
safeSearch: !this.notSafeSearch,
Expand Down Expand Up @@ -201,9 +201,14 @@ class Search extends Component {
window.torrentSocket.on('newStatistic', this.newStatisticFunc);

this.remoteSearchTorrent = (torrents) => {
if(!torrents)
if(!torrents || !torrents.torrents)
return


if (this.searchTorrentId != torrents.id)
return

torrents = torrents.torrents

if(torrents.length === this.searchLimit)
this.moreSearchTorrents = true;

Expand All @@ -213,8 +218,13 @@ class Search extends Component {
window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent);

this.remoteSearchFiles = (torrents) => {
if(!torrents)
if(!torrents || !torrents.torrents)
return

if (this.searchFilesId != torrents.id)
return

torrents = torrents.torrents

if(torrents.length > 0 && this.calcTorrentsFiles(torrents) === this.searchLimit)
this.moreSearchFiles = true;
Expand Down
14 changes: 7 additions & 7 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ module.exports = async ({
}

const mergeTorrentsWithDownloadsFn = (Fn, copy) => (...args) => {
const callback = args[args.length - 1]
const rest = args.slice(0, -1)
Fn(...rest, (data) => callback(mergeTorrentsWithDownloads(data, copy)))
const callback = args[args.length - 2]
const rest = args.slice(0, -2)
Fn(...rest, (data) => callback(mergeTorrentsWithDownloads(data, copy)), args[args.length - 1])
}

const downloadFilesList = (torrent) => torrent.files.map((file, index) => ({
Expand Down Expand Up @@ -393,7 +393,7 @@ module.exports = async ({
});
}

recive('searchTorrent', mergeTorrentsWithDownloadsFn((text, navigation, callback) => {
recive('searchTorrent', mergeTorrentsWithDownloadsFn((text, navigation, callback, id) => {
searchTorrentCall(text, navigation, callback)
p2p.emit('searchTorrent', {text, navigation}, (remote, socketObject) => {
logT('search', 'remote search results', remote && remote.length)
Expand All @@ -403,7 +403,7 @@ module.exports = async ({
const peer = { address: socket.remoteAddress, port: socket.remotePort }
remote = remote.map(torrent => Object.assign(torrent, {peer}))
}
send('remoteSearchTorrent', mergeTorrentsWithDownloads(remote))
send('remoteSearchTorrent', {torrents: mergeTorrentsWithDownloads(remote), id})
})
}));

Expand Down Expand Up @@ -485,7 +485,7 @@ module.exports = async ({
});
}

recive('searchFiles', mergeTorrentsWithDownloadsFn((text, navigation, callback) => {
recive('searchFiles', mergeTorrentsWithDownloadsFn((text, navigation, callback, id) => {
searchFilesCall(text, navigation, callback)
p2p.emit('searchFiles', {text, navigation}, (remote, socketObject) => {
logT('search', 'remote search files results', remote && remote.length)
Expand All @@ -495,7 +495,7 @@ module.exports = async ({
const peer = { address: socket.remoteAddress, port: socket.remotePort }
remote = remote.map(torrent => Object.assign(torrent, {peer}))
}
send('remoteSearchFiles', mergeTorrentsWithDownloads(remote))
send('remoteSearchFiles', {torrents: mergeTorrentsWithDownloads(remote), id})
})
}));

Expand Down
1 change: 1 addition & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ app.on("ready", async () => {
if(mainWindow)
mainWindow.webContents.send('callback', id, JSON.stringify(responce))
}
arg.push(id);
}
callback.apply(null, arg)
})
Expand Down
5 changes: 4 additions & 1 deletion src/background/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ io.on('connection', (socket) =>
{
for(const message in socketMessages)
{
socket.on(message, socketMessages[message])
socket.on(message, (...data) => {
const id = data.shift();
socketMessages[message](...data, id)
})
}
})

Expand Down

0 comments on commit 2329b5a

Please sign in to comment.