fix(search): fix slow query wrong remote response on search

This commit is contained in:
Alexey Kasyanchuk 2021-02-22 03:09:54 +03:00
parent 9d684b89a6
commit 5a712f77b5
5 changed files with 43 additions and 16 deletions

View File

@ -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
{
@ -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]

View File

@ -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)
@ -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;
@ -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,
@ -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,
@ -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;
@ -213,9 +218,14 @@ 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;

View File

@ -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) => ({
@ -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)
@ -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})
})
}));
@ -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)
@ -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})
})
}));

View File

@ -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)
})

View File

@ -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)
})
}
})