From 1e764b4e95e080b8407772c7a2ac26dd2f07977f Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 1 Apr 2023 15:08:35 -0500 Subject: [PATCH] ble/sagas: spawn ble writes Code depends on always receiving a didWrite/didFailToWrite, however when a hub disconnected, these tasks were being cancelled. Spawning protects them from cancellation. --- src/ble/sagas.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/ble/sagas.ts b/src/ble/sagas.ts index ee20d72a..82b1b4cd 100644 --- a/src/ble/sagas.ts +++ b/src/ble/sagas.ts @@ -82,12 +82,15 @@ function* handleWriteCommand( char: BluetoothRemoteGATTCharacteristic, action: ReturnType, ): Generator { - try { - yield* call(() => char.writeValueWithResponse(action.value.buffer)); - yield* put(didWriteCommand(action.id)); - } catch (err) { - yield* put(didFailToWriteCommand(action.id, ensureError(err))); - } + // have to spawn to avoid cancellation + yield* spawn(function* () { + try { + yield* call(() => char.writeValueWithResponse(action.value.buffer)); + yield* put(didWriteCommand(action.id)); + } catch (err) { + yield* put(didFailToWriteCommand(action.id, ensureError(err))); + } + }); } function* handleUartValueChanged(data: DataView): Generator { @@ -98,12 +101,15 @@ function* handleWriteUart( char: BluetoothRemoteGATTCharacteristic, action: ReturnType, ): Generator { - try { - yield* call(() => char.writeValueWithoutResponse(action.value.buffer)); - yield* put(didWriteUart(action.id)); - } catch (err) { - yield* put(didFailToWriteUart(action.id, ensureError(err))); - } + // have to spawn to avoid cancellation + yield* spawn(function* () { + try { + yield* call(() => char.writeValueWithoutResponse(action.value.buffer)); + yield* put(didWriteUart(action.id)); + } catch (err) { + yield* put(didFailToWriteUart(action.id, ensureError(err))); + } + }); } function* handleBleConnectPybricks(): Generator {