Close over lastMetric and cacheObj in cache.js

This commit is contained in:
John Karahalis 2019-12-06 13:55:48 -05:00
Родитель 864ec05b49
Коммит e837b86cc6
2 изменённых файлов: 22 добавлений и 21 удалений

Просмотреть файл

@ -26,7 +26,7 @@
// import AdjustableDate from './components/AdjustableDate.svelte';
import optionSet from "./stores/options.json";
import cache, { storeToQuery } from "./stores/cache";
import { createRequestCache, storeToQuery } from "./stores/cache";
import { store, settingChanged, modeIsImplemented } from "./stores/store";
// annotations
@ -45,6 +45,7 @@
export let name;
const menuOptions = Object.values(optionSet).filter(v => v.inMenu);
const cache = createRequestCache();
let visible = false;
// FIXME: this and all the buttons around exporting should be

Просмотреть файл

@ -6,9 +6,6 @@ import options from "./options.json";
import { fetchExploreData } from "./fetchData";
import { store } from "./store";
const cacheObj = {};
let lastMetric;
export const removeLocalParams = obj => {
const toRemove = Object.keys(obj).filter(f => {
return Object.keys(options)
@ -79,22 +76,25 @@ function setRanges(data, options = { setMinStartDate: false }) {
return data;
}
const cachedRequest = derived(store, $store => {
const queryParams = removeLocalParams($store);
const q = storeToQuery(queryParams);
const newMetric = queryParams.usage;
const metricChanged = lastMetric && lastMetric !== newMetric;
const setMinStartDate = !$store.startDate || metricChanged;
export function createRequestCache() {
const cacheObj = {};
let lastMetric;
if ($store.mode !== "explore") return undefined;
if (!(q in cacheObj)) {
cacheObj[q] = fetchExploreData(queryParams, q);
}
const promise = cacheObj[q].then(data =>
setRanges(data, { setMinStartDate })
);
lastMetric = newMetric;
return promise;
});
return derived(store, $store => {
const queryParams = removeLocalParams($store);
const q = storeToQuery(queryParams);
const newMetric = queryParams.usage;
const metricChanged = lastMetric && lastMetric !== newMetric;
const setMinStartDate = !$store.startDate || metricChanged;
export default cachedRequest;
if ($store.mode !== "explore") return undefined;
if (!(q in cacheObj)) {
cacheObj[q] = fetchExploreData(queryParams, q);
}
const promise = cacheObj[q].then(data =>
setRanges(data, { setMinStartDate })
);
lastMetric = newMetric;
return promise;
});
}