]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix: avoid multiple subscriptions with empty promises
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 2 May 2022 09:13:16 +0000 (11:13 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 2 May 2022 09:13:16 +0000 (11:13 +0200)
Fix #1129

packages/pinia/__tests__/subscriptions.spec.ts
packages/pinia/src/store.ts

index 4322792c30cffa5ac6bb25e6c131cb71b2847d0d..cfc928d14b3377ea7f73d5ab039b4a784e62775b 100644 (file)
@@ -202,7 +202,7 @@ describe('Subscriptions', () => {
       expect(spy2).toHaveBeenCalledTimes(1)
     })
 
-    it.skip('triggers pre subscriptions only once on $patch', async () => {
+    it('triggers pre subscriptions only once on $patch', async () => {
       const s1 = useStore()
       const spy1 = jest.fn()
 
@@ -216,7 +216,7 @@ describe('Subscriptions', () => {
       // adding an extra await works
       // await false
       // adding any other delay also works
-      await delay(20)
+      // await delay(20)
       // await nextTick()
       expect(spy1).toHaveBeenCalledTimes(1)
       expect(spy1).not.toHaveBeenCalledWith(
index 3d5f3db9d0dd3d5ac1381373a70527ffee25f903..65bd466a12ed5eb6ed0777ad2f66c3ad766c2414 100644 (file)
@@ -252,6 +252,9 @@ function createSetupStore<
 
   const hotState = ref({} as S)
 
+  // avoid triggering too many listeners
+  // https://github.com/vuejs/pinia/issues/1129
+  let activeListener: Symbol | undefined
   function $patch(stateMutation: (state: UnwrapRef<S>) => void): void
   function $patch(partialState: _DeepPartial<UnwrapRef<S>>): void
   function $patch(
@@ -282,8 +285,11 @@ function createSetupStore<
         events: debuggerEvents as DebuggerEvent[],
       }
     }
+    const myListenerId = (activeListener = Symbol())
     nextTick().then(() => {
-      isListening = true
+      if (activeListener === myListenerId) {
+        isListening = true
+      }
     })
     isSyncListening = true
     // because we paused the watcher, we need to manually call the subscriptions