Skip to content

Commit

Permalink
fix: 优化$loading
Browse files Browse the repository at this point in the history
  • Loading branch information
004059 committed Nov 18, 2021
1 parent f631418 commit 486ee45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pinia-plugin-loading",
"version": "1.0.5",
"version": "1.0.7",
"description": "Auto loading data binding plugin for pinia. You don't need to write showLoading and hideLoading any more.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ export function PiniaLoading({ options, store }: PiniaPluginContext) {
const $loading: Record<string, Ref<Boolean>> = {}
Object.keys(options.actions).forEach((actionKey) => {
const originAction = options.actions[actionKey]
const loading = ref(false)
const action = function(this: unknown, ...args : unknown[]) {
const rtn = originAction.apply(this, args)
if (rtn instanceof Promise) {
$loading[actionKey] = ref(false)
return new Promise((resolve, reject) => {
loading.value = true
$loading[actionKey].value = true
rtn
.then(resolve)
.catch(reject)
.finally(() => {
loading.value = false
$loading[actionKey].value = false
})
})
} else {
return rtn
}
}
store[actionKey] = action
$loading[actionKey] = loading
})

store.$loading = $loading
}
}

declare module 'pinia' {
// eslint-disable-next-line
export interface PiniaCustomProperties<Id, S, G, A> {
$loading: {
[K in keyof A as A[K] extends () => Promise<any> ? K : never ]: Ref<Boolean>;
Expand Down

0 comments on commit 486ee45

Please sign in to comment.