@@ -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 ,
@@ -247,7 +246,7 @@ function createSetupStore<
247
246
if ( isListening ) {
248
247
debuggerEvents = event
249
248
// avoid triggering this while the store is being built and the state is being set in pinia
250
- } else if ( isListening == false && ! store . _hotUpdating ) {
249
+ } else if ( isListening === false && ! store . _hotUpdating ) {
251
250
// let patch send all the events together later
252
251
/* istanbul ignore else */
253
252
if ( Array . isArray ( debuggerEvents ) ) {
@@ -262,8 +261,8 @@ function createSetupStore<
262
261
}
263
262
264
263
// internal state
265
- let isListening : boolean // set to true at the end
266
- let isSyncListening : boolean // set to true at the end
264
+ let isListening = false // set to true at the end
265
+ let shouldTrigger = false // The initial value does not matter, and no need to set to true at the end
267
266
let subscriptions : SubscriptionCallback < S > [ ] = [ ]
268
267
let actionSubscriptions : StoreOnActionListener < Id , S , G , A > [ ] = [ ]
269
268
let debuggerEvents : DebuggerEvent [ ] | DebuggerEvent
@@ -278,9 +277,6 @@ function createSetupStore<
278
277
279
278
const hotState = ref ( { } as S )
280
279
281
- // avoid triggering too many listeners
282
- // https://github.com/vuejs/pinia/issues/1129
283
- let activeListener : Symbol | undefined
284
280
function $patch ( stateMutation : ( state : UnwrapRef < S > ) => void ) : void
285
281
function $patch ( partialState : _DeepPartial < UnwrapRef < S > > ) : void
286
282
function $patch (
@@ -289,7 +285,7 @@ function createSetupStore<
289
285
| ( ( state : UnwrapRef < S > ) => void )
290
286
) : void {
291
287
let subscriptionMutation : SubscriptionCallbackMutation < S >
292
- isListening = isSyncListening = false
288
+ isListening = false
293
289
// reset the debugger events since patches are sync
294
290
/* istanbul ignore else */
295
291
if ( __DEV__ ) {
@@ -311,13 +307,7 @@ function createSetupStore<
311
307
events : debuggerEvents as DebuggerEvent [ ] ,
312
308
}
313
309
}
314
- const myListenerId = ( activeListener = Symbol ( ) )
315
- nextTick ( ) . then ( ( ) => {
316
- if ( activeListener === myListenerId ) {
317
- isListening = true
318
- }
319
- } )
320
- isSyncListening = true
310
+ isListening = true
321
311
// because we paused the watcher, we need to manually call the subscriptions
322
312
triggerSubscriptions (
323
313
subscriptions ,
@@ -441,11 +431,19 @@ function createSetupStore<
441
431
options . detached ,
442
432
( ) => stopWatcher ( )
443
433
)
444
- const stopWatcher = scope . run ( ( ) =>
445
- watch (
434
+ const stopWatcher = scope . run ( ( ) => {
435
+ const stop1 = watch (
436
+ ( ) => pinia . state . value [ $id ] ,
437
+ ( ) => {
438
+ shouldTrigger = isListening
439
+ } ,
440
+ { deep : true , flush : 'sync' }
441
+ )
442
+
443
+ const stop2 = watch (
446
444
( ) => pinia . state . value [ $id ] as UnwrapRef < S > ,
447
445
( state ) => {
448
- if ( options . flush === 'sync' ? isSyncListening : isListening ) {
446
+ if ( shouldTrigger ) {
449
447
callback (
450
448
{
451
449
storeId : $id ,
@@ -458,7 +456,14 @@ function createSetupStore<
458
456
} ,
459
457
assign ( { } , $subscribeOptions , options )
460
458
)
461
- ) !
459
+
460
+ const stop = ( ) => {
461
+ stop1 ( )
462
+ stop2 ( )
463
+ }
464
+
465
+ return stop
466
+ } ) !
462
467
463
468
return removeSubscription
464
469
} ,
@@ -614,12 +619,8 @@ function createSetupStore<
614
619
615
620
// avoid devtools logging this as a mutation
616
621
isListening = false
617
- isSyncListening = false
618
622
pinia . state . value [ $id ] = toRef ( newStore . _hmrPayload , 'hotState' )
619
- isSyncListening = true
620
- nextTick ( ) . then ( ( ) => {
621
- isListening = true
622
- } )
623
+ isListening = true
623
624
624
625
for ( const actionName in newStore . _hmrPayload . actions ) {
625
626
const actionFn : _Method = newStore [ actionName ]
@@ -748,7 +749,6 @@ function createSetupStore<
748
749
}
749
750
750
751
isListening = true
751
- isSyncListening = true
752
752
return store
753
753
}
754
754
0 commit comments