const args = Array.from(arguments)
const localStore = this || store
- let afterCallback: () => void = noop
+ let afterCallback: (
+ resolvedReturn: ReturnType<typeof actions[typeof actionName]>
+ ) => 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
}
callback({ args, name: actionName, store: localStore, after, onError })
})
- let ret
-
+ let ret: ReturnType<typeof actions[typeof actionName]>
try {
ret = actions[actionName].apply(localStore, args as unknown as any[])
Promise.resolve(ret).then(afterCallback).catch(onErrorCallback)
} catch (error) {
+ onErrorCallback(error)
throw error
}
*/
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.