|
1 | 1 | import asyncio |
2 | 2 | from concurrent.futures import ThreadPoolExecutor |
| 3 | +from functools import wraps |
3 | 4 | import logging |
4 | | -from typing import List, Literal, Optional |
| 5 | +from typing import Callable, List, Literal, Optional |
5 | 6 |
|
6 | 7 | from .utils.logger import logger |
7 | 8 | from .filters import ConnectionType |
@@ -34,6 +35,7 @@ def __init__( |
34 | 35 | self.__client_task = None |
35 | 36 | self.__sip_client = None |
36 | 37 | self.__calls: List[SipCall] = [] |
| 38 | + self.__pending_callbacks: List[Callable] = [] |
37 | 39 | if self.connection_type == "AUTO": |
38 | 40 | self.__setup_connection_type() |
39 | 41 |
|
@@ -97,6 +99,11 @@ async def register(self): |
97 | 99 | caller_id=self.caller_id or "", |
98 | 100 | sip_core=self.sip_core, |
99 | 101 | ) |
| 102 | + # Register any pending callbacks |
| 103 | + for callback in self.__pending_callbacks: |
| 104 | + self.__sip_client._register_callback("incoming_call_cb", callback) |
| 105 | + self.__pending_callbacks = [] # clear pending callbacks |
| 106 | + |
100 | 107 | self.__client_task = asyncio.create_task(self.__sip_client.run()) |
101 | 108 | try: |
102 | 109 | await asyncio.wait_for(self.__sip_client.registered.wait(), 4) |
@@ -139,3 +146,14 @@ def remove_call(self, call: SipCall): |
139 | 146 | self.__calls.remove(call) |
140 | 147 | except ValueError: |
141 | 148 | pass |
| 149 | + |
| 150 | + def on_incoming_call(self, func): |
| 151 | + @wraps(func) |
| 152 | + async def wrapper(call: SipCall): |
| 153 | + return await func(call) |
| 154 | + |
| 155 | + if self.__sip_client: |
| 156 | + self.__sip_client._register_callback("incoming_call_cb", wrapper) |
| 157 | + else: |
| 158 | + self.__pending_callbacks.append(wrapper) |
| 159 | + return |
0 commit comments