Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4de3c42

Browse files
committed
router progress
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent1307578 commit4de3c42

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import{createRouteString}from'./useStateMachine'
2+
3+
describe('route string',()=>{
4+
it('should take a single key route',()=>{
5+
constresult=createRouteString('a')
6+
expect(result).toBe('a')
7+
})
8+
it('should take a 1 level nested key route',()=>{
9+
constresult=createRouteString({a:'b'})
10+
expect(result).toBe('a.b')
11+
})
12+
it('should take a 3 level nested key route',()=>{
13+
constresult=createRouteString({a:{b:{c:'d'}}})
14+
expect(result).toBe('a.b.c.d')
15+
})
16+
})

‎web-app/src/services/state/useStateMachine.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import*asReactfrom'react'
22
import*asTfrom'typings'
3-
import{State}from'xstate'
43
import{createMachine}from'./machine'
54
import{useMachine}from'../xstate-react'
65
importloggerfrom'../logger'
@@ -13,9 +12,27 @@ interface Output {
1312

1413
declareletacquireVsCodeApi:any
1514

16-
constcreateRouteString=(routeObject:object)=>{
17-
letpaths=[]
18-
constkey=Object.keys(routeObject)[0]
15+
exportconstcreateRouteString=(route:object|string):string=>{
16+
if(typeofroute==='string'){
17+
returnroute
18+
}
19+
constpaths:string[]=[]
20+
letcurrent:object|string|undefined=route
21+
while(current){
22+
// current is final string value
23+
if(typeofcurrent==='string'){
24+
paths.push(current)
25+
break
26+
}
27+
28+
// current is object
29+
constnext:string=Object.keys(current)[0]
30+
paths.push(next)
31+
//@ts-ignore
32+
current=current[next]
33+
}
34+
35+
returnpaths.join('.')
1936
}
2037

2138
consteditor=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
constlistener='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+
constroute=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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp