]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: allow actions to be destructured
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 21 Jul 2021 16:00:17 +0000 (18:00 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 21 Jul 2021 16:00:17 +0000 (18:00 +0200)
__tests__/actions.spec.ts
src/store.ts

index ecd507ab14769c1aa1a70f531e549935fda70385..36d7e2c775425d9ba2fdf3fdb2a39b9e02c5a47d 100644 (file)
@@ -25,6 +25,11 @@ describe('Actions', () => {
         async getNonA() {
           return this.nonA
         },
+        simple() {
+          this.toggle()
+          return 'simple'
+        },
+
         toggle() {
           return (this.a = !this.a)
         },
@@ -211,4 +216,23 @@ describe('Actions', () => {
     })
     await expect(store.getNonA()).resolves.toBe('hello')
   })
+
+  it('can destructure actions', () => {
+    const store = useStore()
+    const { simple } = store
+    expect(simple()).toBe('simple')
+    // works with the wrong this
+    expect({ simple }.simple()).toBe('simple')
+    // special this check
+    expect({ $id: 'o', simple }.simple()).toBe('simple')
+    // override the function like devtools do
+    expect(
+      {
+        $id: store.$id,
+        simple,
+        // otherwise it would faial
+        toggle() {},
+      }.simple()
+    ).toBe('simple')
+  })
 })
index b8ca5857eb19cc5155a1b08c96ff2aca4800da90..87dc9ea4de50745ea73ec73eb50bc6c492096f4d 100644 (file)
@@ -310,7 +310,7 @@ function createSetupStore<
 
       let ret: any
       try {
-        ret = action.apply(this || store, args)
+        ret = action.apply(this && this.$id === $id ? this : store, args)
         // handle sync errors
       } catch (error) {
         if (onErrorCallback(error) !== false) {