Skip to content

Commit c49cb92

Browse files
Added listeners to the constructing kw options (#2044)
1 parent d1d1c57 commit c49cb92

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

pyscript.core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyscript.core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pyscript/core",
3-
"version": "0.4.26",
3+
"version": "0.4.27",
44
"type": "module",
55
"description": "PyScript",
66
"module": "./index.js",

pyscript.core/src/stdlib/pyscript/websocket.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,24 @@ class WebSocket(object):
3030

3131
def __init__(self, **kw):
3232
url = kw["url"]
33-
socket = None
3433
if protocols in kw:
3534
socket = js.WebSocket.new(url, kw[protocols])
3635
else:
3736
socket = js.WebSocket.new(url)
3837
object.__setattr__(self, "_ws", socket)
3938

39+
for t in ["onclose", "onerror", "onmessage", "onopen"]:
40+
if t in kw:
41+
socket[t] = kw[t]
42+
4043
def __getattr__(self, attr):
4144
return getattr(self._ws, attr)
4245

4346
def __setattr__(self, attr, value):
4447
if attr == "onmessage":
45-
setattr(self._ws, attr, lambda e: value(EventMessage(e)))
48+
self._ws[attr] = lambda e: value(EventMessage(e))
4649
else:
47-
setattr(self._ws, attr, value)
50+
self._ws[attr] = value
4851

4952
def close(self, **kw):
5053
if code in kw and reason in kw:

pyscript.core/test/ws/index.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
print(event.type)
2323
document.documentElement.classList.add("ok")
2424

25-
ws = WebSocket(url="ws://localhost:5037/")
26-
ws.onopen = onopen
27-
ws.onmessage = onmessage
28-
ws.onclose = onclose
25+
ws = WebSocket(
26+
url="ws://localhost:5037/",
27+
onopen=onopen,
28+
onmessage=onmessage,
29+
onclose=onclose
30+
)
2931
</script>
3032
</body>
3133
</html>

0 commit comments

Comments
 (0)