middleware.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Middleware Mixin that allow to add middleware to the app."""
  2. from __future__ import annotations
  3. import dataclasses
  4. import inspect
  5. from reflex.event import Event
  6. from reflex.middleware import HydrateMiddleware, Middleware
  7. from reflex.state import BaseState, StateUpdate
  8. from .mixin import AppMixin
  9. @dataclasses.dataclass
  10. class MiddlewareMixin(AppMixin):
  11. """Middleware Mixin that allow to add middleware to the app."""
  12. # Middleware to add to the app. Users should use `add_middleware`.
  13. _middlewares: list[Middleware] = dataclasses.field(default_factory=list)
  14. def _init_mixin(self):
  15. self._middlewares.append(HydrateMiddleware())
  16. def add_middleware(self, middleware: Middleware, index: int | None = None):
  17. """Add middleware to the app.
  18. Args:
  19. middleware: The middleware to add.
  20. index: The index to add the middleware at.
  21. """
  22. if index is None:
  23. self._middlewares.append(middleware)
  24. else:
  25. self._middlewares.insert(index, middleware)
  26. async def _preprocess(self, state: BaseState, event: Event) -> StateUpdate | None:
  27. """Preprocess the event.
  28. This is where middleware can modify the event before it is processed.
  29. Each middleware is called in the order it was added to the app.
  30. If a middleware returns an update, the event is not processed and the
  31. update is returned.
  32. Args:
  33. state: The state to preprocess.
  34. event: The event to preprocess.
  35. Returns:
  36. An optional state to return.
  37. """
  38. for middleware in self._middlewares:
  39. out = middleware.preprocess(app=self, state=state, event=event) # pyright: ignore [reportArgumentType]
  40. if inspect.isawaitable(out):
  41. out = await out
  42. if out is not None:
  43. return out
  44. async def _postprocess(
  45. self, state: BaseState, event: Event, update: StateUpdate
  46. ) -> StateUpdate:
  47. """Postprocess the event.
  48. This is where middleware can modify the delta after it is processed.
  49. Each middleware is called in the order it was added to the app.
  50. Args:
  51. state: The state to postprocess.
  52. event: The event to postprocess.
  53. update: The current state update.
  54. Returns:
  55. The state update to return.
  56. """
  57. out = update
  58. for middleware in self._middlewares:
  59. out = middleware.postprocess(
  60. app=self, # pyright: ignore [reportArgumentType]
  61. state=state,
  62. event=event,
  63. update=update,
  64. )
  65. if inspect.isawaitable(out):
  66. out = await out
  67. return out # pyright: ignore[reportReturnType]