Просмотр исходного кода

Cache Wizard Networks' last server response

Use cache only if appropriate (i.e. options have not changed, instances
have not changed etc.).
This should cut down a lot on waiting time and server requests count if
the user clicks `Back` and `Next` buttons frequently.
Sergiu Miclea 8 лет назад
Родитель
Сommit
c82c42c61e

+ 7 - 0
src/actions/InstanceActions.js

@@ -103,6 +103,13 @@ class InstanceActions {
   }
 
   loadInstancesDetails(endpointId, instances) {
+    let store = InstanceStore.getState()
+    instances.sort((a, b) => a.instance_name.localeCompare(b.instance_name))
+    let hash = i => `${i.instance_name}-${i.id}`
+    if (store.instancesDetails.map(hash).join('_') === instances.map(hash).join('_')) {
+      return { fromCache: true }
+    }
+
     instances.forEach(instance => {
       InstanceSource.loadInstanceDetails(endpointId, instance.instance_name).then(
         instance => { this.loadInstanceDetailsSuccess(instance) },

+ 11 - 4
src/actions/NetworkActions.js

@@ -15,19 +15,26 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import alt from '../alt'
 
 import NetworkSource from '../sources/NetworkSource'
+import NetworkStore from '../stores/NetworkStore'
 
 class NetworkActions {
   loadNetworks(endpointId, environment) {
+    let storedCacheId = NetworkStore.getState().cacheId
+    let cacheId = `${endpointId}-${btoa(JSON.stringify(environment))}`
+    if (cacheId === storedCacheId) {
+      return { fromCache: true }
+    }
+
     NetworkSource.loadNetworks(endpointId, environment).then(
-      networks => { this.loadNetworksSuccess(networks) },
+      networks => { this.loadNetworksSuccess(networks, cacheId) },
       response => { this.loadNetworksFailed(response) }
     )
 
-    return true
+    return { fromCache: false }
   }
 
-  loadNetworksSuccess(networks) {
-    return networks
+  loadNetworksSuccess(networks, cacheId) {
+    return { networks, cacheId }
   }
 
   loadNetworksFailed(response) {

+ 1 - 1
src/components/pages/WizardPage/WizardPage.jsx

@@ -35,8 +35,8 @@ import WizardStore from '../../../stores/WizardStore'
 import WizardActions from '../../../actions/WizardActions'
 import InstanceStore from '../../../stores/InstanceStore'
 import InstanceActions from '../../../actions/InstanceActions'
-import NetworkActions from '../../../actions/NetworkActions'
 import NetworkStore from '../../../stores/NetworkStore'
+import NetworkActions from '../../../actions/NetworkActions'
 import NotificationActions from '../../../actions/NotificationActions'
 import ReplicaActions from '../../../actions/ReplicaActions'
 import ScheduleActions from '../../../actions/ScheduleActions'

+ 5 - 1
src/stores/InstanceStore.js

@@ -174,7 +174,11 @@ class InstanceStore {
     this.searchNotFound = true
   }
 
-  handleLoadInstancesDetails({ count }) {
+  handleLoadInstancesDetails({ count, fromCache }) {
+    if (fromCache) {
+      return
+    }
+
     this.loadingInstancesDetails = true
     this.instancesDetailsCount = count
     this.instancesDetails = []

+ 8 - 2
src/stores/NetworkStore.js

@@ -19,6 +19,7 @@ class NetworkStore {
   constructor() {
     this.networks = []
     this.loading = false
+    this.cacheId = null
 
     this.bindListeners({
       handleLoadNetworks: NetworkActions.LOAD_NETWORKS,
@@ -27,13 +28,18 @@ class NetworkStore {
     })
   }
 
-  handleLoadNetworks() {
+  handleLoadNetworks({ fromCache }) {
+    if (fromCache) {
+      return
+    }
+
     this.loading = true
   }
 
-  handleLoadNetworksSuccess(networks) {
+  handleLoadNetworksSuccess({ networks, cacheId }) {
     this.loading = false
     this.networks = networks
+    this.cacheId = cacheId
   }
 
   handleLoadNetworksFailed() {