From 20afff221e351c2366e8fb7c7fb05ca2fe355476 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 12 May 2021 11:30:05 +0200 Subject: [PATCH] refactor: pass resolved value in types onAction --- src/store.ts | 12 +++++++----- src/types.ts | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) 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. -- 2.47.3