Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Dec 17, 2023
1 parent 32aa193 commit 825521c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion demo/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LoginForm(BaseModel):

@router.post('/login', response_model=FastUI, response_model_exclude_none=True)
async def login_form_post(form: Annotated[LoginForm, fastui_form(LoginForm)]) -> list[AnyComponent]:
token = await db.save_user(form.email)
token = await db.create_user(form.email)
return [c.FireEvent(event=AuthEvent(token=token, url='/auth/profile'))]


Expand Down
16 changes: 9 additions & 7 deletions demo/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ async def get_user(token: str) -> User | None:
return User(*rs.rows[0])


async def save_user(email: str) -> str:
async def create_user(email: str) -> str:
async with _connect() as conn:
await _delete_old_users(conn)
token = secrets.token_hex()
await conn.execute(
'insert into users (token, email) values (?, ?)',
(token, email),
)
await conn.execute('insert into users (token, email) values (?, ?)', (token, email))
return token


Expand All @@ -39,8 +37,8 @@ async def delete_user(user: User) -> None:

async def count_users() -> int:
async with _connect() as conn:
sql = "select count(*) from users where last_active > datetime(current_timestamp, '-1 hour')"
rs = await conn.execute(sql)
await _delete_old_users(conn)
rs = await conn.execute('select count(*) from users')
return rs.rows[0][0]


Expand All @@ -60,6 +58,10 @@ async def create_db() -> None:
"""


async def _delete_old_users(conn: libsql_client.Client) -> None:
await conn.execute('delete from users where last_active < datetime(current_timestamp, "-1 hour")')


@asynccontextmanager
async def _connect() -> libsql_client.Client:
auth_token = os.getenv('SQLITE_AUTH_TOKEN')
Expand Down
4 changes: 2 additions & 2 deletions src/npm-fastui/src/components/ServerLoad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const ServerLoadDefer: FC<{ path: string; components: FastProps[]; loadTrigger?:
loadTrigger,
sse,
}) => {
const { eventContext, fireId } = usePageEventListen(loadTrigger)
const { eventContext } = usePageEventListen(loadTrigger)

if (fireId) {
if (eventContext) {
return (
<EventContextProvider context={eventContext}>
{sse ? <ServerLoadSSE path={path} /> : <ServerLoadFetch path={path} />}
Expand Down

0 comments on commit 825521c

Please sign in to comment.