-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
fix(runtime-core): unwatch should be callable during SSR #11925
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -373,6 +373,56 @@ describe('api: watch', () => { | |
expect(dummy).toBe(0) | ||
}) | ||
|
||
it('stopping the watcher (SSR)', async () => { | ||
type SetBoolean = (v: boolean) => void | ||
const setSSR = (ssr: boolean) => { | ||
__SSR__ = ssr | ||
__VUE_SSR_SETTERS__.forEach((setInSSRSetupState: SetBoolean) => { | ||
setInSSRSetupState(ssr) | ||
}) | ||
} | ||
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. This exists to hit the SSR code path that has the regression. |
||
setSSR(true) | ||
|
||
let dummy = 0 | ||
const count = ref<number>(1) | ||
const captureValue = (value: number) => { | ||
dummy = value | ||
} | ||
const watchCallback = vi.fn(newValue => { | ||
captureValue(newValue) | ||
}) | ||
const scenario = () => { | ||
const Comp = defineComponent({ | ||
created() { | ||
const getter = () => this.count | ||
captureValue(getter()) // sets dummy to 1 | ||
const stop = this.$watch(getter, watchCallback) | ||
stop() | ||
this.count = 2 // shouldn't trigger side effect | ||
}, | ||
render() { | ||
return h('div', this.count) | ||
}, | ||
setup() { | ||
return { count } | ||
}, | ||
}) | ||
const root = nodeOps.createElement('div') | ||
render(h(Comp), root) | ||
} | ||
|
||
expect(scenario).not.toThrowError(/stop is not a function/) | ||
expect(watchCallback).not.toHaveBeenCalled() | ||
expect(dummy).toBe(1) | ||
await nextTick() | ||
count.value = 3 // shouldn't trigger side effect | ||
await nextTick() | ||
expect(watchCallback).not.toHaveBeenCalled() | ||
expect(dummy).toBe(1) | ||
|
||
setSSR(false) | ||
}) | ||
|
||
it('stopping the watcher (with source)', async () => { | ||
const state = reactive({ count: 0 }) | ||
let dummy | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,11 +179,11 @@ function doWatch( | |
// immediately watch or watchEffect | ||
baseWatchOptions.once = true | ||
} else { | ||
return { | ||
stop: NOOP, | ||
resume: NOOP, | ||
pause: NOOP, | ||
} as WatchHandle | ||
const watchStopHandle = () => {} | ||
watchStopHandle.stop = NOOP | ||
watchStopHandle.resume = NOOP | ||
watchStopHandle.pause = NOOP | ||
return watchStopHandle | ||
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 considered doing this: return Object.assign(() => {}, {
stop: NOOP,
resume: NOOP,
pause: NOOP,
}) But decided against it to avoid the temporary allocation of the object literal. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added to address TypeScript type check errors in the test file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if it's undesirable to declare
__VUE_SSR_SETTERS__
. Maybe that exposes this type and it's not intended to be public. If so, is there a better way to type that interface in the unit test?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can export
setInSSRSetupState
and import it intoapiWatch.spec.ts
using a relative path.core/packages/runtime-core/src/component.ts
Line 715 in 4019369