Skip to content

Commit 7512fa1

Browse files
committed
feat: pin_wait_change() support timeout parameter
1 parent 434ebac commit 7512fa1

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

docs/spec.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ pin_wait
267267

268268
The ``spec`` fields of ``pin_wait`` commands:
269269

270-
* names
270+
* names: list,
271+
* timeout: int,
271272

272273

273274
popup

pywebio/pin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,15 @@ def __setitem__(self, name, value):
238238
pin = Pin_()
239239

240240

241-
def pin_wait_change(*names):
241+
def pin_wait_change(*names, timeout=None):
242242
"""``pin_wait_change()`` listens to a list of pin widgets, when the value of any widgets changes,
243243
the function returns with the name and value of the changed widget.
244244
245245
:param str names: List of names of pin widget
246-
:return dict: ``{"name": name of the changed widget, "value": current value of the changed widget }``
246+
:param int/None timeout: If ``timeout`` is a positive number, ``pin_wait_change()`` blocks at most ``timeout`` seconds
247+
and returns ``None`` if no changes to the widgets within that time. Set to ``None`` (the default) to disable timeout.
248+
:return dict/None: ``{"name": name of the changed widget, "value": current value of the changed widget }`` ,
249+
when a timeout occurs, return ``None``.
247250
248251
:demo_host:`Here </markdown_previewer>` is an demo of using `pin_wait_change()` to make a markdown previewer.
249252
@@ -257,7 +260,7 @@ def pin_wait_change(*names):
257260
if len(names) == 1 and isinstance(names[0], (list, tuple)):
258261
names = names[0]
259262

260-
send_msg('pin_wait', spec=dict(names=names))
263+
send_msg('pin_wait', spec=dict(names=names, timeout=timeout))
261264

262265
return get_client_val()
263266

webiojs/src/handlers/pin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class PinHandler implements CommandHandler {
2020
} else if (msg.command === 'pin_update') {
2121
PinUpdate(msg.spec.name, msg.spec.attributes);
2222
} else if (msg.command === 'pin_wait') {
23-
let p = WaitChange(msg.spec.names);
23+
let p = WaitChange(msg.spec.names, msg.spec.timeout);
2424
Promise.resolve(p).then(function (value) {
2525
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: value});
2626
}).catch((error) => {

webiojs/src/models/pin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function PinUpdate(name: string, attributes: { [k: string]: any }) {
2727

2828
let onchange_callbacks: { [name: string]: ((val: any) => void)[] } = {}; // name->[]
2929

30-
export function WaitChange(names: string[]) {
30+
export function WaitChange(names: string[], timeout: number) {
3131
let promises = [];
3232
for (let name of names) {
3333
if (!(name in onchange_callbacks))
@@ -39,6 +39,13 @@ export function WaitChange(names: string[]) {
3939
});
4040
}));
4141
}
42+
if (timeout) {
43+
promises.push(new Promise(resolve => {
44+
setTimeout(() => {
45+
resolve(null);
46+
}, timeout * 1000);
47+
}));
48+
}
4249
return Promise.race(promises);
4350
}
4451

0 commit comments

Comments
 (0)