From 3a92286d717de172f50bb5ec005dddfda84c6736 Mon Sep 17 00:00:00 2001 From: John Furrow Date: Sat, 19 Mar 2016 15:11:02 +0100 Subject: [PATCH] Don't request data if previous request has not yet been successful --- client/source/scripts/stores/BaseStore.js | 17 +++ client/source/scripts/stores/TorrentStore.js | 112 ++++++++++-------- .../scripts/stores/TransferDataStore.js | 18 ++- 3 files changed, 95 insertions(+), 52 deletions(-) diff --git a/client/source/scripts/stores/BaseStore.js b/client/source/scripts/stores/BaseStore.js index 00c78787..34878407 100644 --- a/client/source/scripts/stores/BaseStore.js +++ b/client/source/scripts/stores/BaseStore.js @@ -6,17 +6,34 @@ export default class BaseStore extends EventEmitter { this.dispatcherID = null; this.on('uncaughtException', this.handleError); + this.requests = {}; this.setMaxListeners(20); } + beginRequest(id) { + this.requests[id] = true; + } + handleError(error) { console.trace(error); } + isRequestPending(id) { + if (this.requests[id] == null || this.requests[id] === false) { + return false; + } + + return true; + } + listen(event, callback) { this.on(event, callback); } + resolveRequest(id) { + this.requests[id] = false; + } + unlisten(event, callback) { this.removeListener(event, callback); } diff --git a/client/source/scripts/stores/TorrentStore.js b/client/source/scripts/stores/TorrentStore.js index 3ff20615..d1ed9f04 100644 --- a/client/source/scripts/stores/TorrentStore.js +++ b/client/source/scripts/stores/TorrentStore.js @@ -26,7 +26,10 @@ class TorrentStoreClass extends BaseStore { } fetchTorrentDetails() { - TorrentActions.fetchTorrentDetails(UIStore.getTorrentDetailsHash()); + if (!this.isRequestPending('fetch-torrent-details')) { + this.beginRequest('fetch-torrent-details'); + TorrentActions.fetchTorrentDetails(UIStore.getTorrentDetailsHash()); + } if (this.pollTorrentDetailsIntervalID === null) { this.startPollingTorrentDetails(); @@ -34,20 +37,46 @@ class TorrentStoreClass extends BaseStore { } fetchTorrents() { - TorrentActions.fetchTorrents(); + if (!this.isRequestPending('fetch-torrents')) { + this.beginRequest('fetch-torrents'); + TorrentActions.fetchTorrents(); + } if (this.pollTorrentsIntervalID === null) { this.startPollingTorrents(); } } - getTorrentDetails(hash) { - return this.torrents[hash].details || {}; + filterTorrents() { + let searchFilter = TorrentFilterStore.getSearchFilter(); + let statusFilter = TorrentFilterStore.getStatusFilter(); + let trackerFilter = TorrentFilterStore.getTrackerFilter(); + + let filteredTorrents = Object.assign([], this.sortedTorrents); + + if (searchFilter && searchFilter !== '') { + filteredTorrents = searchTorrents(filteredTorrents, searchFilter); + } + + if (statusFilter && statusFilter !== 'all') { + filteredTorrents = filterTorrents(filteredTorrents, { + type: 'status', + filter: statusFilter + }); + } + + if (trackerFilter && trackerFilter !== 'all') { + filteredTorrents = filterTorrents(filteredTorrents, { + type: 'tracker', + filter: trackerFilter + }); + } + + this.filteredTorrents = filteredTorrents; } - setTorrentDetails(hash, torrentDetails) { - this.torrents[hash].details = torrentDetails; - this.emit(EventTypes.CLIENT_TORRENT_DETAILS_CHANGE); + getTorrentDetails(hash) { + return this.torrents[hash].details || {}; } getSelectedTorrents() { @@ -74,16 +103,6 @@ class TorrentStoreClass extends BaseStore { this.emit(EventTypes.CLIENT_ADD_TORRENT_SUCCESS); } - setSelectedTorrents(event, hash) { - this.selectedTorrents = selectTorrents({ - event, - hash, - selectedTorrents: this.selectedTorrents, - torrentList: this.filteredTorrents - }); - this.emit(EventTypes.UI_TORRENT_SELECTION_CHANGE); - } - getTorrent(hash) { return this.torrents[hash]; } @@ -108,7 +127,31 @@ class TorrentStoreClass extends BaseStore { this.emit(EventTypes.CLIENT_MOVE_TORRENTS_REQUEST_ERROR); } - setTorrents(torrents) { + setSelectedTorrents(event, hash) { + this.selectedTorrents = selectTorrents({ + event, + hash, + selectedTorrents: this.selectedTorrents, + torrentList: this.filteredTorrents + }); + this.emit(EventTypes.UI_TORRENT_SELECTION_CHANGE); + } + + handleFetchTorrentsSuccess(torrents) { + this.sortTorrents(torrents); + this.filterTorrents(); + + this.emit(EventTypes.CLIENT_TORRENTS_REQUEST_SUCCESS); + this.resolveRequest('fetch-torrents'); + } + + setTorrentDetails(hash, torrentDetails) { + this.torrents[hash].details = torrentDetails; + this.emit(EventTypes.CLIENT_TORRENT_DETAILS_CHANGE); + this.resolveRequest('fetch-torrent-details'); + } + + sortTorrents(torrents) { let torrentsSort = TorrentFilterStore.getTorrentsSort(); this.torrents = torrents; @@ -116,34 +159,6 @@ class TorrentStoreClass extends BaseStore { // Convert torrents hash to array and sort it. this.sortedTorrents = sortTorrents(this.torrents, {direction: torrentsSort.direction, property: torrentsSort.value}); - - let searchFilter = TorrentFilterStore.getSearchFilter(); - let statusFilter = TorrentFilterStore.getStatusFilter(); - let trackerFilter = TorrentFilterStore.getTrackerFilter(); - - let filteredTorrents = Object.assign([], this.sortedTorrents); - - if (searchFilter && searchFilter !== '') { - filteredTorrents = searchTorrents(filteredTorrents, searchFilter); - } - - if (statusFilter && statusFilter !== 'all') { - filteredTorrents = filterTorrents(filteredTorrents, { - type: 'status', - filter: statusFilter - }); - } - - if (trackerFilter && trackerFilter !== 'all') { - filteredTorrents = filterTorrents(filteredTorrents, { - type: 'tracker', - filter: trackerFilter - }); - } - - this.filteredTorrents = filteredTorrents; - - this.emit(EventTypes.CLIENT_TORRENTS_REQUEST_SUCCESS); } startPollingTorrentDetails() { @@ -171,7 +186,7 @@ class TorrentStoreClass extends BaseStore { } triggerTorrentsFilter() { - this.setTorrents(this.torrents); + this.filterTorrents(); } } @@ -191,7 +206,7 @@ TorrentStore.dispatcherID = AppDispatcher.register((payload) => { TorrentStore.handleAddTorrentSuccess(action.data); break; case ActionTypes.CLIENT_FETCH_TORRENTS_SUCCESS: - TorrentStore.setTorrents(action.data.torrents); + TorrentStore.handleFetchTorrentsSuccess(action.data.torrents); break; case ActionTypes.CLIENT_MOVE_TORRENTS_SUCCESS: TorrentStore.handleMoveTorrentsSuccess(action.data); @@ -200,6 +215,7 @@ TorrentStore.dispatcherID = AppDispatcher.register((payload) => { TorrentStore.handleMoveTorrentsError(action.error); break; case ActionTypes.CLIENT_FETCH_TORRENTS_ERROR: + TorrentStore.handleFetchTorrentsError(); console.log(action); break; case ActionTypes.UI_CLICK_TORRENT: diff --git a/client/source/scripts/stores/TransferDataStore.js b/client/source/scripts/stores/TransferDataStore.js index 2529c948..384ea85b 100644 --- a/client/source/scripts/stores/TransferDataStore.js +++ b/client/source/scripts/stores/TransferDataStore.js @@ -16,11 +16,17 @@ class TransferDataStoreClass extends BaseStore { } fetchTransferData() { - ClientActions.fetchTransferHistory({ - snapshot: 'fiveMin' - }); + if (!this.isRequestPending('fetch-transfer-history')) { + this.beginRequest('fetch-transfer-history'); + ClientActions.fetchTransferHistory({ + snapshot: 'fiveMin' + }); + } - ClientActions.fetchTransferData(); + if (!this.isRequestPending('fetch-transfer-data')) { + this.beginRequest('fetch-transfer-data'); + ClientActions.fetchTransferData(); + } if (this.pollTransferDataID === null) { this.startPollingTransferData(); @@ -101,14 +107,17 @@ class TransferDataStoreClass extends BaseStore { }; this.emit(EventTypes.CLIENT_TRANSFER_DATA_REQUEST_SUCCESS); + this.resolveRequest('fetch-transfer-data'); } handleTransferDataError() { this.emit(EventTypes.CLIENT_TRANSFER_DATA_REQUEST_ERROR); + this.resolveRequest('fetch-transfer-data'); } handleTransferHistoryError(error) { this.emit(EventTypes.CLIENT_TRANSFER_HISTORY_REQUEST_ERROR); + this.resolveRequest('fetch-transfer-history'); } handleTransferHistorySuccess(transferData) { @@ -118,6 +127,7 @@ class TransferDataStoreClass extends BaseStore { }; this.emit(EventTypes.CLIENT_TRANSFER_HISTORY_REQUEST_SUCCESS); + this.resolveRequest('fetch-transfer-history'); } startPollingTransferData() {