Created
August 20, 2024 15:47
-
-
Save Valian/186b300830211f75bfac6567e4bdb650 to your computer and use it in GitHub Desktop.
usePushEvent - similar to useFetch, but for live_vue library
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 usePushEvent<T extends object, Params extends object = object>( | |
eventName: string, | |
defaultValue?: T | |
) { | |
const live = useLiveVue(); | |
const isLoading = ref(false); | |
const isFinished = ref(false); | |
const response = ref<T | undefined>(defaultValue); | |
const error = ref<string | null>(null); | |
const execute = (data: Params) => { | |
isLoading.value = true; | |
isFinished.value = false; | |
live.pushEvent(eventName, data, (reply) => { | |
response.value = reply; | |
error.value = null; | |
isLoading.value = false; | |
isFinished.value = true; | |
}); | |
}; | |
return { execute, isLoading, response, error, isFinished }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment