Skip to content

Instantly share code, notes, and snippets.

@Valian
Created August 20, 2024 15:47
Show Gist options
  • Save Valian/186b300830211f75bfac6567e4bdb650 to your computer and use it in GitHub Desktop.
Save Valian/186b300830211f75bfac6567e4bdb650 to your computer and use it in GitHub Desktop.
usePushEvent - similar to useFetch, but for live_vue library
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