Skip to content

Commit 15fc75f

Browse files
authored
fix(runtime-core): use separate emits caches for components and mixins (#11661)
1 parent 4810f14 commit 15fc75f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/runtime-core/__tests__/componentEmits.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import {
55
type ComponentPublicInstance,
6+
createApp,
67
defineComponent,
78
h,
89
nextTick,
@@ -598,4 +599,45 @@ describe('component: emit', () => {
598599
render(h(ComponentC), el)
599600
expect(renderFn).toHaveBeenCalledTimes(1)
600601
})
602+
603+
test('merging emits for a component that is also used as a mixin', () => {
604+
const render = () => h('div')
605+
const CompA = {
606+
render,
607+
}
608+
const validateByMixin = vi.fn(() => true)
609+
const validateByGlobalMixin = vi.fn(() => true)
610+
611+
const mixin = {
612+
emits: {
613+
one: validateByMixin,
614+
},
615+
}
616+
617+
const CompB = defineComponent({
618+
mixins: [mixin, CompA],
619+
created(this) {
620+
this.$emit('one', 1)
621+
},
622+
render,
623+
})
624+
625+
const app = createApp({
626+
render() {
627+
return [h(CompA), h(CompB)]
628+
},
629+
})
630+
631+
app.mixin({
632+
emits: {
633+
one: validateByGlobalMixin,
634+
two: null,
635+
},
636+
})
637+
638+
const root = nodeOps.createElement('div')
639+
app.mount(root)
640+
expect(validateByMixin).toHaveBeenCalledTimes(1)
641+
expect(validateByGlobalMixin).not.toHaveBeenCalled()
642+
})
601643
})

packages/runtime-core/src/componentEmits.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,14 @@ export function emit(
232232
}
233233
}
234234

235+
const mixinEmitsCache = new WeakMap<ConcreteComponent, ObjectEmitsOptions>()
235236
export function normalizeEmitsOptions(
236237
comp: ConcreteComponent,
237238
appContext: AppContext,
238239
asMixin = false,
239240
): ObjectEmitsOptions | null {
240-
const cache = appContext.emitsCache
241+
const cache =
242+
__FEATURE_OPTIONS_API__ && asMixin ? mixinEmitsCache : appContext.emitsCache
241243
const cached = cache.get(comp)
242244
if (cached !== undefined) {
243245
return cached

0 commit comments

Comments
 (0)