Skip to content

Commit 62f845a

Browse files
committed
Make Pusher a class
1 parent 5b6e565 commit 62f845a

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

fasthtml/_modidx.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
'fasthtml.core.HttpHeader': ('api/core.html#httpheader', 'fasthtml/core.py'),
4747
'fasthtml.core.MiddlewareBase': ('api/core.html#middlewarebase', 'fasthtml/core.py'),
4848
'fasthtml.core.MiddlewareBase.__call__': ('api/core.html#middlewarebase.__call__', 'fasthtml/core.py'),
49+
'fasthtml.core.Pusher': ('api/core.html#pusher', 'fasthtml/core.py'),
50+
'fasthtml.core.Pusher.__call__': ('api/core.html#pusher.__call__', 'fasthtml/core.py'),
51+
'fasthtml.core.Pusher.__init__': ('api/core.html#pusher.__init__', 'fasthtml/core.py'),
52+
'fasthtml.core.Pusher.queue': ('api/core.html#pusher.queue', 'fasthtml/core.py'),
53+
'fasthtml.core.Pusher.set_q': ('api/core.html#pusher.set_q', 'fasthtml/core.py'),
4954
'fasthtml.core.Redirect': ('api/core.html#redirect', 'fasthtml/core.py'),
5055
'fasthtml.core.Redirect.__init__': ('api/core.html#redirect.__init__', 'fasthtml/core.py'),
5156
'fasthtml.core.Redirect.__response__': ('api/core.html#redirect.__response__', 'fasthtml/core.py'),
@@ -99,7 +104,6 @@
99104
'fasthtml.core.get_key': ('api/core.html#get_key', 'fasthtml/core.py'),
100105
'fasthtml.core.parse_form': ('api/core.html#parse_form', 'fasthtml/core.py'),
101106
'fasthtml.core.parsed_date': ('api/core.html#parsed_date', 'fasthtml/core.py'),
102-
'fasthtml.core.pusher': ('api/core.html#pusher', 'fasthtml/core.py'),
103107
'fasthtml.core.reg_re_param': ('api/core.html#reg_re_param', 'fasthtml/core.py'),
104108
'fasthtml.core.serve': ('api/core.html#serve', 'fasthtml/core.py'),
105109
'fasthtml.core.signal_shutdown': ('api/core.html#signal_shutdown', 'fasthtml/core.py'),

fasthtml/core.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'scopesrc', 'viewport', 'charset', 'all_meths', 'parsed_date', 'snake2hyphens', 'HtmxHeaders', 'HttpHeader',
88
'HtmxResponseHeaders', 'form2dict', 'parse_form', 'flat_xt', 'Beforeware', 'EventStream', 'signal_shutdown',
99
'WS_RouteX', 'uri', 'decode_uri', 'flat_tuple', 'Redirect', 'RouteX', 'RouterX', 'get_key', 'def_hdrs',
10-
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'pusher']
10+
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'Pusher']
1111

1212
# %% ../nbs/api/00_core.ipynb
1313
import json,uuid,inspect,types,uvicorn,signal,asyncio,threading
@@ -689,21 +689,32 @@ def _add_ids(s):
689689
for c in s.children: _add_ids(c)
690690

691691
# %% ../nbs/api/00_core.ipynb
692-
def pusher(app, dest_id='_dest', auto_id=True):
693-
queue = asyncio.Queue()
694-
695-
@app.ws("/ws")
696-
async def ws(ws, send):
697-
while True: await send(await queue.get())
698-
699-
@app.route
700-
def index():
701-
return Div(id=dest_id, hx_trigger='load', ws_send=True, hx_ext="ws", ws_connect="/ws")
702-
703-
def push(*s):
692+
class Pusher:
693+
def __init__(self, app, dest_id='_dest', auto_id=True):
694+
store_attr()
695+
self._queue = None
696+
self('')
697+
@app.route
698+
def index():
699+
return Div(id=self.dest_id, hx_trigger='load', hx_ext="ws",
700+
ws_send=True, ws_connect="/ws")
701+
702+
@property
703+
def queue(self):
704+
self.set_q()
705+
return self._queue
706+
707+
def set_q(self):
708+
if self._queue: return
709+
self._queue = asyncio.Queue()
710+
@self.app.ws("/ws")
711+
async def ws(ws, send):
712+
try:
713+
while True: await send(await self.queue.get())
714+
except WebSocketDisconnect: self._queue=None
715+
716+
def __call__(self, *s):
704717
id = getattr(s[0], 'id', None)
705-
if not id: s = Div(*s, hx_swap_oob='innerHTML', id=dest_id)
706-
if auto_id: _add_ids(s)
707-
queue.put_nowait(s)
708-
709-
return push
718+
if not id: s = Div(*s, hx_swap_oob='innerHTML', id=self.dest_id)
719+
if self.auto_id: _add_ids(s)
720+
self.queue.put_nowait(s)

fasthtml/starlette.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
from starlette.types import ASGIApp, Receive, Scope, Send
2020
from starlette.concurrency import run_in_threadpool
2121
from starlette.background import BackgroundTask, BackgroundTasks
22+
from starlette.websockets import WebSocketDisconnect
2223

nbs/api/00_core.ipynb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,24 +2683,35 @@
26832683
"outputs": [],
26842684
"source": [
26852685
"#| export\n",
2686-
"def pusher(app, dest_id='_dest', auto_id=True):\n",
2687-
" queue = asyncio.Queue()\n",
2688-
"\n",
2689-
" @app.ws(\"/ws\")\n",
2690-
" async def ws(ws, send):\n",
2691-
" while True: await send(await queue.get())\n",
2692-
"\n",
2693-
" @app.route\n",
2694-
" def index():\n",
2695-
" return Div(id=dest_id, hx_trigger='load', ws_send=True, hx_ext=\"ws\", ws_connect=\"/ws\")\n",
2696-
"\n",
2697-
" def push(*s):\n",
2686+
"class Pusher:\n",
2687+
" def __init__(self, app, dest_id='_dest', auto_id=True):\n",
2688+
" store_attr()\n",
2689+
" self._queue = None\n",
2690+
" self('')\n",
2691+
" @app.route\n",
2692+
" def index():\n",
2693+
" return Div(id=self.dest_id, hx_trigger='load', hx_ext=\"ws\",\n",
2694+
" ws_send=True, ws_connect=\"/ws\")\n",
2695+
" \n",
2696+
" @property\n",
2697+
" def queue(self):\n",
2698+
" self.set_q()\n",
2699+
" return self._queue\n",
2700+
"\n",
2701+
" def set_q(self):\n",
2702+
" if self._queue: return\n",
2703+
" self._queue = asyncio.Queue()\n",
2704+
" @self.app.ws(\"/ws\")\n",
2705+
" async def ws(ws, send):\n",
2706+
" try:\n",
2707+
" while True: await send(await self.queue.get())\n",
2708+
" except WebSocketDisconnect: self._queue=None\n",
2709+
"\n",
2710+
" def __call__(self, *s):\n",
26982711
" id = getattr(s[0], 'id', None)\n",
2699-
" if not id: s = Div(*s, hx_swap_oob='innerHTML', id=dest_id)\n",
2700-
" if auto_id: _add_ids(s)\n",
2701-
" queue.put_nowait(s)\n",
2702-
"\n",
2703-
" return push"
2712+
" if not id: s = Div(*s, hx_swap_oob='innerHTML', id=self.dest_id)\n",
2713+
" if self.auto_id: _add_ids(s)\n",
2714+
" self.queue.put_nowait(s)"
27042715
]
27052716
},
27062717
{

0 commit comments

Comments
 (0)