Skip to content

Commit 151de30

Browse files
authored
Vue 3 support for vue-wait (#106)
1 parent 133de26 commit 151de30

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ yarn add vue-wait
3131
```
3232

3333
### 2. Require:
34+
#### For Vue 2.x
3435
```js
3536
import VueWait from 'vue-wait'
3637

@@ -42,6 +43,19 @@ new Vue({
4243
})
4344
```
4445

46+
#### For Vue 3.x
47+
```js
48+
import { createApp } from 'vue'
49+
import { createVueWait } from 'vue-wait'
50+
import App from './App.vue'
51+
52+
const VueWait = createVueWait()
53+
54+
createApp(App) // Create app with root component
55+
.use(VueWait) // Register vue-wait
56+
.mount('#app')
57+
```
58+
4559
### 3. Use in Your Components
4660

4761
```vue

src/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,5 @@ export interface VueWaitOptions{
117117
export function mapWaitingGetters(getters: any): any;
118118

119119
export function mapWaitingActions(vuexModuleName: any, actions?: any): any;
120+
121+
export function createVueWait(options?: VueWaitOptions): Object;

src/vue-wait.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class VueWait {
3838
}
3939

4040
init(Vue, store) {
41-
if (nodeIsDebug() && !install.installed) {
41+
if (nodeIsDebug() && !install.installed && VueWait.getVueVersion(Vue) < 3) {
4242
console.warn(
4343
`[vue-wait] not installed. Make sure to call \`Vue.use(VueWait)\` before init root instance.`
4444
);
@@ -67,16 +67,25 @@ export default class VueWait {
6767
store.registerModule(vuexModuleName, vuexStore);
6868
}
6969

70-
this.stateHandler = new Vue({
70+
const config = {
7171
computed: {
7272
is: () => waiter => store.getters[`${vuexModuleName}/is`](waiter),
7373
any: () => store.getters[`${vuexModuleName}/any`],
7474
percent: () => waiter =>
7575
store.getters[`${vuexModuleName}/percent`](waiter)
7676
}
77-
});
77+
};
78+
79+
if (VueWait.getVueVersion(Vue) > 2) {
80+
const { createApp } = require('vue');
81+
this.stateHandler = createApp(config).mount(
82+
document.createElement('div')
83+
);
84+
} else {
85+
this.stateHandler = new Vue(config);
86+
}
7887
} else {
79-
this.stateHandler = new Vue({
88+
const config = {
8089
data() {
8190
return {
8291
waitingFor: [],
@@ -106,7 +115,16 @@ export default class VueWait {
106115
this.progresses = progress(this.progresses, waiter, current, total);
107116
}
108117
}
109-
});
118+
};
119+
120+
if (VueWait.getVueVersion(Vue) > 2) {
121+
const { createApp } = require('vue');
122+
this.stateHandler = createApp(config).mount(
123+
document.createElement('div')
124+
);
125+
} else {
126+
this.stateHandler = new Vue(config);
127+
}
110128
}
111129

112130
this.initialized = true;
@@ -116,6 +134,10 @@ export default class VueWait {
116134
return this.stateHandler.any;
117135
}
118136

137+
static getVueVersion(app) {
138+
return parseFloat((app.version || '').split('.')[0] || 0);
139+
}
140+
119141
is(waiter) {
120142
return this.stateHandler.is(waiter);
121143
}
@@ -208,7 +230,34 @@ export function install(Vue) {
208230
install.installed = true;
209231
}
210232

233+
function createVueWait(options) {
234+
const Wait = {
235+
async install(app) {
236+
if (this.installed && app) {
237+
if (nodeIsDebug()) {
238+
console.warn('[vue-wait] already installed');
239+
}
240+
return;
241+
}
242+
243+
const instance = new VueWait(options);
244+
instance.init(app, app.config.globalProperties.$store);
245+
app.config.globalProperties[instance.options.accessorName] = instance;
246+
247+
app.mixin({
248+
beforeCreate() {
249+
this.__$waitInstance = instance;
250+
}
251+
});
252+
253+
this.installed = true;
254+
}
255+
};
256+
257+
return Wait;
258+
}
259+
211260
// Export which are imported to export
212-
export { mapWaitingActions, mapWaitingGetters, waitFor };
261+
export { mapWaitingActions, mapWaitingGetters, waitFor, createVueWait };
213262

214263
VueWait.install = install;

0 commit comments

Comments
 (0)