-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 } | ||
} |