|
29 | 29 | import BluetoothConnectingDialog from './bluetooth/BluetoothConnectingDialog.svelte'; |
30 | 30 | import SelectMicrobitDialogBluetooth from './bluetooth/SelectMicrobitDialogBluetooth.svelte'; |
31 | 31 | import MicrobitWearingInstructionDialog from './MicrobitWearingInstructionDialog.svelte'; |
32 | | - import WebUsbTryAgain from './WebUsbTryAgain.svelte'; |
| 32 | + import WebUsbTryAgain, { USBTryAgainType } from './WebUsbTryAgain.svelte'; |
33 | 33 | import { onDestroy, onMount } from 'svelte'; |
34 | 34 | import { get, Unsubscriber } from 'svelte/store'; |
35 | 35 | import { compatibility } from '../../script/stores/uiStore'; |
|
40 | 40 | const { bluetooth, usb } = get(compatibility); |
41 | 41 | let endOfFlow = false; |
42 | 42 | let flashStage: FlashStage = usb ? 'bluetooth' : 'radio-sender'; |
43 | | - let reconnectRequired = false; |
| 43 | + let usbTryAgainType: USBTryAgainType = 'replug microbit'; |
44 | 44 | let flashProgress = 0; |
45 | 45 |
|
46 | 46 | const stageToHex = (flashStage: FlashStage): HexType => { |
|
72 | 72 | break; |
73 | 73 | } else if (/No device selected/.test(err.message)) { |
74 | 74 | $connectionDialogState.connectionState = ConnectDialogStates.USB_TRY_AGAIN; |
75 | | -
|
| 75 | + usbTryAgainType = 'select microbit'; |
| 76 | + break; |
| 77 | + } else if (/Unable to claim interface/.test(err.message)) { |
| 78 | + $connectionDialogState.connectionState = ConnectDialogStates.USB_TRY_AGAIN; |
| 79 | + usbTryAgainType = 'close tabs'; |
76 | 80 | break; |
77 | 81 | } else { |
78 | 82 | // Unhandled error. User will need to reconnect their micro:bit |
79 | 83 | $connectionDialogState.connectionState = ConnectDialogStates.USB_TRY_AGAIN; |
80 | | - reconnectRequired = true; |
| 84 | + usbTryAgainType = 'replug microbit'; |
81 | 85 | break; |
82 | 86 | } |
83 | 87 | default: { |
84 | 88 | $connectionDialogState.connectionState = ConnectDialogStates.USB_TRY_AGAIN; |
85 | | - reconnectRequired = true; |
| 89 | + usbTryAgainType = 'replug microbit'; |
86 | 90 | } |
87 | 91 | } |
88 | 92 | }; |
89 | 93 |
|
| 94 | + const handleConnectionError = (err: any) => { |
| 95 | + if (flashStage === 'bluetooth') { |
| 96 | + $connectionDialogState.connectionState = ConnectDialogStates.MANUAL_TUTORIAL; |
| 97 | + } else { |
| 98 | + handleWebUSBError(err); |
| 99 | + } |
| 100 | + }; |
| 101 | +
|
90 | 102 | async function tryMicrobitConnection(): Promise<void> { |
91 | 103 | try { |
92 | 104 | await Microbits.linkMicrobit(); |
93 | 105 | } catch (err) { |
94 | | - if (flashStage === 'bluetooth') { |
95 | | - $connectionDialogState.connectionState = ConnectDialogStates.MANUAL_TUTORIAL; |
96 | | - } else { |
97 | | - $connectionDialogState.connectionState = ConnectDialogStates.USB_TRY_AGAIN; |
98 | | - } |
| 106 | + handleConnectionError(err); |
99 | 107 | return; |
100 | 108 | } |
101 | 109 | const friendlyName = await getMicrobitName(); |
|
116 | 124 | } |
117 | 125 | return friendlyName; |
118 | 126 | } catch (err) { |
119 | | - if (flashStage === 'bluetooth') { |
120 | | - $connectionDialogState.connectionState = ConnectDialogStates.MANUAL_TUTORIAL; |
121 | | - } else { |
122 | | - handleWebUSBError(err); |
123 | | - } |
| 127 | + handleConnectionError(err); |
124 | 128 | } |
125 | 129 | } |
126 | 130 |
|
|
144 | 148 | onConnectingSerial(friendlyName); |
145 | 149 | } |
146 | 150 | } catch (err) { |
147 | | - if (flashStage === 'bluetooth') { |
148 | | - $connectionDialogState.connectionState = ConnectDialogStates.MANUAL_TUTORIAL; |
149 | | - } else { |
150 | | - handleWebUSBError(err); |
151 | | - } |
| 151 | + handleConnectionError(err); |
152 | 152 | } |
153 | 153 | } |
154 | 154 |
|
|
169 | 169 | setTimeout(() => { |
170 | 170 | $connectionDialogState.connectionState = ConnectDialogStates.NONE; |
171 | 171 | endOfFlow = false; |
172 | | - reconnectRequired = false; |
173 | 172 | }, 200); |
174 | 173 | } |
175 | 174 |
|
|
381 | 380 | ConnectDialogStates.CONNECT_BATTERY)} /> |
382 | 381 | {:else if $connectionDialogState.connectionState === ConnectDialogStates.USB_TRY_AGAIN} |
383 | 382 | <WebUsbTryAgain |
384 | | - {reconnectRequired} |
| 383 | + type={usbTryAgainType} |
385 | 384 | onCancel={endFlow} |
386 | 385 | onTryAgain={() => { |
387 | 386 | $connectionDialogState.connectionState = ConnectDialogStates.CONNECT_CABLE; |
388 | | - reconnectRequired = false; |
389 | 387 | }} /> |
390 | 388 | {/if} |
391 | 389 | </StandardDialog> |
|
0 commit comments