Skip to content
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

fix(runtime-core): use separate emits caches for components and mixins #11661

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import {
type ComponentPublicInstance,
createApp,
defineComponent,
h,
nextTick,
Expand Down Expand Up @@ -598,4 +599,45 @@ describe('component: emit', () => {
render(h(ComponentC), el)
expect(renderFn).toHaveBeenCalledTimes(1)
})

test('merging emits for a component that is also used as a mixin', () => {
const render = () => h('div')
const CompA = {
render,
}
const validateByMixin = vi.fn(() => true)
const validateByGlobalMixin = vi.fn(() => true)

const mixin = {
emits: {
one: validateByMixin,
},
}

const CompB = defineComponent({
mixins: [mixin, CompA],
created(this) {
this.$emit('one', 1)
},
render,
})

const app = createApp({
render() {
return [h(CompA), h(CompB)]
},
})

app.mixin({
emits: {
one: validateByGlobalMixin,
two: null,
},
})

const root = nodeOps.createElement('div')
app.mount(root)
expect(validateByMixin).toHaveBeenCalledTimes(1)
expect(validateByGlobalMixin).not.toHaveBeenCalled()
})
})
4 changes: 3 additions & 1 deletion packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ export function emit(
}
}

const mixinEmitsCache = new WeakMap<ConcreteComponent, ObjectEmitsOptions>()
export function normalizeEmitsOptions(
comp: ConcreteComponent,
appContext: AppContext,
asMixin = false,
): ObjectEmitsOptions | null {
const cache = appContext.emitsCache
const cache =
__FEATURE_OPTIONS_API__ && asMixin ? mixinEmitsCache : appContext.emitsCache
const cached = cache.get(comp)
if (cached !== undefined) {
return cached
Expand Down