Skip to content

Commit

Permalink
feat: introducing onDevToolsClientConnected hook
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Dec 26, 2023
1 parent 392de9f commit d2d6f7a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/devtools-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
onDevToolsConnected,
onDevToolsClientConnected,
setupDevToolsPlugin,
addCustomTab,
addCustomCommand,
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools-kit/src/core/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function onDevToolsClientConnected(fn: () => void) {
}

apiHooks.hook(DevToolsEvents.DEVTOOLS_STATE_UPDATED, (state) => {
if (state.connected && devtoolsState.clientConnected) {
if (state.connected && state.clientConnected) {
fn()
resolve()
}
Expand Down
9 changes: 7 additions & 2 deletions packages/devtools-kit/src/core/general/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AppRecord } from '@vue-devtools-next/schema'
import type { AppRecord, DevToolsState } from '@vue-devtools-next/schema'
import { deepClone, target as global } from '@vue-devtools-next/shared'
import { debounce } from 'perfect-debounce'
import type { DevToolsPluginApi } from '../../api'
import { DevToolsEvents, apiHooks } from '../../api'
import type { Router, RouterInfo } from '../router'
Expand Down Expand Up @@ -33,6 +34,10 @@ global[StateKey] ??= {

global[ContextKey] ??= deepClone(DefaultContext)

const callStateUpdatedHook = debounce((state: DevToolsState, oldState: DevToolsState) => {
apiHooks.callHook(DevToolsEvents.DEVTOOLS_STATE_UPDATED, state, oldState)
}, 80)

export const devtoolsState = new Proxy(global[StateKey], {
get(target, property) {
return global[StateKey][property]
Expand All @@ -51,7 +56,7 @@ export const devtoolsState = new Proxy(global[StateKey], {
global[ContextKey].routerInfo = devtoolsRouterInfo
}

apiHooks.callHook(DevToolsEvents.DEVTOOLS_STATE_UPDATED, global[StateKey], oldState)
callStateUpdatedHook(global[StateKey], oldState)
return true
},
deleteProperty(target, property) {
Expand Down
3 changes: 2 additions & 1 deletion packages/devtools-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { devtoolsContext, devtoolsState, hook, initDevTools, onDevToolsConnected } from './core/general'
import { devtoolsContext, devtoolsState, hook, initDevTools, onDevToolsClientConnected, onDevToolsConnected } from './core/general'
import { addCustomTab } from './core/custom-tab'
import { addCustomCommand, removeCustomCommand } from './core/custom-command'
import { setupDevToolsPlugin } from './api/plugin'
Expand All @@ -25,6 +25,7 @@ export const devtools = {
}

export {
onDevToolsClientConnected,
onDevToolsConnected,
addCustomTab,
addCustomCommand,
Expand Down
5 changes: 4 additions & 1 deletion packages/devtools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { devtools as _devtools, addCustomTab, onDevToolsConnected } from '@vue-devtools-next/kit'
import { devtools as _devtools, addCustomCommand, addCustomTab, onDevToolsClientConnected, onDevToolsConnected, removeCustomCommand } from '@vue-devtools-next/kit'
import { connect } from '@vue-devtools-next/electron'

export type * from '@vue-devtools-next/kit'
Expand All @@ -11,5 +11,8 @@ const devtools = {
export {
devtools,
onDevToolsConnected,
onDevToolsClientConnected,
addCustomTab,
addCustomCommand,
removeCustomCommand,
}
4 changes: 4 additions & 0 deletions packages/playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ app.use(pinia)

app.mount('#app')

// onDevToolsClientConnected(() => {
// console.log('devtools client connected')
// })

// app2.use(router2)
// app2.use(pinia2)
// app2.mount('#app2')

0 comments on commit d2d6f7a

Please sign in to comment.