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"); const io = require("socket.io-client");
window.torrentSocket = io(document.location.protocol + '//' + document.location.hostname + (process.env.NODE_ENV === 'production' ? '/' : ':8095/')); 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 else
{ {
@ -50,13 +61,15 @@ else
} }
} }
window.torrentSocket.emit = (name, ...data) => { window.torrentSocket.emit = (name, ...data) => {
let id;
if(typeof data[data.length - 1] === 'function') 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]; window.torrentSocket.callbacks[id] = data[data.length - 1];
data[data.length - 1] = {callback: id} data[data.length - 1] = {callback: id}
} }
ipcRenderer.send(name, data) ipcRenderer.send(name, data)
return id
} }
ipcRenderer.on('callback', (event, id, data) => { ipcRenderer.on('callback', (event, id, data) => {
const callback = window.torrentSocket.callbacks[id] const callback = window.torrentSocket.callbacks[id]

View File

@ -60,7 +60,7 @@ class Search extends Component {
if(this.state.advancedSearch && this.advanced) if(this.state.advancedSearch && this.advanced)
searchTorrentsParams = Object.assign(searchTorrentsParams, 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) { if(torrents) {
this.searchTorrents = torrents; this.searchTorrents = torrents;
if(torrents.length != this.searchLimit) if(torrents.length != this.searchLimit)
@ -86,7 +86,7 @@ class Search extends Component {
if(this.state.advancedSearch && this.advanced) if(this.state.advancedSearch && this.advanced)
searchFilesParams = Object.assign(searchFilesParams, 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) { if(torrents) {
this.searchFiles = torrents; this.searchFiles = torrents;
let files = 0; let files = 0;
@ -113,7 +113,7 @@ class Search extends Component {
this.setState({moreTorrentsIndicator: true}); this.setState({moreTorrentsIndicator: true});
this.onSearchUpdate('indicator') this.onSearchUpdate('indicator')
window.torrentSocket.emit('searchTorrent', this.currentSearch, { this.searchTorrentId = window.torrentSocket.emit('searchTorrent', this.currentSearch, {
index: this.searchTorrents.length, index: this.searchTorrents.length,
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch, safeSearch: !this.notSafeSearch,
@ -149,7 +149,7 @@ class Search extends Component {
this.setState({moreFilesIndicator: true}); this.setState({moreFilesIndicator: true});
this.onSearchUpdate('indicator') this.onSearchUpdate('indicator')
window.torrentSocket.emit('searchFiles', this.currentSearch, { this.searchFilesId = window.torrentSocket.emit('searchFiles', this.currentSearch, {
index: index, index: index,
limit: this.searchLimit, limit: this.searchLimit,
safeSearch: !this.notSafeSearch, safeSearch: !this.notSafeSearch,
@ -201,9 +201,14 @@ class Search extends Component {
window.torrentSocket.on('newStatistic', this.newStatisticFunc); window.torrentSocket.on('newStatistic', this.newStatisticFunc);
this.remoteSearchTorrent = (torrents) => { this.remoteSearchTorrent = (torrents) => {
if(!torrents) if(!torrents || !torrents.torrents)
return return
if (this.searchTorrentId != torrents.id)
return
torrents = torrents.torrents
if(torrents.length === this.searchLimit) if(torrents.length === this.searchLimit)
this.moreSearchTorrents = true; this.moreSearchTorrents = true;
@ -213,9 +218,14 @@ class Search extends Component {
window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent); window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent);
this.remoteSearchFiles = (torrents) => { this.remoteSearchFiles = (torrents) => {
if(!torrents) if(!torrents || !torrents.torrents)
return return
if (this.searchFilesId != torrents.id)
return
torrents = torrents.torrents
if(torrents.length > 0 && this.calcTorrentsFiles(torrents) === this.searchLimit) if(torrents.length > 0 && this.calcTorrentsFiles(torrents) === this.searchLimit)
this.moreSearchFiles = true; this.moreSearchFiles = true;

View File

@ -79,9 +79,9 @@ module.exports = async ({
} }
const mergeTorrentsWithDownloadsFn = (Fn, copy) => (...args) => { const mergeTorrentsWithDownloadsFn = (Fn, copy) => (...args) => {
const callback = args[args.length - 1] const callback = args[args.length - 2]
const rest = args.slice(0, -1) const rest = args.slice(0, -2)
Fn(...rest, (data) => callback(mergeTorrentsWithDownloads(data, copy))) Fn(...rest, (data) => callback(mergeTorrentsWithDownloads(data, copy)), args[args.length - 1])
} }
const downloadFilesList = (torrent) => torrent.files.map((file, index) => ({ 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) searchTorrentCall(text, navigation, callback)
p2p.emit('searchTorrent', {text, navigation}, (remote, socketObject) => { p2p.emit('searchTorrent', {text, navigation}, (remote, socketObject) => {
logT('search', 'remote search results', remote && remote.length) logT('search', 'remote search results', remote && remote.length)
@ -403,7 +403,7 @@ module.exports = async ({
const peer = { address: socket.remoteAddress, port: socket.remotePort } const peer = { address: socket.remoteAddress, port: socket.remotePort }
remote = remote.map(torrent => Object.assign(torrent, {peer})) 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) searchFilesCall(text, navigation, callback)
p2p.emit('searchFiles', {text, navigation}, (remote, socketObject) => { p2p.emit('searchFiles', {text, navigation}, (remote, socketObject) => {
logT('search', 'remote search files results', remote && remote.length) logT('search', 'remote search files results', remote && remote.length)
@ -495,7 +495,7 @@ module.exports = async ({
const peer = { address: socket.remoteAddress, port: socket.remotePort } const peer = { address: socket.remoteAddress, port: socket.remotePort }
remote = remote.map(torrent => Object.assign(torrent, {peer})) 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) if(mainWindow)
mainWindow.webContents.send('callback', id, JSON.stringify(responce)) mainWindow.webContents.send('callback', id, JSON.stringify(responce))
} }
arg.push(id);
} }
callback.apply(null, arg) callback.apply(null, arg)
}) })

View File

@ -69,7 +69,10 @@ io.on('connection', (socket) =>
{ {
for(const message in socketMessages) for(const message in socketMessages)
{ {
socket.on(message, socketMessages[message]) socket.on(message, (...data) => {
const id = data.shift();
socketMessages[message](...data, id)
})
} }
}) })