Skip to content

Fix/hints #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
router progress
Signed-off-by: shmck <[email protected]>
  • Loading branch information
ShMcK committed Jul 1, 2020
commit 4de3c42588e6000ce82c39ce1a45784da0c5331f
16 changes: 16 additions & 0 deletions web-app/src/services/state/routeString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createRouteString } from './useStateMachine'

describe('route string', () => {
it('should take a single key route', () => {
const result = createRouteString('a')
expect(result).toBe('a')
})
it('should take a 1 level nested key route', () => {
const result = createRouteString({ a: 'b' })
expect(result).toBe('a.b')
})
it('should take a 3 level nested key route', () => {
const result = createRouteString({ a: { b: { c: 'd' } } })
expect(result).toBe('a.b.c.d')
})
})
33 changes: 26 additions & 7 deletions web-app/src/services/state/useStateMachine.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react'
import * as T from 'typings'
import { State } from 'xstate'
import { createMachine } from './machine'
import { useMachine } from '../xstate-react'
import logger from '../logger'
Expand All @@ -13,9 +12,27 @@ interface Output {

declare let acquireVsCodeApi: any

const createRouteString = (routeObject: object) => {
let paths = []
const key = Object.keys(routeObject)[0]
export const createRouteString = (route: object | string): string => {
if (typeof route === 'string') {
return route
}
const paths: string[] = []
let current: object | string | undefined = route
while (current) {
// current is final string value
if (typeof current === 'string') {
paths.push(current)
break
}

// current is object
const next: string = Object.keys(current)[0]
paths.push(next)
// @ts-ignore
current = current[next]
}

return paths.join('.')
}

const editor = acquireVsCodeApi()
Expand All @@ -33,8 +50,6 @@ const useStateMachine = (): Output => {
send(action)
}

console.log(`STATE: ${JSON.stringify(state.value)}`)

// event bus listener
React.useEffect(() => {
const listener = 'message'
Expand All @@ -54,9 +69,13 @@ const useStateMachine = (): Output => {
}
}, [])

// convert route to a string to avoid unnecessary React re-renders on deeply nested objects
const route = createRouteString(state.value)
console.log(`STATE: ${route}`)

return {
context: state.context,
route: '',
route,
send: sendWithLog,
}
}
Expand Down