gQuery/src/index.ts

40 lines
716 B
TypeScript
Raw Normal View History

2021-09-30 16:19:53 +00:00
import { writable, get } from "svelte/store";
const newGCache = () => {
const { subscribe, update, set } = writable({});
async function hydrate(newData) {
update((old) => {
return {
...old,
...newData,
};
});
}
return {
subscribe,
set,
update,
hydrate,
};
};
export const gCache = newGCache();
export async function gQuery(typename, { query, variables }) {
const current = get(gCache);
// Extremely Naive cache
// Just checks to see if the data is there, if it is, don't
// Hit the network again
if (!current?.[typename]) {
const newData = await query({
variables,
fetch,
});
await gCache.hydrate(newData);
}
}