Skip to content

Commit 0834386

Browse files
committed
fix sub page session don't close when master reload or close
1 parent 1a80a70 commit 0834386

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

webiojs/src/models/page.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ export function DeliverMessage(msg: Command) {
7979
msg.page = undefined;
8080
page.server_message(msg);
8181
});
82+
}
83+
84+
export function CloseSession() {
85+
for (let page_id in subpages) {
86+
// @ts-ignore
87+
subpages[page_id].page._pywebio_page.promise.then((page: SubPageSession) => {
88+
page.close_session()
89+
});
90+
}
8291
}

webiojs/src/session.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {error_alert} from "./utils";
22
import {state} from "./state";
33
import {t} from "./i18n";
4+
import {CloseSession} from "./models/page";
45

56
export interface Command {
67
command: string
@@ -58,6 +59,7 @@ function safe_poprun_callbacks(callbacks: (() => void)[], name = 'callback') {
5859
export 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

webiojs/src/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Session} from "./session";
2+
import {randomid} from "./utils";
23

34
// Runtime state
45
export let state = {
@@ -9,6 +10,7 @@ export let state = {
910
InputPanelInitHeight: 300, // 输入panel的初始高度
1011
FixedInputPanel:true,
1112
AutoFocusOnInput:true,
13+
Random: randomid(10),
1214
};
1315

1416
// App config

0 commit comments

Comments
 (0)