Files
pikasTech-HWLAB/web/hwlab-cloud-web/src/composables/useTableLoader.ts
T
2026-06-12 01:17:11 +08:00

23 lines
778 B
TypeScript

import { computed, ref, type ComputedRef, type Ref } from "vue";
export function useTableLoader<T>(loader: () => Promise<T[]>): { rows: Ref<T[]>; loading: Ref<boolean>; error: Ref<string | null>; empty: ComputedRef<boolean>; reload: () => Promise<void> } {
const rows = ref([]) as Ref<T[]>;
const loading = ref(false);
const error = ref<string | null>(null);
const empty = computed(() => !loading.value && rows.value.length === 0);
async function reload(): Promise<void> {
loading.value = true;
error.value = null;
try {
rows.value = await loader();
} catch (err) {
error.value = err instanceof Error ? err.message : String(err);
} finally {
loading.value = false;
}
}
return { rows, loading, error, empty, reload };
}