11import { error_alert } from "./utils" ;
22import { state } from "./state" ;
33import { t } from "./i18n" ;
4+ import { CloseSession } from "./models/page" ;
45
56export interface Command {
67 command : string
@@ -58,6 +59,7 @@ function safe_poprun_callbacks(callbacks: (() => void)[], name = 'callback') {
5859export class SubPageSession implements Session {
5960 webio_session_id : string = '' ;
6061 debug : boolean ;
62+ private _master_id : string ;
6163 private _closed : boolean = false ;
6264
6365 private _session_create_callbacks : ( ( ) => void ) [ ] = [ ] ;
@@ -73,11 +75,17 @@ export class SubPageSession implements Session {
7375 try {
7476 // @ts -ignore
7577 return window_obj . _pywebio_page !== undefined && window_obj . opener !== null && window_obj . opener . WebIO !== undefined ;
76- } catch ( e ) {
78+ } catch ( e ) {
7779 return false ;
7880 }
7981 }
8082
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+
8189 on_session_create ( callback : ( ) => any ) : void {
8290 this . _session_create_callbacks . push ( callback ) ;
8391 } ;
@@ -93,11 +101,18 @@ export class SubPageSession implements Session {
93101 start_session ( debug : boolean ) : void {
94102 this . debug = debug ;
95103 safe_poprun_callbacks ( this . _session_create_callbacks , 'session_create_callback' ) ;
104+ this . _master_id = window . opener . WebIO . _state . Random ;
96105
97106 // @ts -ignore
98107 window . _pywebio_page . resolve ( this ) ;
108+
109+ setInterval ( ( ) => {
110+ if ( ! this . is_master_active ( ) )
111+ this . close_session ( ) ;
112+ } , 300 ) ;
99113 } ;
100114
115+
101116 // called by opener, transfer command to this session
102117 server_message ( command : Command ) {
103118 if ( this . debug )
@@ -107,11 +122,15 @@ export class SubPageSession implements Session {
107122
108123 // send text message to opener
109124 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" ) ) ;
110127 window . opener . WebIO . _state . CurrentSession . send_message ( msg , onprogress ) ;
111128 }
112129
113130 // send binary message to opener
114131 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" ) ) ;
115134 window . opener . WebIO . _state . CurrentSession . send_buffer ( data , onprogress ) ;
116135 }
117136
@@ -240,6 +259,7 @@ export class WebSocketSession implements Session {
240259 close_session ( ) : void {
241260 this . _closed = true ;
242261 safe_poprun_callbacks ( this . _session_close_callbacks , 'session_close_callback' ) ;
262+ CloseSession ( )
243263 try {
244264 this . ws . close ( ) ;
245265 } catch ( e ) {
@@ -367,6 +387,7 @@ export class HttpSession implements Session {
367387 close_session ( ) : void {
368388 this . _closed = true ;
369389 safe_poprun_callbacks ( this . _session_close_callbacks , 'session_close_callback' ) ;
390+ CloseSession ( )
370391 clearInterval ( this . interval_pull_id ) ;
371392 }
372393
0 commit comments