Ver código fonte

refactor(devtools): extract same logic into `createDevtoolsHook` (#1608)

underfin 5 anos atrás
pai
commit
a8966457d3
1 arquivos alterados com 18 adições e 26 exclusões
  1. 18 26
      packages/runtime-core/src/devtools.ts

+ 18 - 26
packages/runtime-core/src/devtools.ts

@@ -47,32 +47,24 @@ export function appUnmounted(app: App) {
   devtools.emit(DevtoolsHooks.APP_UNMOUNT, app)
 }
 
-export function componentAdded(component: ComponentInternalInstance) {
-  if (!devtools || !component.appContext.__app) return
-  devtools.emit(
-    DevtoolsHooks.COMPONENT_ADDED,
-    component.appContext.__app,
-    component.uid,
-    component.parent ? component.parent.uid : undefined
-  )
-}
+export const componentAdded = createDevtoolsHook(DevtoolsHooks.COMPONENT_ADDED)
 
-export function componentUpdated(component: ComponentInternalInstance) {
-  if (!devtools || !component.appContext.__app) return
-  devtools.emit(
-    DevtoolsHooks.COMPONENT_UPDATED,
-    component.appContext.__app,
-    component.uid,
-    component.parent ? component.parent.uid : undefined
-  )
-}
+export const componentUpdated = createDevtoolsHook(
+  DevtoolsHooks.COMPONENT_UPDATED
+)
+
+export const componentRemoved = createDevtoolsHook(
+  DevtoolsHooks.COMPONENT_REMOVED
+)
 
-export function componentRemoved(component: ComponentInternalInstance) {
-  if (!devtools || !component.appContext.__app) return
-  devtools.emit(
-    DevtoolsHooks.COMPONENT_REMOVED,
-    component.appContext.__app,
-    component.uid,
-    component.parent ? component.parent.uid : undefined
-  )
+function createDevtoolsHook(hook: DevtoolsHooks) {
+  return (component: ComponentInternalInstance) => {
+    if (!devtools || !component.appContext.__app) return
+    devtools.emit(
+      hook,
+      component.appContext.__app,
+      component.uid,
+      component.parent ? component.parent.uid : undefined
+    )
+  }
 }