Skip to content

Commit

Permalink
fix(applet): stabilize search results when updating component tree (#539
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alexzhang1030 authored Jul 23, 2024
1 parent cedbccd commit d52a529
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/applet/src/modules/components/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const { expanded: expandedTreeNodes } = createExpandedContext()
const { expanded: expandedStateNodes } = createExpandedContext('component-state')
createSelectedContext()
function getComponentsInspectorTree(filter = '') {
async function getComponentsInspectorTree(filter = '') {
return rpc.value.getInspectorTree({ inspectorId, filter }).then((data) => {
const res = parse(data)
tree.value = res
Expand Down Expand Up @@ -184,14 +184,32 @@ rpc.functions.on(DevToolsMessagingEvents.INSPECTOR_STATE_UPDATED, onInspectorSta
getComponentsInspectorTree()
function searchComponentTree(v: string) {
const value = v.trim().toLowerCase()
toggleFiltered()
getComponentsInspectorTree(value).then(() => {
toggleFiltered()
})
}
watchDebounced(filterComponentName, (v) => {
searchComponentTree(v)
}, { debounce: 300 })
function onInspectorTreeUpdated(_data: string) {
const data = parse(_data) as {
inspectorId: string
rootNodes: CustomInspectorNode[]
}
if (data.inspectorId !== inspectorId)
return
tree.value = data.rootNodes
if (filterComponentName.value) {
searchComponentTree(filterComponentName.value)
}
else {
tree.value = data.rootNodes
}
if (!flattenedTreeNodesIds.value.includes(activeComponentId.value)) {
activeComponentId.value = tree.value?.[0]?.id
Expand All @@ -206,14 +224,6 @@ onUnmounted(() => {
rpc.functions.off(DevToolsMessagingEvents.INSPECTOR_TREE_UPDATED, onInspectorTreeUpdated)
})
watchDebounced(filterComponentName, (v) => {
const value = v.trim().toLowerCase()
toggleFiltered()
getComponentsInspectorTree(value).then(() => {
toggleFiltered()
})
}, { debounce: 300 })
function inspectComponentInspector() {
inspectComponentTipVisible.value = true
emit('onInspectComponentStart')
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/basic/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const routes: RouteRecordRaw[] = [
component: () => import('./pages/CircularState.vue'),
name: 'circular-state',
},
{
path: '/interval-update',
component: () => import('./pages/IntervalUpdate.vue'),
name: 'interval-update',
},
]

const router = createRouter({
Expand Down
16 changes: 16 additions & 0 deletions packages/playground/basic/src/pages/IntervalUpdate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup lang="ts">
import Foo from '../components/Foo.vue'
const str = ref<'hello' | 'world'>('hello')
useIntervalFn(() => {
str.value = str.value === 'hello' ? 'world' : 'hello'
}, 1000)
</script>

<template>
<div>
This is {{ str }} time!
<Foo />
</div>
</template>

0 comments on commit d52a529

Please sign in to comment.