Skip to content

Commit

Permalink
feat: pin_wait_change() support timeout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Oct 3, 2021
1 parent 434ebac commit 7512fa1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/spec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ pin_wait

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

* names
* names: list,
* timeout: int,


popup
Expand Down
9 changes: 6 additions & 3 deletions pywebio/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,15 @@ def __setitem__(self, name, value):
pin = Pin_()


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

send_msg('pin_wait', spec=dict(names=names))
send_msg('pin_wait', spec=dict(names=names, timeout=timeout))

return get_client_val()

Expand Down
2 changes: 1 addition & 1 deletion webiojs/src/handlers/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class PinHandler implements CommandHandler {
} else if (msg.command === 'pin_update') {
PinUpdate(msg.spec.name, msg.spec.attributes);
} else if (msg.command === 'pin_wait') {
let p = WaitChange(msg.spec.names);
let p = WaitChange(msg.spec.names, msg.spec.timeout);
Promise.resolve(p).then(function (value) {
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: value});
}).catch((error) => {
Expand Down
9 changes: 8 additions & 1 deletion webiojs/src/models/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function PinUpdate(name: string, attributes: { [k: string]: any }) {

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

export function WaitChange(names: string[]) {
export function WaitChange(names: string[], timeout: number) {
let promises = [];
for (let name of names) {
if (!(name in onchange_callbacks))
Expand All @@ -39,6 +39,13 @@ export function WaitChange(names: string[]) {
});
}));
}
if (timeout) {
promises.push(new Promise(resolve => {
setTimeout(() => {
resolve(null);
}, timeout * 1000);
}));
}
return Promise.race(promises);
}

Expand Down

0 comments on commit 7512fa1

Please sign in to comment.