Skip to content

Commit

Permalink
Fix loading message
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Oct 27, 2023
1 parent 79385fa commit 9702b2f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { fileUtils } from 'roku-debug';
import { util } from './util';
import type { TelemetryManager } from './managers/TelemetryManager';
import type { ActiveDeviceManager, RokuDeviceDetails } from './ActiveDeviceManager';
import { debounce } from 'debounce';

export class BrightScriptDebugConfigurationProvider implements DebugConfigurationProvider {

Expand Down Expand Up @@ -437,6 +438,7 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
*/
private async promptForHost() {
const deferred = new Deferred<string>();
const disposables: Array<() => any> = [];

const discoveryTime = 5_000;
const manualLabel = 'Enter manually';
Expand All @@ -454,8 +456,11 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
//create a text-based spinner factory for use in the "loading ..." label
const generateSpinnerText = util.createTextSpinner(3);

const refreshList = () => {
let itemsRefreshed: Array<QuickPickItem & { device?: RokuDeviceDetails }> = this.activeDeviceManager.getActiveDevices().map(device => ({
const refreshListDebounced = debounce(() => refreshList(true), 400);

const refreshList = (updateSpinnerText = false) => {
const devices = this.activeDeviceManager.getActiveDevices();
let itemsRefreshed: Array<QuickPickItem & { device?: RokuDeviceDetails }> = devices.map(device => ({
label: `${device.ip} | ${device.deviceInfo['user-device-name']} - ${device.deviceInfo['serial-number']} - ${device.deviceInfo['model-number']}`,
device: device
}));
Expand All @@ -479,15 +484,13 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
}

if (this.activeDeviceManager.timeSinceLastDiscoveredDevice < discoveryTime) {
devicesLabel.label += ` (searching ${generateSpinnerText()})`;
setTimeout(() => {
refreshList();
}, 500);
devicesLabel.label += ` (searching ${generateSpinnerText(updateSpinnerText)})`;
refreshListDebounced();
}

// allow user to manually type an IP address
itemsRefreshed.push(
{ label: '', kind: vscode.QuickPickItemKind.Separator },
{ label: ' ', kind: vscode.QuickPickItemKind.Separator },
{ label: manualLabel, device: { id: Number.MAX_SAFE_INTEGER } } as any
);

Expand All @@ -503,8 +506,11 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
};

//anytime the device picker adds/removes a device, update the list
this.activeDeviceManager.on('device-found', refreshList);
this.activeDeviceManager.on('device-expire', refreshList);
disposables.push(
this.activeDeviceManager.on('device-found', () => refreshList()),
this.activeDeviceManager.on('device-expire', () => refreshList())
);

quickPick.onDidHide(() => {
deferred.reject(new Error('No host was selected'));
quickPick.dispose();
Expand All @@ -531,6 +537,9 @@ export class BrightScriptDebugConfigurationProvider implements DebugConfiguratio
refreshList();
const result = await deferred.promise;
quickPick.dispose();
for (const disposable of disposables) {
disposable();
}
return result;
}

Expand Down
22 changes: 22 additions & 0 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,27 @@ describe('Util', () => {
expect(spin()).to.eql('-*-');
expect(spin()).to.eql('*--');
});

it('returns same value when passed false', () => {
const spin = util.createTextSpinner(3, '-', '*');
expect(spin()).to.eql('--*');
expect(spin(false)).to.eql('--*');

expect(spin()).to.eql('-*-');
expect(spin(false)).to.eql('-*-');

expect(spin()).to.eql('*--');
expect(spin(false)).to.eql('*--');

expect(spin()).to.eql('--*');
expect(spin(false)).to.eql('--*');

expect(spin()).to.eql('-*-');
expect(spin(false)).to.eql('-*-');

expect(spin()).to.eql('*--');
expect(spin(false)).to.eql('*--');

});
});
});
10 changes: 7 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,18 @@ class Util {
* @returns function that generates loading strings
*/
public createTextSpinner(max: number, offChar = '◦', onChar = '•') {
let current = 0;
let current = 2;
const fullText = offChar.repeat(max).split('');
/**
* Generate the next text
* @param increment if false, will return the same value as last time
*/
return function spinner() {
return function spinner(increment = true) {
const text = [...fullText];
text[current++ % max] = onChar;
if (increment) {
current++;
}
text[current % max] = onChar;
return text.reverse().join('');
};
}
Expand Down

0 comments on commit 9702b2f

Please sign in to comment.