github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/services/data-caches.js (about) 1 import Service, { inject as service } from '@ember/service'; 2 3 export const COLLECTION_CACHE_DURATION = 60000; // one minute 4 5 export default class DataCachesService extends Service { 6 @service router; 7 @service store; 8 @service system; 9 10 collectionLastFetched = {}; 11 12 async fetch(modelName) { 13 const modelNameToRoute = { 14 job: 'jobs', 15 node: 'clients', 16 }; 17 18 const route = modelNameToRoute[modelName]; 19 const lastFetched = this.collectionLastFetched[modelName]; 20 const now = Date.now(); 21 22 if (this.router.isActive(route)) { 23 // TODO Incorrect because it’s constantly being fetched by watchers, shouldn’t be marked as last fetched only on search 24 this.collectionLastFetched[modelName] = now; 25 return this.store.peekAll(modelName); 26 } else if (lastFetched && now - lastFetched < COLLECTION_CACHE_DURATION) { 27 return this.store.peekAll(modelName); 28 } else { 29 this.collectionLastFetched[modelName] = now; 30 return this.store.findAll(modelName); 31 } 32 } 33 }