1
1
import { error_alert } from "./utils" ;
2
2
import { state } from "./state" ;
3
3
import { t } from "./i18n" ;
4
+ import { CloseSession } from "./models/page" ;
4
5
5
6
export interface Command {
6
7
command : string
@@ -58,6 +59,7 @@ function safe_poprun_callbacks(callbacks: (() => void)[], name = 'callback') {
58
59
export class SubPageSession implements Session {
59
60
webio_session_id : string = '' ;
60
61
debug : boolean ;
62
+ private _master_id : string ;
61
63
private _closed : boolean = false ;
62
64
63
65
private _session_create_callbacks : ( ( ) => void ) [ ] = [ ] ;
@@ -73,11 +75,17 @@ export class SubPageSession implements Session {
73
75
try {
74
76
// @ts -ignore
75
77
return window_obj . _pywebio_page !== undefined && window_obj . opener !== null && window_obj . opener . WebIO !== undefined ;
76
- } catch ( e ) {
78
+ } catch ( e ) {
77
79
return false ;
78
80
}
79
81
}
80
82
83
+ // check if the master page is active
84
+ is_master_active ( ) : boolean {
85
+ return window . opener && window . opener . WebIO && ! window . opener . WebIO . _state . CurrentSession . closed ( ) &&
86
+ this . _master_id == window . opener . WebIO . _state . Random
87
+ }
88
+
81
89
on_session_create ( callback : ( ) => any ) : void {
82
90
this . _session_create_callbacks . push ( callback ) ;
83
91
} ;
@@ -93,11 +101,18 @@ export class SubPageSession implements Session {
93
101
start_session ( debug : boolean ) : void {
94
102
this . debug = debug ;
95
103
safe_poprun_callbacks ( this . _session_create_callbacks , 'session_create_callback' ) ;
104
+ this . _master_id = window . opener . WebIO . _state . Random ;
96
105
97
106
// @ts -ignore
98
107
window . _pywebio_page . resolve ( this ) ;
108
+
109
+ setInterval ( ( ) => {
110
+ if ( ! this . is_master_active ( ) )
111
+ this . close_session ( ) ;
112
+ } , 300 ) ;
99
113
} ;
100
114
115
+
101
116
// called by opener, transfer command to this session
102
117
server_message ( command : Command ) {
103
118
if ( this . debug )
@@ -107,11 +122,15 @@ export class SubPageSession implements Session {
107
122
108
123
// send text message to opener
109
124
send_message ( msg : ClientEvent , onprogress ?: ( loaded : number , total : number ) => void ) : void {
125
+ if ( this . closed ( ) || ! this . is_master_active ( ) )
126
+ return error_alert ( t ( "disconnected_with_server" ) ) ;
110
127
window . opener . WebIO . _state . CurrentSession . send_message ( msg , onprogress ) ;
111
128
}
112
129
113
130
// send binary message to opener
114
131
send_buffer ( data : Blob , onprogress ?: ( loaded : number , total : number ) => void ) : void {
132
+ if ( this . closed ( ) || ! this . is_master_active ( ) )
133
+ return error_alert ( t ( "disconnected_with_server" ) ) ;
115
134
window . opener . WebIO . _state . CurrentSession . send_buffer ( data , onprogress ) ;
116
135
}
117
136
@@ -240,6 +259,7 @@ export class WebSocketSession implements Session {
240
259
close_session ( ) : void {
241
260
this . _closed = true ;
242
261
safe_poprun_callbacks ( this . _session_close_callbacks , 'session_close_callback' ) ;
262
+ CloseSession ( )
243
263
try {
244
264
this . ws . close ( ) ;
245
265
} catch ( e ) {
@@ -367,6 +387,7 @@ export class HttpSession implements Session {
367
387
close_session ( ) : void {
368
388
this . _closed = true ;
369
389
safe_poprun_callbacks ( this . _session_close_callbacks , 'session_close_callback' ) ;
390
+ CloseSession ( )
370
391
clearInterval ( this . interval_pull_id ) ;
371
392
}
372
393
0 commit comments