@@ -17,6 +17,10 @@ const elms = {
17
17
joystickZone : document . querySelector ( "#joystick-zone" ) ,
18
18
buttonsZone : document . querySelector ( "#buttons-zone" ) ,
19
19
} ,
20
+ info : {
21
+ statuses : [ ...document . querySelectorAll ( ".status" ) ] ,
22
+ pings : [ ...document . querySelectorAll ( ".ping" ) ] ,
23
+ } ,
20
24
} ;
21
25
22
26
/// hamburgers
@@ -28,12 +32,33 @@ for (const burger of elms.hamburgers) {
28
32
} ;
29
33
}
30
34
35
+ /// info
36
+
37
+ function updateInfo ( status , ping = Infinity ) {
38
+ for ( const statusElm of elms . info . statuses ) statusElm . innerText = status ;
39
+ for ( const pingElm of elms . info . pings ) pingElm . innerText = `ping: ${ ping } ms` ;
40
+ }
41
+
42
+ updateInfo ( "200 OK" , Date . now ( ) - window . performance . timing . navigationStart ) ;
43
+
31
44
/// poster
32
45
33
46
let host = params . get ( "host" ) ?? window . location . origin ;
34
47
let interval = Number ( params . get ( "interval" ) ) || 100 ;
35
48
let showButtons = true ;
36
49
50
+ async function callApi ( method = "GET" , path = "/" , options = { } ) {
51
+ const prevTime = window . performance . now ( ) ;
52
+ try {
53
+ const res = await fetch ( host + path , { method, ...options } ) ;
54
+ updateInfo ( `${ res . status } ${ res . statusText } ` , window . performance . now ( ) - prevTime ) ;
55
+ return res ;
56
+ } catch ( error ) {
57
+ updateInfo ( error . message . split ( / \s + / ) [ 0 ] , window . performance . now ( ) - prevTime ) ;
58
+ throw error ;
59
+ }
60
+ }
61
+
37
62
const joystickPoster = {
38
63
_angle : Math . PI / 2 ,
39
64
_force : 1 ,
@@ -54,14 +79,9 @@ const joystickPoster = {
54
79
55
80
async post ( ) {
56
81
console . log ( "POST joystick" , { angle : this . angle , force : this . force , query : this . query } ) ;
57
- try {
58
- const res = await fetch ( `${ host } /joystick?q=${ this . query } ` , { method : "POST" } ) ;
59
- console . log ( "Responded joystick" , await res . arrayBuffer ( ) , await res . text ( ) ) ;
60
- return res ;
61
- } catch ( error ) {
62
- console . error ( error ) ;
63
- return null ;
64
- }
82
+ const res = await callApi ( "POST" , "/joystick?q=" + this . query ) ;
83
+ console . log ( "Responded joystick" , await res . text ( ) ) ;
84
+ return res ;
65
85
} ,
66
86
67
87
intervalId : null ,
@@ -131,8 +151,8 @@ for (const button of elms.main.buttonsZone.childNodes) {
131
151
button . onclick = async ( { target : button } ) => {
132
152
const id = button . innerText . toLowerCase ( ) ;
133
153
console . log ( "POST button" , id ) ;
134
- const res = await fetch ( ` ${ host } / ${ id } ` , { method : "POST" } ) ;
135
- console . log ( "Responded button" , await res . arrayBuffer ( ) , await res . text ( ) ) ;
154
+ const res = await callApi ( "POST" , "/" + id ) ;
155
+ console . log ( "Responded button" , await res . text ( ) ) ;
136
156
} ;
137
157
}
138
158
0 commit comments