Skip to content

Commit

Permalink
fix sub page session don't close when master reload or close
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Jul 6, 2022
1 parent 1a80a70 commit 0834386
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
9 changes: 9 additions & 0 deletions webiojs/src/models/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ export function DeliverMessage(msg: Command) {
msg.page = undefined;
page.server_message(msg);
});
}

export function CloseSession() {
for (let page_id in subpages) {
// @ts-ignore
subpages[page_id].page._pywebio_page.promise.then((page: SubPageSession) => {
page.close_session()
});
}
}
23 changes: 22 additions & 1 deletion webiojs/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {error_alert} from "./utils";
import {state} from "./state";
import {t} from "./i18n";
import {CloseSession} from "./models/page";

export interface Command {
command: string
Expand Down Expand Up @@ -58,6 +59,7 @@ function safe_poprun_callbacks(callbacks: (() => void)[], name = 'callback') {
export class SubPageSession implements Session {
webio_session_id: string = '';
debug: boolean;
private _master_id: string;
private _closed: boolean = false;

private _session_create_callbacks: (() => void)[] = [];
Expand All @@ -73,11 +75,17 @@ export class SubPageSession implements Session {
try {
// @ts-ignore
return window_obj._pywebio_page !== undefined && window_obj.opener !== null && window_obj.opener.WebIO !== undefined;
}catch (e) {
} catch (e) {
return false;
}
}

// check if the master page is active
is_master_active(): boolean {
return window.opener && window.opener.WebIO && !window.opener.WebIO._state.CurrentSession.closed() &&
this._master_id == window.opener.WebIO._state.Random
}

on_session_create(callback: () => any): void {
this._session_create_callbacks.push(callback);
};
Expand All @@ -93,11 +101,18 @@ export class SubPageSession implements Session {
start_session(debug: boolean): void {
this.debug = debug;
safe_poprun_callbacks(this._session_create_callbacks, 'session_create_callback');
this._master_id = window.opener.WebIO._state.Random;

// @ts-ignore
window._pywebio_page.resolve(this);

setInterval(() => {
if (!this.is_master_active())
this.close_session();
}, 300);
};


// called by opener, transfer command to this session
server_message(command: Command) {
if (this.debug)
Expand All @@ -107,11 +122,15 @@ export class SubPageSession implements Session {

// send text message to opener
send_message(msg: ClientEvent, onprogress?: (loaded: number, total: number) => void): void {
if (this.closed() || !this.is_master_active())
return error_alert(t("disconnected_with_server"));
window.opener.WebIO._state.CurrentSession.send_message(msg, onprogress);
}

// send binary message to opener
send_buffer(data: Blob, onprogress?: (loaded: number, total: number) => void): void {
if (this.closed() || !this.is_master_active())
return error_alert(t("disconnected_with_server"));
window.opener.WebIO._state.CurrentSession.send_buffer(data, onprogress);
}

Expand Down Expand Up @@ -240,6 +259,7 @@ export class WebSocketSession implements Session {
close_session(): void {
this._closed = true;
safe_poprun_callbacks(this._session_close_callbacks, 'session_close_callback');
CloseSession()
try {
this.ws.close();
} catch (e) {
Expand Down Expand Up @@ -367,6 +387,7 @@ export class HttpSession implements Session {
close_session(): void {
this._closed = true;
safe_poprun_callbacks(this._session_close_callbacks, 'session_close_callback');
CloseSession()
clearInterval(this.interval_pull_id);
}

Expand Down
2 changes: 2 additions & 0 deletions webiojs/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Session} from "./session";
import {randomid} from "./utils";

// Runtime state
export let state = {
Expand All @@ -9,6 +10,7 @@ export let state = {
InputPanelInitHeight: 300, // 输入panel的初始高度
FixedInputPanel:true,
AutoFocusOnInput:true,
Random: randomid(10),
};

// App config
Expand Down

0 comments on commit 0834386

Please sign in to comment.