diff --git a/src/app/app.js b/src/app/app.js index b4fda24c..e1a0353c 100644 --- a/src/app/app.js +++ b/src/app/app.js @@ -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] diff --git a/src/app/search.js b/src/app/search.js index 5b563d2e..6dc6b69f 100644 --- a/src/app/search.js +++ b/src/app/search.js @@ -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,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; diff --git a/src/background/api.js b/src/background/api.js index dfc895ba..59e4561d 100644 --- a/src/background/api.js +++ b/src/background/api.js @@ -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}) }) })); diff --git a/src/background/background.js b/src/background/background.js index 6f1d5358..93845332 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -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) }) diff --git a/src/background/server.js b/src/background/server.js index ffc22d78..e3421a0e 100644 --- a/src/background/server.js +++ b/src/background/server.js @@ -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) + }) } })