Skip to content

Commit

Permalink
Add a "refresh" button in the Devices panel
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed May 13, 2022
1 parent e175123 commit f8adff5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion package.json
Expand Up @@ -81,6 +81,7 @@
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@tsconfig/svelte": "^1.0.10",
"@types/backoff": "^2.5.2",
"@types/chai": "^4.1.5",
"@types/fs-extra": "^5.0.4",
"@types/glob": "^7.1.1",
Expand Down Expand Up @@ -173,7 +174,7 @@
"views": {
"vscode-brightscript-language": [
{
"id": "onlineDevices",
"id": "onlineDevicesView",
"name": "Devices"
}
],
Expand Down Expand Up @@ -220,6 +221,11 @@
"command": "extension.brightscript.rendezvous.clearHistory",
"when": "debugType == 'brightscript' && view == rendezvousView",
"group": "Rendezvous"
},
{
"command": "extension.brightscript.refreshDeviceList",
"when": "view == onlineDevicesView",
"group": "navigation"
}
],
"commandPalette": []
Expand Down Expand Up @@ -1928,6 +1934,12 @@
"dark": "./images/icons/preview-right-dark.svg"
}
},
{
"command": "extension.brightscript.refreshDeviceList",
"title": "Refresh",
"category": "BrighterScript",
"icon": "$(refresh)"
},
{
"command": "extension.brightscript.languageServer.restart",
"title": "Restart Language Server",
Expand Down
48 changes: 33 additions & 15 deletions src/ActiveDeviceManager.ts
Expand Up @@ -29,7 +29,8 @@ export class ActiveDeviceManager extends EventEmitter {
});

this.deviceCache = new NodeCache({ stdTTL: 3600, checkperiod: 120 });
this.deviceCache.on('expired', (deviceId, device) => {
//anytime a device leaves the cache (either expired or manually deleted)
this.deviceCache.on('del', (deviceId, device) => {
this.emit('expiredDevice', deviceId, device);
});
this.processEnabledState();
Expand All @@ -54,10 +55,18 @@ export class ActiveDeviceManager extends EventEmitter {
return this.deviceCache.getStats();
}

/**
* Clear the list and re-scan the whole network for devices
*/
public refresh() {
this.stop();
this.start();
}

// Will ether stop or start the watching process based on the running state and user settings
private processEnabledState() {
if (this.enabled && !this.isRunning) {
this.findDevices();
this.start();
} else if (!this.enabled && this.isRunning) {
this.stop();
}
Expand All @@ -68,25 +77,34 @@ export class ActiveDeviceManager extends EventEmitter {
this.exponentialBackoff.reset();
}

this.deviceCache.del(
this.deviceCache.keys()
);
this.deviceCache.flushAll();
this.isRunning = false;
}

// Begin searching and watching for devices
private findDevices() {
this.exponentialBackoff = backoff.exponential({
randomisationFactor: 0,
initialDelay: 1000,
maxDelay: 30000
});
/**
* Begin searching and watching for devices
*/
private start() {
if (!this.isRunning) {
this.exponentialBackoff = backoff.exponential({
randomisationFactor: 0,
initialDelay: 2000,
maxDelay: 60000
});

this.exponentialBackoff.on('ready', (eventNumber, delay) => {
void this.discoverAll(delay);
this.exponentialBackoff.backoff();
});
void this.discoverAll(1000);

this.exponentialBackoff.on('ready', (eventNumber, delay) => {
void this.discoverAll(delay);
this.exponentialBackoff.backoff();
});

this.exponentialBackoff.backoff();
this.isRunning = true;
this.exponentialBackoff.backoff();
this.isRunning = true;
}
}

// Discover all Roku devices on the network and watch for new ones that connect
Expand Down
2 changes: 1 addition & 1 deletion src/BrightScriptCommands.spec.ts
Expand Up @@ -22,7 +22,7 @@ describe('BrightScriptFileUtils ', () => {
let languagesMock;

beforeEach(() => {
commands = new BrightScriptCommands({} as any, {} as any, {} as any);
commands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any);
commandsMock = sinon.mock(commands);
languagesMock = sinon.mock(vscode.languages);
});
Expand Down
9 changes: 8 additions & 1 deletion src/BrightScriptCommands.ts
Expand Up @@ -8,13 +8,15 @@ import { util } from './util';
import { util as rokuDebugUtil } from 'roku-debug/dist/util';
import type { RemoteControlManager, RemoteControlModeInitiator } from './managers/RemoteControlManager';
import type { WhatsNewManager } from './managers/WhatsNewManager';
import type { ActiveDeviceManager } from './ActiveDeviceManager';

export class BrightScriptCommands {

constructor(
private remoteControlManager: RemoteControlManager,
private whatsNewManager: WhatsNewManager,
private context: vscode.ExtensionContext
private context: vscode.ExtensionContext,
private activeDeviceManager: ActiveDeviceManager
) {
this.fileUtils = new BrightScriptFileUtils();
}
Expand All @@ -33,6 +35,11 @@ export class BrightScriptCommands {
await this.sendRemoteCommand(key);
});

//the "Refresh" button in the Devices list
this.registerCommand('refreshDeviceList', (key: string) => {
this.activeDeviceManager.refresh();
});

this.registerCommand('sendRemoteText', async () => {
let items: vscode.QuickPickItem[] = [];
for (const item of new GlobalStateManager(this.context).sendRemoteTextHistory) {
Expand Down
11 changes: 8 additions & 3 deletions src/extension.ts
Expand Up @@ -72,14 +72,19 @@ export class Extension {
);

this.telemetryManager.sendStartupEvent();
let activeDeviceManager = new ActiveDeviceManager();

this.remoteControlManager = new RemoteControlManager(this.telemetryManager);
this.brightScriptCommands = new BrightScriptCommands(this.remoteControlManager, this.whatsNewManager, context);
this.brightScriptCommands = new BrightScriptCommands(
this.remoteControlManager,
this.whatsNewManager,
context,
activeDeviceManager
);

//update the tracked version of the extension
this.globalStateManager.lastRunExtensionVersion = currentExtensionVersion;

let activeDeviceManager = new ActiveDeviceManager();

const declarationProvider = new DeclarationProvider();
context.subscriptions.push(declarationProvider);
Expand All @@ -104,7 +109,7 @@ export class Extension {

//register a tree data provider for this extension's "Online Devices" panel
let onlineDevicesViewProvider = new OnlineDevicesViewProvider(context, activeDeviceManager);
vscode.window.registerTreeDataProvider('onlineDevices', onlineDevicesViewProvider);
vscode.window.registerTreeDataProvider('onlineDevicesView', onlineDevicesViewProvider);

context.subscriptions.push(vscode.commands.registerCommand('extension.brightscript.rendezvous.clearHistory', async () => {
await vscode.debug.activeDebugSession.customRequest('rendezvous.clearHistory');
Expand Down

0 comments on commit f8adff5

Please sign in to comment.