import { get, writable } from "svelte/store";

export const loading_store = writable(0)

export const loading_start = () => {
	loading_store.set(get(loading_store) + 1)
}

export const loading_finish = () => {
	loading_store.set(get(loading_store) - 1)
}

export const loading_run = async <T>(fn: () => Promise<T>): Promise<T> => {
	try {
		loading_start()
		return await fn()
	} finally {
		loading_finish()
	}
}
