-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: async subscription do not run when mutate state synchronously after patch #2870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ import { | |
| toRefs, | ||
| Ref, | ||
| ref, | ||
| nextTick, | ||
| } from 'vue' | ||
| import { | ||
| StateTree, | ||
|
|
@@ -250,7 +249,7 @@ function createSetupStore< | |
| if (isListening) { | ||
| debuggerEvents = event | ||
| // avoid triggering this while the store is being built and the state is being set in pinia | ||
| } else if (isListening == false && !store._hotUpdating) { | ||
| } else if (!isListening && !store._hotUpdating) { | ||
| // let patch send all the events together later | ||
| /* istanbul ignore else */ | ||
| if (Array.isArray(debuggerEvents)) { | ||
|
|
@@ -265,8 +264,8 @@ function createSetupStore< | |
| } | ||
|
|
||
| // internal state | ||
| let isListening: boolean // set to true at the end | ||
| let isSyncListening: boolean // set to true at the end | ||
| let isListening: boolean | undefined // set to true at the end | ||
| let shouldTrigger: boolean | undefined // The initial value does not matter, and no need to set to true at the end | ||
| let subscriptions: Set<SubscriptionCallback<S>> = new Set() | ||
| let actionSubscriptions: Set<StoreOnActionListener<Id, S, G, A>> = new Set() | ||
| let debuggerEvents: DebuggerEvent[] | DebuggerEvent | ||
|
|
@@ -281,9 +280,6 @@ 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( | ||
|
|
@@ -292,7 +288,7 @@ function createSetupStore< | |
| | ((state: UnwrapRef<S>) => void) | ||
| ): void { | ||
| let subscriptionMutation: SubscriptionCallbackMutation<S> | ||
| isListening = isSyncListening = false | ||
| isListening = false | ||
| // reset the debugger events since patches are sync | ||
| /* istanbul ignore else */ | ||
| if (__DEV__) { | ||
|
|
@@ -314,13 +310,7 @@ function createSetupStore< | |
| events: debuggerEvents as DebuggerEvent[], | ||
| } | ||
| } | ||
| const myListenerId = (activeListener = Symbol()) | ||
| nextTick().then(() => { | ||
| if (activeListener === myListenerId) { | ||
| isListening = true | ||
| } | ||
| }) | ||
| isSyncListening = true | ||
| isListening = true | ||
| // because we paused the watcher, we need to manually call the subscriptions | ||
| triggerSubscriptions( | ||
| subscriptions, | ||
|
|
@@ -444,11 +434,19 @@ function createSetupStore< | |
| options.detached, | ||
| () => stopWatcher() | ||
| ) | ||
| const stopWatcher = scope.run(() => | ||
| watch( | ||
| const stopWatcher = scope.run(() => { | ||
| const stop1 = watch( | ||
| () => pinia.state.value[$id], | ||
| () => { | ||
| shouldTrigger = isListening | ||
| }, | ||
| { deep: true, flush: 'sync' } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm revisitting this PR and realizing that this might impact perf too much
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to see in the playground the impact
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without checking the playground, I think we can't go this way: #610 |
||
| ) | ||
|
|
||
| const stop2 = watch( | ||
| () => pinia.state.value[$id] as UnwrapRef<S>, | ||
| (state) => { | ||
| if (options.flush === 'sync' ? isSyncListening : isListening) { | ||
| if (shouldTrigger) { | ||
| callback( | ||
| { | ||
| storeId: $id, | ||
|
|
@@ -461,7 +459,12 @@ function createSetupStore< | |
| }, | ||
| assign({}, $subscribeOptions, options) | ||
| ) | ||
| )! | ||
|
|
||
| return () => { | ||
| stop1() | ||
| stop2() | ||
| } | ||
| })! | ||
|
|
||
| return removeSubscription | ||
| }, | ||
|
|
@@ -617,12 +620,8 @@ function createSetupStore< | |
|
|
||
| // avoid devtools logging this as a mutation | ||
| isListening = false | ||
| isSyncListening = false | ||
| pinia.state.value[$id] = toRef(newStore._hmrPayload, 'hotState') | ||
| isSyncListening = true | ||
| nextTick().then(() => { | ||
| isListening = true | ||
| }) | ||
| isListening = true | ||
|
|
||
| for (const actionName in newStore._hmrPayload.actions) { | ||
| const actionFn: _Method = newStore[actionName] | ||
|
|
@@ -751,7 +750,6 @@ function createSetupStore< | |
| } | ||
|
|
||
| isListening = true | ||
| isSyncListening = true | ||
| return store | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.