This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function defineLazyComponent(loader: () => Promise<any>) { | |
| return defineAsyncComponent({ | |
| loader, | |
| suspensible: false, | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useAbortableFetch() { | |
| let controller: AbortController | null = null | |
| return async function fetchWithAbort<T>(url: string, opts: any = {}) { | |
| if (controller) controller.abort() | |
| controller = new AbortController() | |
| try { | |
| return await $fetch<T>(url, { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useDebouncedRef<T>(value: T, delay = 300) { | |
| const state = ref(value) | |
| const debounced = ref(value) | |
| let timer: any | |
| watch(state, (val) => { | |
| clearTimeout(timer) | |
| timer = setTimeout(() => { | |
| debounced.value = val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useAsyncDataSafe<T>( | |
| key: string, | |
| handler: () => Promise<T> | |
| ) { | |
| return useAsyncData<T>(key, handler, { | |
| server: true, | |
| lazy: false, | |
| default: () => null, | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function useApi() { | |
| const config = useRuntimeConfig() | |
| const token = useCookie('token') | |
| return async function apiFetch<T>(url: string, opts: any = {}): Promise<T> { | |
| try { | |
| return await $fetch<T>(url, { | |
| baseURL: config.public.apiBase, | |
| headers: { | |
| Authorization: token.value ? `Bearer ${token.value}` : undefined, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function groupBy(arr, keyFn) { | |
| return arr.reduce((acc, item) => { | |
| const key = keyFn(item); | |
| (acc[key] ||= []).push(item); | |
| return acc; | |
| }, {}); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export async function retry(fn, { | |
| retries = 3, | |
| delay = 300, | |
| factor = 2 | |
| } = {}) { | |
| let attempt = 0; | |
| while (true) { | |
| try { | |
| return await fn(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function debounceAsync(fn, delay = 300) { | |
| let timer; | |
| let rejectPrev; | |
| return (...args) => { | |
| if (timer) clearTimeout(timer); | |
| if (rejectPrev) rejectPrev({ canceled: true }); | |
| return new Promise((resolve, reject) => { | |
| rejectPrev = reject; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function pLimit(concurrency) { | |
| const queue = []; | |
| let activeCount = 0; | |
| const next = () => { | |
| if (queue.length === 0 || activeCount >= concurrency) return; | |
| activeCount++; | |
| const { fn, resolve, reject } = queue.shift(); |
NewerOlder