open-nomad/ui/app/services/data-caches.js

34 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 its constantly being fetched by watchers, shouldnt 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);
}
}
}