]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: pass resolved value in types onAction
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 12 May 2021 09:30:05 +0000 (11:30 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 13 May 2021 11:08:03 +0000 (13:08 +0200)
src/store.ts
src/types.ts

index aacd6889d67101c71861dd8a450ecc60b5e6fc9e..a3726ad2f117616dda7b0876b1c257d1066baf49 100644 (file)
@@ -269,12 +269,14 @@ function buildStoreToUse<
       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
       }
 
@@ -282,12 +284,12 @@ function buildStoreToUse<
         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
       }
 
index a9a043b914eacabe0875907168e97a687d5949cb..1f04cf9bb2138fde4bff185bc2332ede98adc768 100644 (file)
@@ -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.