Repository navigation
Expand file tree
/
Copy pathbridge.py
More file actions
72 lines (60 loc) · 2.13 KB
/
Copy pathbridge.py
File metadata and controls
72 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import threading
import queue
class FrontendBridge:
def __init__(self):
self.window = None
self._window_ready = threading.Event()
self._frontend_ready = threading.Event()
self._dispatch_queue: queue.Queue[tuple[str, tuple[object, ...]]] = queue.Queue()
self._feed_lock = threading.Lock()
self._feed_buffer: list[str] = []
self._feed_pending = False
self._dispatch_thread = threading.Thread(
target=self._dispatch_loop,
name="project-center-ui-dispatch",
daemon=True,
)
self._dispatch_thread.start()
def attach_window(self, window):
self.window = window
self._window_ready.set()
def mark_frontend_ready(self):
self._frontend_ready.set()
def call(self, name, *args):
self._dispatch_queue.put((name, args))
def feed(self, chunk):
with self._feed_lock:
self._feed_buffer.append(chunk)
if self._feed_pending:
return
self._feed_pending = True
self._dispatch_queue.put(("term_feed", ()))
def _take_feed_payload(self):
with self._feed_lock:
payload = "".join(self._feed_buffer)
self._feed_buffer.clear()
self._feed_pending = False
return payload
def _dispatch_loop(self):
while True:
name, args = self._dispatch_queue.get()
self._window_ready.wait()
self._frontend_ready.wait()
if self.window is None:
continue
if name == "term_feed":
payload = (self._take_feed_payload(),)
if not payload[0]:
continue
else:
payload = args
script = (
"window.ProjectCenterBridge && "
f"window.ProjectCenterBridge.invoke({json.dumps(name)}, {json.dumps(payload)})"
)
try:
self.window.evaluate_js(script)
except Exception as exc:
print(f"Error dispatching UI event '{name}': {exc}")
frontend = FrontendBridge()