1- import { ref , computed , watch , Ref } from '@vue/composition-api'
1+ import { ref , computed , watch , Ref , onMounted } from '@vue/composition-api'
22import { ComponentTreeNode , EditStatePayload , InspectedComponentData } from '@vue/devtools-api'
33import Vue from 'vue'
44import groupBy from 'lodash/groupBy'
@@ -26,7 +26,6 @@ export const selectedComponentPendingId = ref<ComponentTreeNode['id']>(null)
2626let lastSelectedApp : AppRecord = null
2727export const lastSelectedComponentId : Record < AppRecord [ 'id' ] , ComponentTreeNode [ 'id' ] > = { }
2828export const expandedMap = ref < Record < ComponentTreeNode [ 'id' ] , boolean > > ( { } )
29- export const resetComponentsQueued = ref ( false )
3029
3130export function useComponentRequests ( ) {
3231 const router = useRouter ( )
@@ -134,17 +133,13 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
134133 const { selectComponent, requestComponentTree } = useComponentRequests ( )
135134 const { subscribe } = useBridge ( )
136135
137- function checkIsExpanded ( id ) {
138- return ! ! expandedMap . value [ id ]
139- }
140-
141- const isExpanded = computed ( ( ) => checkIsExpanded ( instance . value . id ) )
136+ const isExpanded = computed ( ( ) => isComponentOpen ( instance . value . id ) )
142137 const isExpandedUndefined = computed ( ( ) => expandedMap . value [ instance . value . id ] == null )
143138
144- function toggleExpand ( load = true ) {
139+ function toggleExpand ( ) {
145140 if ( ! instance . value . hasChildren ) return
146141 setComponentOpen ( instance . value . id , ! isExpanded . value )
147- if ( load ) {
142+ if ( isComponentOpen ( instance . value . id ) ) {
148143 requestComponentTree ( instance . value . id )
149144 }
150145 }
@@ -173,14 +168,16 @@ export function useComponent (instance: Ref<ComponentTreeNode>) {
173168 } )
174169 }
175170
176- if ( isExpanded . value ) {
177- requestComponentTree ( instance . value . id )
178- }
171+ onMounted ( ( ) => {
172+ if ( isExpanded . value ) {
173+ requestComponentTree ( instance . value . id )
174+ }
175+ } )
179176
180177 return {
181178 isExpanded,
182179 isExpandedUndefined,
183- checkIsExpanded ,
180+ isComponentOpen ,
184181 toggleExpand,
185182 isSelected,
186183 select,
@@ -192,6 +189,10 @@ export function setComponentOpen (id: ComponentTreeNode['id'], isOpen: boolean)
192189 Vue . set ( expandedMap . value , id , isOpen )
193190}
194191
192+ export function isComponentOpen ( id ) {
193+ return ! ! expandedMap . value [ id ]
194+ }
195+
195196export function useSelectedComponent ( ) {
196197 const data = computed ( ( ) => selectedComponentData . value )
197198 const state = computed ( ( ) => selectedComponentData . value
@@ -255,7 +256,6 @@ export function useSelectedComponent () {
255256}
256257
257258export function resetComponents ( ) {
258- resetComponentsQueued . value = false
259259 rootInstances . value = [ ]
260260 componentsMap . value = { }
261261 componentsParent = { }
@@ -273,9 +273,6 @@ export async function requestComponentTree (instanceId: ComponentTreeNode['id']
273273 }
274274 requestedComponentTree . add ( instanceId )
275275
276- if ( instanceId === '_root' ) {
277- resetComponentsQueued . value = true
278- }
279276 await waitForAppSelect ( )
280277
281278 getBridge ( ) . send ( BridgeEvents . TO_BACK_COMPONENT_TREE , {
@@ -284,35 +281,45 @@ export async function requestComponentTree (instanceId: ComponentTreeNode['id']
284281 } )
285282}
286283
287- export function restoreChildrenFromComponentsMap ( data : ComponentTreeNode ) {
288- const instance = componentsMap . value [ data . id ]
289- if ( instance && data . hasChildren ) {
290- if ( ! data . children . length && instance . children . length ) {
291- data . children = instance . children
292- } else {
293- for ( const child of data . children ) {
294- restoreChildrenFromComponentsMap ( child )
295- }
296- }
284+ export function ensureComponentsMapData ( data : ComponentTreeNode ) {
285+ let component = componentsMap . value [ data . id ]
286+ if ( ! component ) {
287+ component = addToComponentsMap ( data )
288+ } else {
289+ component = updateComponentsMapData ( data )
290+ }
291+ return component
292+ }
293+
294+ function ensureComponentsMapChildren ( id : string , children : ComponentTreeNode [ ] ) {
295+ const result = children . map ( child => ensureComponentsMapData ( child ) )
296+ for ( const child of children ) {
297+ componentsParent [ child . id ] = id
297298 }
299+ return result
298300}
299301
300- export function updateComponentsMapData ( data : ComponentTreeNode ) {
302+ function updateComponentsMapData ( data : ComponentTreeNode ) {
301303 const component = componentsMap . value [ data . id ]
302304 for ( const key in data ) {
303- Vue . set ( component , key , data [ key ] )
305+ if ( key === 'children' ) {
306+ if ( ! data . hasChildren || data . children . length ) {
307+ const children = ensureComponentsMapChildren ( component . id , data . children )
308+ Vue . set ( component , key , children )
309+ }
310+ } else {
311+ Vue . set ( component , key , data [ key ] )
312+ }
304313 }
305314 return component
306315}
307316
308- export function addToComponentsMap ( instance : ComponentTreeNode ) {
309- componentsMap . value [ instance . id ] = instance
310- if ( instance . children ) {
311- instance . children . forEach ( c => {
312- componentsParent [ c . id ] = instance . id
313- addToComponentsMap ( c )
314- } )
317+ function addToComponentsMap ( data : ComponentTreeNode ) {
318+ if ( ! data . hasChildren || data . children . length ) {
319+ data . children = ensureComponentsMapChildren ( data . id , data . children )
315320 }
321+ componentsMap . value [ data . id ] = data
322+ return data
316323}
317324
318325export async function loadComponent ( id : ComponentTreeNode [ 'id' ] ) {
0 commit comments