Skip to content

Commit 2cd9955

Browse files
committed
raise error when input after page close
1 parent 037c960 commit 2cd9955

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

pywebio/io_ctrl.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .session import chose_impl, next_client_event, get_current_task_id, get_current_session
1111
from .utils import random_str
12+
from .exceptions import PageClosedException
1213

1314
logger = logging.getLogger(__name__)
1415

@@ -383,6 +384,11 @@ def input_event_handle(item_valid_funcs, form_valid_funcs, preprocess_funcs, onc
383384
elif event_name == 'from_cancel':
384385
data = None
385386
break
387+
elif event_name == 'page_close':
388+
current_page = get_current_session().get_page_id(check_active=False)
389+
closed_page = event_data
390+
if closed_page == current_page:
391+
raise PageClosedException
386392
else:
387393
logger.warning("Unhandled Event: %s", event)
388394

pywebio/session/base.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def push_scope(self, name):
9797
task_id = type(self).get_current_task_id()
9898
self.scope_stack[task_id].append(name)
9999

100-
def get_page_id(self):
100+
def get_page_id(self, check_active=True):
101101
"""
102102
get the if of current page in task, return `None` when it's master page,
103103
raise PageClosedException when current page is closed
@@ -108,7 +108,7 @@ def get_page_id(self):
108108
return None
109109

110110
page_id = self.page_stack[task_id][-1]
111-
if page_id not in self.active_page[task_id]:
111+
if page_id not in self.active_page[task_id] and check_active:
112112
raise PageClosedException(
113113
"The page is closed by app user, "
114114
"you see this exception mostly because you set `silent_quit=False` in `pywebio.output.page()`"
@@ -150,13 +150,8 @@ def next_client_event(self) -> dict:
150150
raise NotImplementedError
151151

152152
def send_client_event(self, event):
153-
"""send event from client to session,
154-
return True when this event is handled by this method, which means the subclass must ignore this event.
155-
"""
156-
if event['event'] == 'page_close':
157-
self.notify_page_lost(event['task_id'], event['data'])
158-
return True
159-
return False
153+
"""send event from client to session"""
154+
raise NotImplementedError
160155

161156
def get_task_commands(self) -> list:
162157
raise NotImplementedError

pywebio/session/coroutinebased.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ def send_client_event(self, event):
138138
139139
:param dict event: 事件️消息
140140
"""
141-
handled = super(CoroutineBasedSession, self).send_client_event(event)
142-
if handled:
143-
return
141+
if event['event'] == 'page_close':
142+
self.notify_page_lost(event['task_id'], event['data'])
144143

145144
coro_id = event['task_id']
146145
coro = self.coros.get(coro_id)

pywebio/session/threadbased.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ def send_client_event(self, event):
146146
147147
:param dict event: 事件️消息
148148
"""
149-
handled = super(ThreadBasedSession, self).send_client_event(event)
150-
if handled:
151-
return
149+
if event['event'] == 'page_close':
150+
self.notify_page_lost(event['task_id'], event['data'])
152151

153152
task_id = event['task_id']
154153
mq = self.task_mqs.get(task_id)

0 commit comments

Comments
 (0)