Skip to content

Commit

Permalink
Make Pusher a class
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 13, 2024
1 parent 5b6e565 commit 62f845a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
6 changes: 5 additions & 1 deletion fasthtml/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
'fasthtml.core.HttpHeader': ('api/core.html#httpheader', 'fasthtml/core.py'),
'fasthtml.core.MiddlewareBase': ('api/core.html#middlewarebase', 'fasthtml/core.py'),
'fasthtml.core.MiddlewareBase.__call__': ('api/core.html#middlewarebase.__call__', 'fasthtml/core.py'),
'fasthtml.core.Pusher': ('api/core.html#pusher', 'fasthtml/core.py'),
'fasthtml.core.Pusher.__call__': ('api/core.html#pusher.__call__', 'fasthtml/core.py'),
'fasthtml.core.Pusher.__init__': ('api/core.html#pusher.__init__', 'fasthtml/core.py'),
'fasthtml.core.Pusher.queue': ('api/core.html#pusher.queue', 'fasthtml/core.py'),
'fasthtml.core.Pusher.set_q': ('api/core.html#pusher.set_q', 'fasthtml/core.py'),
'fasthtml.core.Redirect': ('api/core.html#redirect', 'fasthtml/core.py'),
'fasthtml.core.Redirect.__init__': ('api/core.html#redirect.__init__', 'fasthtml/core.py'),
'fasthtml.core.Redirect.__response__': ('api/core.html#redirect.__response__', 'fasthtml/core.py'),
Expand Down Expand Up @@ -99,7 +104,6 @@
'fasthtml.core.get_key': ('api/core.html#get_key', 'fasthtml/core.py'),
'fasthtml.core.parse_form': ('api/core.html#parse_form', 'fasthtml/core.py'),
'fasthtml.core.parsed_date': ('api/core.html#parsed_date', 'fasthtml/core.py'),
'fasthtml.core.pusher': ('api/core.html#pusher', 'fasthtml/core.py'),
'fasthtml.core.reg_re_param': ('api/core.html#reg_re_param', 'fasthtml/core.py'),
'fasthtml.core.serve': ('api/core.html#serve', 'fasthtml/core.py'),
'fasthtml.core.signal_shutdown': ('api/core.html#signal_shutdown', 'fasthtml/core.py'),
Expand Down
47 changes: 29 additions & 18 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'scopesrc', 'viewport', 'charset', 'all_meths', 'parsed_date', 'snake2hyphens', 'HtmxHeaders', 'HttpHeader',
'HtmxResponseHeaders', 'form2dict', 'parse_form', 'flat_xt', 'Beforeware', 'EventStream', 'signal_shutdown',
'WS_RouteX', 'uri', 'decode_uri', 'flat_tuple', 'Redirect', 'RouteX', 'RouterX', 'get_key', 'def_hdrs',
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'pusher']
'FastHTML', 'serve', 'Client', 'cookie', 'reg_re_param', 'MiddlewareBase', 'FtResponse', 'unqid', 'Pusher']

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

# %% ../nbs/api/00_core.ipynb
def pusher(app, dest_id='_dest', auto_id=True):
queue = asyncio.Queue()

@app.ws("/ws")
async def ws(ws, send):
while True: await send(await queue.get())

@app.route
def index():
return Div(id=dest_id, hx_trigger='load', ws_send=True, hx_ext="ws", ws_connect="/ws")

def push(*s):
class Pusher:
def __init__(self, app, dest_id='_dest', auto_id=True):
store_attr()
self._queue = None
self('')
@app.route
def index():
return Div(id=self.dest_id, hx_trigger='load', hx_ext="ws",
ws_send=True, ws_connect="/ws")

@property
def queue(self):
self.set_q()
return self._queue

def set_q(self):
if self._queue: return
self._queue = asyncio.Queue()
@self.app.ws("/ws")
async def ws(ws, send):
try:
while True: await send(await self.queue.get())
except WebSocketDisconnect: self._queue=None

def __call__(self, *s):
id = getattr(s[0], 'id', None)
if not id: s = Div(*s, hx_swap_oob='innerHTML', id=dest_id)
if auto_id: _add_ids(s)
queue.put_nowait(s)

return push
if not id: s = Div(*s, hx_swap_oob='innerHTML', id=self.dest_id)
if self.auto_id: _add_ids(s)
self.queue.put_nowait(s)
1 change: 1 addition & 0 deletions fasthtml/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
from starlette.types import ASGIApp, Receive, Scope, Send
from starlette.concurrency import run_in_threadpool
from starlette.background import BackgroundTask, BackgroundTasks
from starlette.websockets import WebSocketDisconnect

45 changes: 28 additions & 17 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2683,24 +2683,35 @@
"outputs": [],
"source": [
"#| export\n",
"def pusher(app, dest_id='_dest', auto_id=True):\n",
" queue = asyncio.Queue()\n",
"\n",
" @app.ws(\"/ws\")\n",
" async def ws(ws, send):\n",
" while True: await send(await queue.get())\n",
"\n",
" @app.route\n",
" def index():\n",
" return Div(id=dest_id, hx_trigger='load', ws_send=True, hx_ext=\"ws\", ws_connect=\"/ws\")\n",
"\n",
" def push(*s):\n",
"class Pusher:\n",
" def __init__(self, app, dest_id='_dest', auto_id=True):\n",
" store_attr()\n",
" self._queue = None\n",
" self('')\n",
" @app.route\n",
" def index():\n",
" return Div(id=self.dest_id, hx_trigger='load', hx_ext=\"ws\",\n",
" ws_send=True, ws_connect=\"/ws\")\n",
" \n",
" @property\n",
" def queue(self):\n",
" self.set_q()\n",
" return self._queue\n",
"\n",
" def set_q(self):\n",
" if self._queue: return\n",
" self._queue = asyncio.Queue()\n",
" @self.app.ws(\"/ws\")\n",
" async def ws(ws, send):\n",
" try:\n",
" while True: await send(await self.queue.get())\n",
" except WebSocketDisconnect: self._queue=None\n",
"\n",
" def __call__(self, *s):\n",
" id = getattr(s[0], 'id', None)\n",
" if not id: s = Div(*s, hx_swap_oob='innerHTML', id=dest_id)\n",
" if auto_id: _add_ids(s)\n",
" queue.put_nowait(s)\n",
"\n",
" return push"
" if not id: s = Div(*s, hx_swap_oob='innerHTML', id=self.dest_id)\n",
" if self.auto_id: _add_ids(s)\n",
" self.queue.put_nowait(s)"
]
},
{
Expand Down

0 comments on commit 62f845a

Please sign in to comment.