componentEmits.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { type ObjectEmitsOptions, baseEmit } from '@vue/runtime-dom'
  2. import type { VaporComponent, VaporComponentInstance } from './component'
  3. import { EMPTY_OBJ, hasOwn, isArray } from '@vue/shared'
  4. import { resolveSource } from './componentProps'
  5. /**
  6. * The logic from core isn't too reusable so it's better to duplicate here
  7. */
  8. export function normalizeEmitsOptions(
  9. comp: VaporComponent,
  10. ): ObjectEmitsOptions | null {
  11. const cached = comp.__emitsOptions
  12. if (cached) return cached
  13. const raw = comp.emits
  14. if (!raw) return null
  15. let normalized: ObjectEmitsOptions
  16. if (isArray(raw)) {
  17. normalized = {}
  18. for (const key in raw) normalized[key] = null
  19. } else {
  20. normalized = raw
  21. }
  22. return (comp.__emitsOptions = normalized)
  23. }
  24. export function emit(
  25. instance: VaporComponentInstance,
  26. event: string,
  27. ...rawArgs: any[]
  28. ): void {
  29. baseEmit(
  30. instance,
  31. instance.rawProps || EMPTY_OBJ,
  32. propGetter,
  33. event,
  34. ...rawArgs,
  35. )
  36. }
  37. function propGetter(rawProps: Record<string, any>, key: string) {
  38. const dynamicSources = rawProps.$
  39. if (dynamicSources) {
  40. let i = dynamicSources.length
  41. while (i--) {
  42. const source = resolveSource(dynamicSources[i])
  43. if (hasOwn(source, key)) return source[key]
  44. }
  45. }
  46. return rawProps[key] && resolveSource(rawProps[key])
  47. }