<commit_before>// This is the backend that is injected into the page that a Vue app lives in
// when the Vue Devtools panel is activated.

import { highlight, unHighlight, getInstanceRect } from './highlighter'
import { initVuexBackend } from './vuex'
import { initEventsBackend } from './events'
import { findRelatedComponent } from './utils'
import { stringify, classify, camelize, set, parse, getComponentName } from '../util'
import ComponentSelector from './component-selector'
import SharedData, { init as initSharedData } from 'src/shared-data'

// hook should have been injected before this executes.
const hook = window.__VUE_DEVTOOLS_GLOBAL_HOOK__
const rootInstances = []
const propModes = ['default', 'sync', 'once']

export const instanceMap = window.__VUE_DEVTOOLS_INSTANCE_MAP__ = new Map()
const consoleBoundInstances = Array(5)
let currentInspectedId
let bridge
let filter = ''
let captureCount = 0
let isLegacy = false
let rootUID = 0

export function initBackend (_bridge) {
  bridge = _bridge
  if (hook.Vue) {
    isLegacy = hook.Vue.version && hook.Vue.version.split('.')[0] === '1'
    connect()
  } else {
    hook.once('init', connect)
  }

  initRightClick()
}

function connect () {
  initSharedData({
    bridge,
    Vue: hook.Vue
  })

  hook.currentTab = 'components'
  bridge.on('switch-tab', tab => {
    hook.currentTab = tab
    if (tab === 'components') {
      flush()
    }
  })

  // the backend may get injected to the same page multiple times
  // if the user closes and reopens the devtools.
  // make sure there's only one flush listener.
  hook.off('flush')
  hook.on('flush', () => {
    if (hook.currentTab === 'components') {
      flush()
    }
  })

  bridge.on('select-instance', id => {
    currentInspectedId = id
    const instance = instanceMap.get(id)
    bindToConsole(instance)
    flush()
    bridge.send('instance-selected')
  })

  bridge.on('scroll-to-instance', id => {
    const instance = instanceMap.get(id)
    instance && scrollIntoView(instance)
  })

  bridge.on('filter-instances', _filter => {
    filter = _filter.toLowerCase()
    flush()
  })

  bridge.on('refresh', scan)

  bridge.on('enter-instance', id => highlight(instanceMap.get(id)))

  bridge.on('leave-instance', unHighlight)

  // eslint-disable-next-line no-new
  new ComponentSelector(bridge, instanceMap)

  // Get the instance id that is targeted by context menu
  bridge.on('get-context-menu-target', () => {
    const instance = window.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__

    window.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__ = null
    window.__VUE_DEVTOOLS_CONTEXT_MENU_HAS_TARGET__ = false

    if (instance) {
      const id = instance.__VUE_DEVTOOLS_UID__
      if (id) {
        return bridge.send('inspect-instance', id)
      }
    }

    toast('No Vue component was found', 'warn')
  })

  bridge.on('set-instance-data', args => {
    setStateValue(args)
    flush()
  })

  // vuex
  if (hook.store) {
    initVuexBackend(hook, bridge)
  } else {
    hook.once('vuex:init', store => {
      initVuexBackend(hook, bridge)
    })
  }

  // events
  initEventsBackend(hook.Vue, bridge)

  window.__VUE_DEVTOOLS_INSPECT__ = inspectInstance

  // User project devtools config
  if (window.hasOwnProperty('VUE_DEVTOOLS_CONFIG')) {
    const config = window.VUE_DEVTOOLS_CONFIG

    // Open in editor
    if (config.hasOwnProperty('openInEditorHost')) {
      SharedData.openInEditorHost = config.openInEditorHost
    }
  }

  bridge.log('backend ready.')
  bridge.send('ready', hook.Vue.version)
  console.log(
    `%c vue-devtools %c Detected Vue v${hook.Vue.version} %c`,
    'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
    'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
    'background:transparent'
  )
  scan()
}

/**
 * Scan the page for root level Vue instances.
 */

