You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/configuring.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,24 @@ For commands and queries, middlewares run once around the single handler. For ev
46
46
!!! note
47
47
The generator was chosen to keep both the input message and the return value read-only.
48
48
49
+
### Classic middlewares
50
+
51
+
As an alternative, classic middlewares receive `call_next` as their first argument, followed by the handler's arguments. This pattern allows you to read and modify the return value:
52
+
```python
53
+
from collections.abc import Awaitable, Callable
54
+
from typing import Any
55
+
import time
56
+
57
+
asyncdeftiming_middleware(
58
+
call_next: Callable[[Any], Awaitable[Any]],
59
+
message: Any,
60
+
) -> Any:
61
+
start = time.time()
62
+
result =await call_next(message)
63
+
print(f"Execution time: {time.time() - start}s")
64
+
return result
65
+
```
66
+
49
67
## Class-based listeners and middlewares
50
68
51
69
For more flexibility, listeners and middlewares can be defined as classes with a `__call__` method. This allows you to inject dependencies and configure their behavior.
0 commit comments