Skip to content

Commit

Permalink
feat: add composable for vue-wait
Browse files Browse the repository at this point in the history
  • Loading branch information
pirhoo committed Oct 3, 2024
1 parent 2749c97 commit 2a080f2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/composables/wait.js
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit 2a080f2

Please sign in to comment.