open-nomad/ui/app/services/data-caches.js
Buck Doyle ee7141a59a
Add phase 1 of global search (#8175)
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.
2020-06-19 13:05:28 -05:00

34 lines
1 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);
}
}
}