Skip to content

Commit 6c17168

Browse files
committed
fix: avoid multiple subscriptions with empty promises
Fix #1129
1 parent dbc59d1 commit 6c17168

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/pinia/__tests__/subscriptions.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe('Subscriptions', () => {
202202
expect(spy2).toHaveBeenCalledTimes(1)
203203
})
204204

205-
it.skip('triggers pre subscriptions only once on $patch', async () => {
205+
it('triggers pre subscriptions only once on $patch', async () => {
206206
const s1 = useStore()
207207
const spy1 = jest.fn()
208208

@@ -216,7 +216,7 @@ describe('Subscriptions', () => {
216216
// adding an extra await works
217217
// await false
218218
// adding any other delay also works
219-
await delay(20)
219+
// await delay(20)
220220
// await nextTick()
221221
expect(spy1).toHaveBeenCalledTimes(1)
222222
expect(spy1).not.toHaveBeenCalledWith(

packages/pinia/src/store.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ function createSetupStore<
252252

253253
const hotState = ref({} as S)
254254

255+
// avoid triggering too many listeners
256+
// https://github.com/vuejs/pinia/issues/1129
257+
let activeListener: Symbol | undefined
255258
function $patch(stateMutation: (state: UnwrapRef<S>) => void): void
256259
function $patch(partialState: _DeepPartial<UnwrapRef<S>>): void
257260
function $patch(
@@ -282,8 +285,11 @@ function createSetupStore<
282285
events: debuggerEvents as DebuggerEvent[],
283286
}
284287
}
288+
const myListenerId = (activeListener = Symbol())
285289
nextTick().then(() => {
286-
isListening = true
290+
if (activeListener === myListenerId) {
291+
isListening = true
292+
}
287293
})
288294
isSyncListening = true
289295
// because we paused the watcher, we need to manually call the subscriptions

0 commit comments

Comments
 (0)