@@ -19,7 +19,6 @@ import {
19
19
toRefs ,
20
20
Ref ,
21
21
ref ,
22
- nextTick ,
23
22
} from 'vue'
24
23
import {
25
24
StateTree ,
@@ -250,7 +249,7 @@ function createSetupStore<
250
249
if ( isListening ) {
251
250
debuggerEvents = event
252
251
// avoid triggering this while the store is being built and the state is being set in pinia
253
- } else if ( isListening == false && ! store . _hotUpdating ) {
252
+ } else if ( isListening === false && ! store . _hotUpdating ) {
254
253
// let patch send all the events together later
255
254
/* istanbul ignore else */
256
255
if ( Array . isArray ( debuggerEvents ) ) {
@@ -265,8 +264,8 @@ function createSetupStore<
265
264
}
266
265
267
266
// internal state
268
- let isListening : boolean // set to true at the end
269
- let isSyncListening : boolean // set to true at the end
267
+ let isListening = false // set to true at the end
268
+ let shouldTrigger = false // The initial value does not matter, and no need to set to true at the end
270
269
let subscriptions : Set < SubscriptionCallback < S > > = new Set ( )
271
270
let actionSubscriptions : Set < StoreOnActionListener < Id , S , G , A > > = new Set ( )
272
271
let debuggerEvents : DebuggerEvent [ ] | DebuggerEvent
@@ -281,9 +280,6 @@ function createSetupStore<
281
280
282
281
const hotState = ref ( { } as S )
283
282
284
- // avoid triggering too many listeners
285
- // https://github.com/vuejs/pinia/issues/1129
286
- let activeListener : Symbol | undefined
287
283
function $patch ( stateMutation : ( state : UnwrapRef < S > ) => void ) : void
288
284
function $patch ( partialState : _DeepPartial < UnwrapRef < S > > ) : void
289
285
function $patch (
@@ -292,7 +288,7 @@ function createSetupStore<
292
288
| ( ( state : UnwrapRef < S > ) => void )
293
289
) : void {
294
290
let subscriptionMutation : SubscriptionCallbackMutation < S >
295
- isListening = isSyncListening = false
291
+ isListening = false
296
292
// reset the debugger events since patches are sync
297
293
/* istanbul ignore else */
298
294
if ( __DEV__ ) {
@@ -314,13 +310,7 @@ function createSetupStore<
314
310
events : debuggerEvents as DebuggerEvent [ ] ,
315
311
}
316
312
}
317
- const myListenerId = ( activeListener = Symbol ( ) )
318
- nextTick ( ) . then ( ( ) => {
319
- if ( activeListener === myListenerId ) {
320
- isListening = true
321
- }
322
- } )
323
- isSyncListening = true
313
+ isListening = true
324
314
// because we paused the watcher, we need to manually call the subscriptions
325
315
triggerSubscriptions (
326
316
subscriptions ,
@@ -444,11 +434,19 @@ function createSetupStore<
444
434
options . detached ,
445
435
( ) => stopWatcher ( )
446
436
)
447
- const stopWatcher = scope . run ( ( ) =>
448
- watch (
437
+ const stopWatcher = scope . run ( ( ) => {
438
+ const stop1 = watch (
439
+ ( ) => pinia . state . value [ $id ] ,
440
+ ( ) => {
441
+ shouldTrigger = isListening
442
+ } ,
443
+ { deep : true , flush : 'sync' }
444
+ )
445
+
446
+ const stop2 = watch (
449
447
( ) => pinia . state . value [ $id ] as UnwrapRef < S > ,
450
448
( state ) => {
451
- if ( options . flush === 'sync' ? isSyncListening : isListening ) {
449
+ if ( shouldTrigger ) {
452
450
callback (
453
451
{
454
452
storeId : $id ,
@@ -461,7 +459,14 @@ function createSetupStore<
461
459
} ,
462
460
assign ( { } , $subscribeOptions , options )
463
461
)
464
- ) !
462
+
463
+ const stop = ( ) => {
464
+ stop1 ( )
465
+ stop2 ( )
466
+ }
467
+
468
+ return stop
469
+ } ) !
465
470
466
471
return removeSubscription
467
472
} ,
@@ -617,12 +622,8 @@ function createSetupStore<
617
622
618
623
// avoid devtools logging this as a mutation
619
624
isListening = false
620
- isSyncListening = false
621
625
pinia . state . value [ $id ] = toRef ( newStore . _hmrPayload , 'hotState' )
622
- isSyncListening = true
623
- nextTick ( ) . then ( ( ) => {
624
- isListening = true
625
- } )
626
+ isListening = true
626
627
627
628
for ( const actionName in newStore . _hmrPayload . actions ) {
628
629
const actionFn : _Method = newStore [ actionName ]
@@ -751,7 +752,6 @@ function createSetupStore<
751
752
}
752
753
753
754
isListening = true
754
- isSyncListening = true
755
755
return store
756
756
}
757
757
0 commit comments