File size: 1,120 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { BackendContext } from '@vue-devtools/app-backend-api'
import { HookEvents } from '@vue-devtools/shared-utils'

const internalRE = /^(?:pre-)?hook:/

function wrap(app, Vue, method, ctx: BackendContext) {
  const original = Vue.prototype[method]
  if (original) {
    Vue.prototype[method] = function (...args) {
      const res = original.apply(this, args)
      logEvent(this, method, args[0], args.slice(1))
      return res
    }
  }

  function logEvent(vm, type, eventName, payload) {
    // The string check is important for compat with 1.x where the first
    // argument may be an object instead of a string.
    // this also ensures the event is only logged for direct $emit (source)
    // instead of by $dispatch/$broadcast
    if (typeof eventName === 'string' && !internalRE.test(eventName)) {
      const instance = vm._self || vm
      ctx.hook.emit(HookEvents.COMPONENT_EMIT, app, instance, eventName, payload)
    }
  }
}

export function wrapVueForEvents(app, Vue, ctx: BackendContext) {
  ['$emit', '$broadcast', '$dispatch'].forEach((method) => {
    wrap(app, Vue, method, ctx)
  })
}