Last active
May 20, 2020 07:35
-
-
Save moreta/2c8a718534d398db6de861fde2f8c390 to your computer and use it in GitHub Desktop.
Event loading sample code for Vue Composition Api
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
import { ref } from '@vue/composition-api' | |
function logDuration<T extends (...args: any[]) => any> (func: T): (...funcArgs: Parameters<T>) => Promise<ReturnType<T>> { | |
const funcName = func.name | |
return async (...args: Parameters<T>): Promise<ReturnType<T>> => { | |
console.time(funcName) | |
const results = await func(...args) | |
console.timeEnd(funcName) | |
return results | |
} | |
} | |
export function useEventLoading<T extends (...args: any[]) => any> (func: T) { | |
const eventLoading = ref(false) | |
const eventCall = async (...args: Parameters<T>): Promise<ReturnType<T>> => { | |
eventLoading.value = true | |
const results = await logDuration(func)(...args) | |
eventLoading.value = false | |
return results | |
} | |
return { eventLoading, eventCall } | |
} |
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
<template> | |
<section class="main"> | |
<v-overlay :value="eventLoading"> | |
<v-progress-circular indeterminate size="64"></v-progress-circular> | |
</v-overlay> | |
<v-btn @click="handleSubmit">SUBMIT</v-btn> | |
</section> | |
</template> | |
<script lang="ts"> | |
import { useEventLoading } from '@/composables/use-event-loading' | |
import { defineComponent, ref } from '@vue/composition-api' | |
export default defineComponent({ | |
name: 'Sample', | |
setup () { | |
const { | |
myGreateApi | |
} = useApiSerivce() | |
const { | |
eventLoading, | |
eventCall | |
} = useEventLoading(async function myGreateApiCallFunc () { | |
await myGreateApi() | |
}) | |
const handleSubmit = async () => { | |
return await eventCall() | |
} | |
return { | |
eventLoading, | |
handleSubmit | |
} | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment