From 08343867db1e7435c3b25983af56d5c8d1db48fc Mon Sep 17 00:00:00 2001 From: wangweimin Date: Wed, 6 Jul 2022 20:38:09 +0800 Subject: [PATCH] fix sub page session don't close when master reload or close --- webiojs/src/models/page.ts | 9 +++++++++ webiojs/src/session.ts | 23 ++++++++++++++++++++++- webiojs/src/state.ts | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/webiojs/src/models/page.ts b/webiojs/src/models/page.ts index b291860b..18051d62 100644 --- a/webiojs/src/models/page.ts +++ b/webiojs/src/models/page.ts @@ -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() + }); + } } \ No newline at end of file diff --git a/webiojs/src/session.ts b/webiojs/src/session.ts index b4888516..4cc4be28 100644 --- a/webiojs/src/session.ts +++ b/webiojs/src/session.ts @@ -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 @@ -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)[] = []; @@ -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); }; @@ -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) @@ -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); } @@ -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) { @@ -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); } diff --git a/webiojs/src/state.ts b/webiojs/src/state.ts index 2ceffc2d..d819d669 100644 --- a/webiojs/src/state.ts +++ b/webiojs/src/state.ts @@ -1,4 +1,5 @@ import {Session} from "./session"; +import {randomid} from "./utils"; // Runtime state export let state = { @@ -9,6 +10,7 @@ export let state = { InputPanelInitHeight: 300, // 输入panel的初始高度 FixedInputPanel:true, AutoFocusOnInput:true, + Random: randomid(10), }; // App config