dotnet: call call_soon_threadsafe() once per event
This changes the ConnectionStatusChanged event handler so that it only
calls loop.call_soon_threadsafe() once per callback.
@@ -186,24 +186,31 @@ class BleakClientDotNet(BaseBleakClient):
self._session.Dispose()
self._session = None
- loop = asyncio.get_event_loop()
-
- def _ConnectionStatusChanged_Handler(sender, args):
- logger.debug(
- "_ConnectionStatusChanged_Handler: %d", sender.ConnectionStatus
- )
- if sender.ConnectionStatus == BluetoothConnectionStatus.Connected:
+ def handle_connection_status_changed(
+ connection_status: BluetoothConnectionStatus,
+ ):
+ if connection_status == BluetoothConnectionStatus.Connected:
for e in self._connect_events:
- loop.call_soon_threadsafe(e.set)
+ e.set()
- if sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected:
+ elif connection_status == BluetoothConnectionStatus.Disconnected:
if self._disconnected_callback:
- loop.call_soon_threadsafe(self._disconnected_callback, self)
+ self._disconnected_callback(self)
for e in self._disconnect_events:
- loop.call_soon_threadsafe(e.set)
+ e.set()
+
+ handle_disconnect()
+
+ loop = asyncio.get_event_loop()
- loop.call_soon_threadsafe(handle_disconnect)
+ def _ConnectionStatusChanged_Handler(sender, args):
+ logger.debug(
+ "_ConnectionStatusChanged_Handler: %d", sender.ConnectionStatus
+ )
+ loop.call_soon_threadsafe(
+ handle_connection_status_changed, sender.ConnectionStatus
+ )
self._connection_status_changed_token = (
self._requester.add_ConnectionStatusChanged(
