forked from Fuphoenixes/piniaPluginLoading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
29 lines (29 loc) · 830 Bytes
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// src/index.ts
import { ref } from "vue-demi";
function PiniaLoading({ options, store }) {
if (options.actions) {
const $loading = {};
Object.keys(options.actions).forEach((actionKey) => {
const originAction = options.actions[actionKey];
const action = function(...args) {
const rtn = originAction.apply(this, args);
if (rtn instanceof Promise) {
$loading[actionKey] = ref(false);
return new Promise((resolve, reject) => {
$loading[actionKey].value = true;
rtn.then(resolve).catch(reject).finally(() => {
$loading[actionKey].value = false;
});
});
} else {
return rtn;
}
};
store[actionKey] = action;
});
store.$loading = $loading;
}
}
export {
PiniaLoading
};