function scan () {
  rootInstances.length = 0
  let inFragment = false
  let currentFragment = null
  walk(document, function (node) {
    if (inFragment) {
      if (node === currentFragment._fragmentEnd) {
        inFragment = false
        currentFragment = null
      }
      return true
    }
    let instance = node.__vue__
    if (instance) {
      instance = instance.$root
      if (instance._isFragment) {<commit_msg>fix: duplicate root instances (#711)<commit_after>// This is the backend that is injected into the page that a Vue app lives in
// when the Vue Devtools panel is activated.

import { highlight, unHighlight, getInstanceRect } from './highlighter'
import { initVuexBackend } from './vuex'
import { initEventsBackend } from './events'
import { findRelatedComponent } from './utils'
import { stringify, classify, camelize, set, parse, getComponentName } from '../util'
import ComponentSelector from './component-selector'
import SharedData, { init as initSharedData } from 'src/shared-data'

// hook should have been injected before this executes.
const hook = window.__VUE_DEVTOOLS_GLOBAL_HOOK__
const rootInstances = []
const propModes = ['default', 'sync', 'once']

export const instanceMap = window.__VUE_DEVTOOLS_INSTANCE_MAP__ = new Map()
const consoleBoundInstances = Array(5)
let currentInspectedId
let bridge
let filter = ''
let captureCount = 0
let isLegacy = false
let rootUID = 0

export function initBackend (_bridge) {
  bridge = _bridge
  if (hook.Vue) {
    isLegacy = hook.Vue.version && hook.Vue.version.split('.')[0] === '1'
    connect()
  } else {
    hook.once('init', connect)
  }

  initRightClick()
}

function connect () {
  initSharedData({
    bridge,
    Vue: hook.Vue
  })

  hook.currentTab = 'components'
  bridge.on('switch-tab', tab => {
    hook.currentTab = tab
    if (tab === 'components') {
      flush()
    }
  })

  // the backend may get injected to the same page multiple times
  // if the user closes and reopens the devtools.
  // make sure there's only one flush listener.
  hook.off('flush')
  hook.on('flush', () => {
    if (hook.currentTab === 'components') {
      flush()
    }
  })

  bridge.on('select-instance', id => {
    currentInspectedId = id
    const instance = instanceMap.get(id)
    bindToConsole(instance)
    flush()
    bridge.send('instance-selected')
  })

  bridge.on('scroll-to-instance', id => {
    const instance = instanceMap.get(id)
    instance && scrollIntoView(instance)
  })

  bridge.on('filter-instances', _filter => {
    filter = _filter.toLowerCase()
    flush()
  })

  bridge.on('refresh', scan)

  bridge.on('enter-instance', id => highlight(instanceMap.get(id)))

  bridge.on('leave-instance', unHighlight)

  // eslint-disable-next-line no-new
  new ComponentSelector(bridge, instanceMap)

  // Get the instance id that is targeted by context menu
  bridge.on('get-context-menu-target', () => {
    const instance = window.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__

    window.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__ = null
    window.__VUE_DEVTOOLS_CONTEXT_MENU_HAS_TARGET__ = false

    if (instance) {
      const id = instance.__VUE_DEVTOOLS_UID__
      if (id) {
        return bridge.send('inspect-instance', id)
      }
    }

    toast('No Vue component was found', 'warn')
  })

  bridge.on('set-instance-data', args => {
    setStateValue(args)
    flush()
  })

  // vuex
  if (hook.store) {
    initVuexBackend(hook, bridge)
  } else {
    hook.once('vuex:init', store => {
      initVuexBackend(hook, bridge)
    })
  }

  // events
  initEventsBackend(hook.Vue, bridge)

  window.__VUE_DEVTOOLS_INSPECT__ = inspectInstance

  // User project devtools config
  if (window.hasOwnProperty('VUE_DEVTOOLS_CONFIG')) {
    const config = window.VUE_DEVTOOLS_CONFIG

    // Open in editor
    if (config.hasOwnProperty('openInEditorHost')) {
      SharedData.openInEditorHost = config.openInEditorHost
    }
  }

  bridge.log('backend ready.')
  bridge.send('ready', hook.Vue.version)
  console.log(
    `%c vue-devtools %c Detected Vue v${hook.Vue.version} %c`,
    'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
    'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff',
    'background:transparent'
  )
  scan()
}

/**
 * Scan the page for root level Vue instances.
 */

function scan () {
  rootInstances.length = 0
  let inFragment = false
  let currentFragment = null
  walk(document, function (node) {
    if (inFragment) {
      if (node === currentFragment._fragmentEnd) {
        inFragment = false
        currentFragment = null
      }
      return true
    }
    let instance = node.__vue__
    if (instance) {
      if (rootInstances.indexOf(instance.$root) === -1) {
        instance = instance.$root
      }
      if (instance._isFragment) {
{'fraction_non_alphanumeric': 0.1255311818077409, 'fraction_numerical': 0.006431606753187091, 'mean_word_length': 3.293885601577909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7109499', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2770, 'n_words': 701}