ee7141a59a
This introduces a DataCaches service so recently-updated collections don’t need to be requeried within a minute, or based on the current route. It only searches jobs and nodes. There are known bugs that will be addressed in upcoming PRs.
34 lines
1 KiB
JavaScript
34 lines
1 KiB
JavaScript
import Service, { inject as service } from '@ember/service';
|
||
|
||
export const COLLECTION_CACHE_DURATION = 60000; // one minute
|
||
|
||
export default class DataCachesService extends Service {
|
||
@service router;
|
||
@service store;
|
||
@service system;
|
||
|
||
collectionLastFetched = {};
|
||
|
||
async fetch(modelName) {
|
||
const modelNameToRoute = {
|
||
job: 'jobs',
|
||
node: 'clients',
|
||
};
|
||
|
||
const route = modelNameToRoute[modelName];
|
||
const lastFetched = this.collectionLastFetched[modelName];
|
||
const now = Date.now();
|
||
|
||
if (this.router.isActive(route)) {
|
||
// TODO Incorrect because it’s constantly being fetched by watchers, shouldn’t be marked as last fetched only on search
|
||
this.collectionLastFetched[modelName] = now;
|
||
return this.store.peekAll(modelName);
|
||
} else if (lastFetched && now - lastFetched < COLLECTION_CACHE_DURATION) {
|
||
return this.store.peekAll(modelName);
|
||
} else {
|
||
this.collectionLastFetched[modelName] = now;
|
||
return this.store.findAll(modelName);
|
||
}
|
||
}
|
||
}
|