11import * as React from 'react'
22import * as T from 'typings'
3- import { State } from 'xstate'
43import { createMachine } from './machine'
54import { useMachine } from '../xstate-react'
65import logger from '../logger'
@@ -13,9 +12,27 @@ interface Output {
1312
1413declare let acquireVsCodeApi : any
1514
16- const createRouteString = ( routeObject : object ) => {
17- let paths = [ ]
18- const key = Object . keys ( routeObject ) [ 0 ]
15+ export const createRouteString = ( route : object | string ) : string => {
16+ if ( typeof route === 'string' ) {
17+ return route
18+ }
19+ const paths : string [ ] = [ ]
20+ let current : object | string | undefined = route
21+ while ( current ) {
22+ // current is final string value
23+ if ( typeof current === 'string' ) {
24+ paths . push ( current )
25+ break
26+ }
27+
28+ // current is object
29+ const next : string = Object . keys ( current ) [ 0 ]
30+ paths . push ( next )
31+ // @ts -ignore
32+ current = current [ next ]
33+ }
34+
35+ return paths . join ( '.' )
1936}
2037
2138const editor = acquireVsCodeApi ( )
@@ -33,8 +50,6 @@ const useStateMachine = (): Output => {
3350 send ( action )
3451 }
3552
36- console . log ( `STATE: ${ JSON . stringify ( state . value ) } ` )
37-
3853 // event bus listener
3954 React . useEffect ( ( ) => {
4055 const listener = 'message'
@@ -54,9 +69,13 @@ const useStateMachine = (): Output => {
5469 }
5570 } , [ ] )
5671
72+ // convert route to a string to avoid unnecessary React re-renders on deeply nested objects
73+ const route = createRouteString ( state . value )
74+ console . log ( `STATE: ${ route } ` )
75+
5776 return {
5877 context : state . context ,
59- route : '' ,
78+ route,
6079 send : sendWithLog ,
6180 }
6281}
0 commit comments