@@ -9,13 +9,18 @@ import {
99} from "./messaging/incoming" ;
1010import { info , warn } from "./utils/logging" ;
1111import { autoLogin } from "./utils/login" ;
12+ import { sendMessageToTestBox } from "./messaging" ;
13+ import { INITIALIZE_ACK , LOGIN_ACK , NAVIGATE_ACK } from "./messaging/outgoing" ;
1214
1315export type TestBoxEventRouter = {
1416 [ K in keyof IncomingEventMap ] ?: ( ( data : IncomingEventMap [ K ] ) => void ) [ ] ;
1517} ;
1618
1719// Note to future selves: you cannot destructure testbox here, the
1820// type narrowing does not work correctly if you do so here.
21+ let loggingIn = false ;
22+ let navigateUrl = "" ;
23+
1924export function routeMessage (
2025 { testbox } : UnionedIncomingMessages ,
2126 router : TestBoxEventRouter
@@ -24,15 +29,27 @@ export function routeMessage(
2429 if ( VALID_INCOMING_EVENTS . includes ( event ) ) {
2530 switch ( event ) {
2631 case INITIALIZE :
32+ sendMessageToTestBox ( INITIALIZE_ACK ) ;
2733 initializeTestBox ( data ) ;
2834 break ;
2935 case NAVIGATE :
30- window . location . href = data . url ;
36+ sendMessageToTestBox ( NAVIGATE_ACK ) ;
37+ if ( loggingIn ) {
38+ navigateUrl = data . url ;
39+ } else {
40+ const navigateFunc = router [ "navigate" ] [ 0 ] ;
41+ navigateFunc ( data ) ;
42+ }
3143 break ;
3244 case LOGIN :
45+ loggingIn = true ;
46+ sendMessageToTestBox ( LOGIN_ACK ) ;
3347 autoLogin ( data , router ) . then ( ( nextUrl ) => {
34- if ( nextUrl && nextUrl !== window . location . href ) {
35- window . location . href = nextUrl ;
48+ loggingIn = false ;
49+ const goTo = navigateUrl || nextUrl ;
50+ if ( goTo && goTo !== window . location . href ) {
51+ const navigateFunc = router [ "navigate" ] [ 0 ] ;
52+ navigateFunc ( { url : goTo } ) ;
3653 }
3754 } ) ;
3855 break ;
0 commit comments