From dfaadafaf0900b4b1f38ac6a8b68d43c71388310 Mon Sep 17 00:00:00 2001 From: laffra Date: Mon, 27 Nov 2023 19:39:18 +0100 Subject: [PATCH] Use worker.sync for pubsub --- examples/custom.py | 2 +- examples/pubsub.py | 7 +++ examples/worker.py | 12 +++++ examples/worker.toml | 0 ltk/jquery.py | 9 +++- ltk/logger.py | 28 ++++++---- ltk/ltk.css | 82 ++++++++++------------------- ltk/pubsub.py | 121 +++++++++++++++++++++++++++---------------- ltk/widgets.py | 25 +++++++++ 9 files changed, 172 insertions(+), 114 deletions(-) create mode 100644 examples/worker.py create mode 100644 examples/worker.toml diff --git a/examples/custom.py b/examples/custom.py index afb8131..35acde4 100644 --- a/examples/custom.py +++ b/examples/custom.py @@ -11,7 +11,7 @@ def __init__(self, src, label): ltk.Image(src).width(196), ltk.Text(label).width("100%").css("text-align", "center") ) - self.element.css("border", "2px solid orange") + self.css("border", "2px solid orange") return ( diff --git a/examples/pubsub.py b/examples/pubsub.py index d0f81c8..cabdbb9 100644 --- a/examples/pubsub.py +++ b/examples/pubsub.py @@ -2,6 +2,11 @@ import ltk import random +from polyscript import XWorker + +print("launch worker") +worker = XWorker("/examples/worker.py", config="/examples/worker.toml", type="micropython") +print("worker", worker) fan = ltk.Preformatted("") @@ -18,6 +23,7 @@ def publish(event=None): ]) ltk.publish( "Influencer", # the sender + "Fan", # the intended receiver "message", # the subscription topic message # the message to send ) @@ -34,6 +40,7 @@ def create(): ltk.Important("What every fan sees:"), ltk.HBox( fan + .attr("id", "fan") .css("border", "1px solid gray") .css("overflow", "hidden") .width(200) diff --git a/examples/worker.py b/examples/worker.py new file mode 100644 index 0000000..07250c1 --- /dev/null +++ b/examples/worker.py @@ -0,0 +1,12 @@ +import ltk +from polyscript import xworker # type: ignore + +ltk.find("#fan").append("Worker is running...") + +subscribe = xworker.sync.subscribe +publish = xworker.sync.publish + +def handle_message(message): + ltk.find("#fan").append(message) + +subscribe("Worker-Fan", "message", handle_message) \ No newline at end of file diff --git a/examples/worker.toml b/examples/worker.toml new file mode 100644 index 0000000..e69de29 diff --git a/ltk/jquery.py b/ltk/jquery.py index 14a4572..1a77a7c 100644 --- a/ltk/jquery.py +++ b/ltk/jquery.py @@ -123,8 +123,13 @@ def push_state(url): window.history.pushState(None, "", url) -def inject_script(url): - create("