@@ -21,7 +21,6 @@ import {
21
21
ref ,
22
22
set ,
23
23
del ,
24
- nextTick ,
25
24
isVue2 ,
26
25
} from 'vue-demi'
27
26
import {
@@ -256,7 +255,7 @@ function createSetupStore<
256
255
if ( isListening ) {
257
256
debuggerEvents = event
258
257
// avoid triggering this while the store is being built and the state is being set in pinia
259
- } else if ( isListening == false && ! store . _hotUpdating ) {
258
+ } else if ( isListening === false && ! store . _hotUpdating ) {
260
259
// let patch send all the events together later
261
260
/* istanbul ignore else */
262
261
if ( Array . isArray ( debuggerEvents ) ) {
@@ -271,8 +270,8 @@ function createSetupStore<
271
270
}
272
271
273
272
// internal state
274
- let isListening : boolean // set to true at the end
275
- let isSyncListening : boolean // set to true at the end
273
+ let isListening = false // set to true at the end
274
+ let shouldTrigger = false // The initial value does not matter, and no need to set to true at the end
276
275
let subscriptions : SubscriptionCallback < S > [ ] = [ ]
277
276
let actionSubscriptions : StoreOnActionListener < Id , S , G , A > [ ] = [ ]
278
277
let debuggerEvents : DebuggerEvent [ ] | DebuggerEvent
@@ -291,9 +290,6 @@ function createSetupStore<
291
290
292
291
const hotState = ref ( { } as S )
293
292
294
- // avoid triggering too many listeners
295
- // https://github.com/vuejs/pinia/issues/1129
296
- let activeListener : Symbol | undefined
297
293
function $patch ( stateMutation : ( state : UnwrapRef < S > ) => void ) : void
298
294
function $patch ( partialState : _DeepPartial < UnwrapRef < S > > ) : void
299
295
function $patch (
@@ -302,7 +298,7 @@ function createSetupStore<
302
298
| ( ( state : UnwrapRef < S > ) => void )
303
299
) : void {
304
300
let subscriptionMutation : SubscriptionCallbackMutation < S >
305
- isListening = isSyncListening = false
301
+ isListening = false
306
302
// reset the debugger events since patches are sync
307
303
/* istanbul ignore else */
308
304
if ( __DEV__ ) {
@@ -324,13 +320,7 @@ function createSetupStore<
324
320
events : debuggerEvents as DebuggerEvent [ ] ,
325
321
}
326
322
}
327
- const myListenerId = ( activeListener = Symbol ( ) )
328
- nextTick ( ) . then ( ( ) => {
329
- if ( activeListener === myListenerId ) {
330
- isListening = true
331
- }
332
- } )
333
- isSyncListening = true
323
+ isListening = true
334
324
// because we paused the watcher, we need to manually call the subscriptions
335
325
triggerSubscriptions (
336
326
subscriptions ,
@@ -454,11 +444,19 @@ function createSetupStore<
454
444
options . detached ,
455
445
( ) => stopWatcher ( )
456
446
)
457
- const stopWatcher = scope . run ( ( ) =>
458
- watch (
447
+ const stopWatcher = scope . run ( ( ) => {
448
+ const stop1 = watch (
449
+ ( ) => pinia . state . value [ $id ] ,
450
+ ( ) => {
451
+ shouldTrigger = isListening
452
+ } ,
453
+ { deep : true , flush : 'sync' }
454
+ )
455
+
456
+ const stop2 = watch (
459
457
( ) => pinia . state . value [ $id ] as UnwrapRef < S > ,
460
458
( state ) => {
461
- if ( options . flush === 'sync' ? isSyncListening : isListening ) {
459
+ if ( shouldTrigger ) {
462
460
callback (
463
461
{
464
462
storeId : $id ,
@@ -471,7 +469,14 @@ function createSetupStore<
471
469
} ,
472
470
assign ( { } , $subscribeOptions , options )
473
471
)
474
- ) !
472
+
473
+ const stop = ( ) => {
474
+ stop1 ( )
475
+ stop2 ( )
476
+ }
477
+
478
+ return stop
479
+ } ) !
475
480
476
481
return removeSubscription
477
482
} ,
@@ -647,12 +652,8 @@ function createSetupStore<
647
652
648
653
// avoid devtools logging this as a mutation
649
654
isListening = false
650
- isSyncListening = false
651
655
pinia . state . value [ $id ] = toRef ( newStore . _hmrPayload , 'hotState' )
652
- isSyncListening = true
653
- nextTick ( ) . then ( ( ) => {
654
- isListening = true
655
- } )
656
+ isListening = true
656
657
657
658
for ( const actionName in newStore . _hmrPayload . actions ) {
658
659
const actionFn : _Method = newStore [ actionName ]
@@ -779,7 +780,6 @@ function createSetupStore<
779
780
}
780
781
781
782
isListening = true
782
- isSyncListening = true
783
783
return store
784
784
}
785
785
0 commit comments