From 2a080f2ec2eb57b9c01ecebfde2a9996e424f729 Mon Sep 17 00:00:00 2001 From: Pierre Romera Date: Thu, 3 Oct 2024 12:06:38 +0000 Subject: [PATCH] feat: add composable for vue-wait --- src/composables/wait.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/composables/wait.js diff --git a/src/composables/wait.js b/src/composables/wait.js new file mode 100644 index 0000000000..068abd0250 --- /dev/null +++ b/src/composables/wait.js @@ -0,0 +1,28 @@ +import { getCurrentInstance } from 'vue' + +export function useWait() { + const instance = getCurrentInstance() + + // If `getCurrentInstance` is called outside of a Vue component setup function, it will return `null`. + if (!instance) { + throw new Error('useCore must be called within a Vue component setup function.') + } + + // The `instance` object has a `proxy` property, which is the component's public instance. + // `proxy.$wait` gives us access to the global `$wait` object provided by vue-wait + const { + proxy: { $wait: wait } + } = instance + + // `waitFor` is a higher-order function that takes an `id` and a function `fn`. + const waitFor = (id, fn) => { + return async (...args) => { + wait.start(id) + const promise = await fn(...args) + wait.end(id) + return promise + } + } + + return { wait, waitFor } +}