Cleanup
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
export const BASE = import.meta.env.DEV ? 'http://10.69.5.23' : '';
|
||||
import {ref, onUnmounted} from 'vue';
|
||||
|
||||
export type DataRow = Record<string, number | string | null>
|
||||
|
||||
export const BASE = import.meta.env.DEV ? 'http://10.69.5.23' : '';
|
||||
|
||||
const cache: any = {};
|
||||
const current = ref<any>(null);
|
||||
const hourly = ref<any[]>([]);
|
||||
const daily = ref<any[]>([]);
|
||||
|
||||
let interval: ReturnType<typeof setInterval>;
|
||||
let refCount = 0;
|
||||
|
||||
async function get<T>(path: string, params: Record<string, string> = {}): Promise<T> {
|
||||
const url = new URL(`${BASE}${path}`, window.location.origin);
|
||||
@@ -29,3 +37,24 @@ export const api = {
|
||||
...(fields ? {fields} : {})
|
||||
}),
|
||||
};
|
||||
|
||||
async function refresh() {
|
||||
const start = new Date(), end = new Date();
|
||||
start.setDate(start.getDate() + 1);
|
||||
end.setDate(end.getDate() + 5);
|
||||
current.value = await api.current();
|
||||
return {current}
|
||||
}
|
||||
|
||||
export function useWeather(ms = 5 * 60_000) {
|
||||
if(++refCount === 1) {
|
||||
refresh();
|
||||
interval = setInterval(refresh, ms);
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
if(--refCount === 0) clearInterval(interval);
|
||||
});
|
||||
|
||||
return {current, hourly, daily, refresh};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user