|
| 1 | +import asyncio |
| 2 | +import inspect |
| 3 | +import sys |
| 4 | + |
| 5 | +from functools import wraps |
| 6 | +from pyscript.magic_js import document |
| 7 | +from pyscript.ffi import create_proxy |
| 8 | +from pyscript.util import is_awaitable |
| 9 | +from pyscript import config |
| 10 | + |
| 11 | + |
| 12 | +class Event: |
| 13 | + """ |
| 14 | + Represents something that may happen at some point in the future. |
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self): |
| 18 | + self._listeners = [] |
| 19 | + |
| 20 | + def trigger(self, result): |
| 21 | + """ |
| 22 | + Trigger the event with a result to pass into the handlers. |
| 23 | + """ |
| 24 | + for listener in self._listeners: |
| 25 | + if is_awaitable(listener): |
| 26 | + # Use create task to avoid making this an async function. |
| 27 | + asyncio.create_task(listener(result)) |
| 28 | + else: |
| 29 | + listener(result) |
| 30 | + |
| 31 | + def add_listener(self, listener): |
| 32 | + """ |
| 33 | + Add a callable/awaitable to listen to when this event is triggered. |
| 34 | + """ |
| 35 | + if is_awaitable(listener) or callable(listener): |
| 36 | + if listener not in self._listeners: |
| 37 | + self._listeners.append(listener) |
| 38 | + else: |
| 39 | + raise ValueError("Listener must be callable or awaitable.") |
| 40 | + |
| 41 | + def remove_listener(self, *args): |
| 42 | + """ |
| 43 | + Clear the specified handler functions in *args. If no handlers |
| 44 | + provided, clear all handlers. |
| 45 | + """ |
| 46 | + if args: |
| 47 | + for listener in args: |
| 48 | + self._listeners.remove(listener) |
| 49 | + else: |
| 50 | + self._listeners = [] |
| 51 | + |
| 52 | + |
| 53 | +def when(target, *args, **kwargs): |
| 54 | + """ |
| 55 | + Add an event listener to the target element(s) for the specified event type. |
| 56 | +
|
| 57 | + The target can be a string representing the event type, or an Event object. |
| 58 | + If the target is an Event object, the event listener will be added to that |
| 59 | + object. If the target is a string, the event listener will be added to the |
| 60 | + element(s) that match the (second) selector argument. |
| 61 | +
|
| 62 | + If a (third) handler argument is provided, it will be called when the event |
| 63 | + is triggered; thus allowing this to be used as both a function and a |
| 64 | + decorator. |
| 65 | + """ |
| 66 | + # If "when" is called as a function, try to grab the handler from the |
| 67 | + # arguments. If there's no handler, this must be a decorator based call. |
| 68 | + handler = None |
| 69 | + if args and (callable(args[0]) or is_awaitable(args[0])): |
| 70 | + handler = args[0] |
| 71 | + elif callable(kwargs.get("handler")) or is_awaitable(kwargs.get("handler")): |
| 72 | + handler = kwargs.pop("handler") |
| 73 | + # If the target is a string, it is the "older" use of `when` where it |
| 74 | + # represents the name of a DOM event. |
| 75 | + if isinstance(target, str): |
| 76 | + # Extract the selector from the arguments or keyword arguments. |
| 77 | + selector = args[0] if args else kwargs.pop("selector") |
| 78 | + if not selector: |
| 79 | + raise ValueError("No selector provided.") |
| 80 | + # Grab the DOM elements to which the target event will be attached. |
| 81 | + from pyscript.web import Element, ElementCollection |
| 82 | + |
| 83 | + if isinstance(selector, str): |
| 84 | + elements = document.querySelectorAll(selector) |
| 85 | + elif isinstance(selector, Element): |
| 86 | + elements = [selector._dom_element] |
| 87 | + elif isinstance(selector, ElementCollection): |
| 88 | + elements = [el._dom_element for el in selector] |
| 89 | + else: |
| 90 | + elements = selector if isinstance(selector, list) else [selector] |
| 91 | + |
| 92 | + def decorator(func): |
| 93 | + if config["type"] == "mpy": # Is MicroPython? |
| 94 | + if is_awaitable(func): |
| 95 | + |
| 96 | + async def wrapper(*args, **kwargs): |
| 97 | + """ |
| 98 | + This is a very ugly hack to get micropython working because |
| 99 | + `inspect.signature` doesn't exist. It may be actually better |
| 100 | + to not try any magic for now and raise the error. |
| 101 | + """ |
| 102 | + try: |
| 103 | + return await func(*args, **kwargs) |
| 104 | + |
| 105 | + except TypeError as e: |
| 106 | + if "takes" in str(e) and "positional arguments" in str(e): |
| 107 | + return await func() |
| 108 | + raise |
| 109 | + |
| 110 | + else: |
| 111 | + |
| 112 | + def wrapper(*args, **kwargs): |
| 113 | + """ |
| 114 | + This is a very ugly hack to get micropython working because |
| 115 | + `inspect.signature` doesn't exist. It may be actually better |
| 116 | + to not try any magic for now and raise the error. |
| 117 | + """ |
| 118 | + try: |
| 119 | + return func(*args, **kwargs) |
| 120 | + |
| 121 | + except TypeError as e: |
| 122 | + if "takes" in str(e) and "positional arguments" in str(e): |
| 123 | + return func() |
| 124 | + raise |
| 125 | + |
| 126 | + else: |
| 127 | + sig = inspect.signature(func) |
| 128 | + if sig.parameters: |
| 129 | + if is_awaitable(func): |
| 130 | + |
| 131 | + async def wrapper(event): |
| 132 | + return await func(event) |
| 133 | + |
| 134 | + else: |
| 135 | + wrapper = func |
| 136 | + else: |
| 137 | + # Function doesn't receive events. |
| 138 | + if is_awaitable(func): |
| 139 | + |
| 140 | + async def wrapper(*args, **kwargs): |
| 141 | + return await func() |
| 142 | + |
| 143 | + else: |
| 144 | + |
| 145 | + def wrapper(*args, **kwargs): |
| 146 | + return func() |
| 147 | + |
| 148 | + wrapper = wraps(func)(wrapper) |
| 149 | + if isinstance(target, Event): |
| 150 | + # The target is a single Event object. |
| 151 | + target.add_listener(wrapper) |
| 152 | + elif isinstance(target, list) and all(isinstance(t, Event) for t in target): |
| 153 | + # The target is a list of Event objects. |
| 154 | + for evt in target: |
| 155 | + evt.add_listener(wrapper) |
| 156 | + else: |
| 157 | + # The target is a string representing an event type, and so a |
| 158 | + # DOM element or collection of elements is found in "elements". |
| 159 | + for el in elements: |
| 160 | + el.addEventListener(target, create_proxy(wrapper)) |
| 161 | + return wrapper |
| 162 | + |
| 163 | + # If "when" was called as a decorator, return the decorator function, |
| 164 | + # otherwise just call the internal decorator function with the supplied |
| 165 | + # handler. |
| 166 | + return decorator(handler) if handler else decorator |
0 commit comments