From: Eduardo San Martin Morote Date: Wed, 12 May 2021 09:30:05 +0000 (+0200) Subject: refactor: pass resolved value in types onAction X-Git-Tag: v0.5.0~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d5c83eb9c7e9450f151c4ae58984f63aa6fe9829;p=thirdparty%2Fvuejs%2Fpinia.git refactor: pass resolved value in types onAction --- diff --git a/src/store.ts b/src/store.ts index aacd6889..a3726ad2 100644 --- a/src/store.ts +++ b/src/store.ts @@ -269,12 +269,14 @@ function buildStoreToUse< const args = Array.from(arguments) const localStore = this || store - let afterCallback: () => void = noop + let afterCallback: ( + resolvedReturn: ReturnType + ) => void = noop let onErrorCallback: (error: unknown) => void = noop - function after(callback: () => void) { + function after(callback: typeof afterCallback) { afterCallback = callback } - function onError(callback: (error: unknown) => void) { + function onError(callback: typeof onErrorCallback) { onErrorCallback = callback } @@ -282,12 +284,12 @@ function buildStoreToUse< callback({ args, name: actionName, store: localStore, after, onError }) }) - let ret - + let ret: ReturnType try { ret = actions[actionName].apply(localStore, args as unknown as any[]) Promise.resolve(ret).then(afterCallback).catch(onErrorCallback) } catch (error) { + onErrorCallback(error) throw error } diff --git a/src/types.ts b/src/types.ts index a9a043b9..1f04cf9b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,9 +64,10 @@ export enum MutationType { */ export interface StoreOnActionListenerContext { /** - * Sets up a hook once the action is finished. + * Sets up a hook once the action is finished. It receives the return value of + * the action, if it's a Promise, it will be unwrapped. */ - after: (callback: () => void) => void + after: (callback: (resolvedReturn: unknown) => void) => void /** * Sets up a hook if the action fails.