Skip to content

Commit

Permalink
Add retry logic for listing IoT Hub to work around showQuickPick issue (
Browse files Browse the repository at this point in the history
#86)

* Add retry logic for listing IoT Hub to work around showQuickPick issue of VS Code

* Update threshold to 500 ms and add comments to explain the retry logic
  • Loading branch information
formulahendry authored Apr 28, 2018
1 parent ebd8dbf commit 74bc40d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/iotHubResourceExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ export class IoTHubResourceExplorer extends BaseExplorer {
TelemetryClient.sendEvent("General.Select.Subscription.Done");
outputChannel.show();
outputChannel.appendLine(`Subscription selected: ${subscriptionItem.label}`);
const iotHubItems = this.loadIoTHubItems(subscriptionItem);
const iotHubItem = await vscode.window.showQuickPick(iotHubItems, { placeHolder: "Select IoT Hub", ignoreFocusOut: true });
const iotHubItem = await this.selectIoTHubItem(subscriptionItem);
if (iotHubItem) {
outputChannel.appendLine(`IoT Hub selected: ${iotHubItem.label}`);
const iotHubConnectionString = await this.getIoTHubConnectionString(subscriptionItem, iotHubItem.iotHubDescription);
Expand Down Expand Up @@ -195,6 +194,26 @@ export class IoTHubResourceExplorer extends BaseExplorer {
return iotHubItems;
}

private async selectIoTHubItem(subscriptionItem: SubscriptionItem): Promise<IotHubItem> {
const iotHubItems = this.loadIoTHubItems(subscriptionItem);
let retryCount = 3;
let iotHubItem;
// Sometimes showQuickPick of VS Code would return 'undefined' and disappear immediately without waiting for Promise is resolved
// Workaround here to add retry logic
do {
const start = new Date().getTime();
iotHubItem = await vscode.window.showQuickPick(iotHubItems, { placeHolder: "Select IoT Hub", ignoreFocusOut: true });
// If user press 'Esc', the showQuickPick would also return 'undefined'
// So we will retry only if showQuickPick retrun 'undefined' within 500 ms (Mostly not triggered by user pressing 'Esc')
if (iotHubItem === undefined && (new Date().getTime() - start < 500)) {
retryCount--;
} else {
return iotHubItem;
}
} while (retryCount > 0)
return iotHubItem;
}

private async updateIoTHubConnectionString(iotHubConnectionString: string) {
const config = Utility.getConfiguration();
await config.update(Constants.IotHubConnectionStringKey, iotHubConnectionString, true);
Expand Down

0 comments on commit 74bc40d

Please sign in to comment.