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: v2.0.0-alpha.17~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20afff221e351c2366e8fb7c7fb05ca2fe355476;p=thirdparty%2Fvuejs%2Fpinia.git refactor: pass resolved value in types onAction --- diff --git a/src/store.ts b/src/store.ts index e8b7e891..31064a00 100644 --- a/src/store.ts +++ b/src/store.ts @@ -296,12 +296,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 } @@ -309,12 +311,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 4c21fa70..cd48cf30 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.