Skip to content

Commit

Permalink
Handle non-async middleware (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
picklelo authored Apr 9, 2023
1 parent e96f1c4 commit ae49759
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pynecone/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""The main Pynecone app."""

import asyncio
import inspect
from typing import Any, Callable, Coroutine, Dict, List, Optional, Tuple, Type, Union

Expand Down Expand Up @@ -167,9 +168,12 @@ async def preprocess(
An optional state to return.
"""
for middleware in self.middleware:
out = await middleware.preprocess(app=self, state=state, event=event)
if asyncio.iscoroutinefunction(middleware.preprocess):
out = await middleware.preprocess(app=self, state=state, event=event)
else:
out = middleware.preprocess(app=self, state=state, event=event)
if out is not None:
return out
return out # type: ignore

async def postprocess(
self, state: State, event: Event, delta: Delta
Expand All @@ -191,11 +195,16 @@ async def postprocess(
An optional state to return.
"""
for middleware in self.middleware:
out = await middleware.postprocess(
app=self, state=state, event=event, delta=delta
)
if asyncio.iscoroutinefunction(middleware.postprocess):
out = await middleware.postprocess(
app=self, state=state, event=event, delta=delta
)
else:
out = middleware.postprocess(
app=self, state=state, event=event, delta=delta
)
if out is not None:
return out
return out # type: ignore

def add_middleware(self, middleware: Middleware, index: Optional[int] = None):
"""Add middleware to the app.
Expand Down

0 comments on commit ae49759

Please sign in to comment.