Skip to content

Commit

Permalink
Moved to more generic ExtendedMap class (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jul 8, 2024
1 parent 9d14824 commit 40547b4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 46 deletions.
7 changes: 3 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import * as vscode from 'vscode';
import {
ConnectionOption,
ExtendedMap,
createConnectStatusBarItem,
createConnectTextAndTooltip,
createConnectionOptions,
createConnectionQuickPick,
getTempDir,
} from './util';
import { DhcService, PanelRegistry } from './services';
import { DhcService } from './services';
import { DhServiceRegistry } from './services';
import {
RUN_CODE_COMMAND,
RUN_SELECTION_COMMAND,
SELECT_CONNECTION_COMMAND,
STATUS_BAR_CONNECTING_TEXT,
STATUS_BAR_DISCONNECTED_TEXT,
} from './common';

export function activate(context: vscode.ExtensionContext) {
Expand All @@ -41,7 +40,7 @@ export function activate(context: vscode.ExtensionContext) {

const dhcServiceRegistry = new DhServiceRegistry(
DhcService,
new PanelRegistry(),
new ExtendedMap<string, vscode.WebviewPanel>(),
outputChannel
);

Expand Down
14 changes: 7 additions & 7 deletions src/services/DhService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import * as vscode from 'vscode';
import type { dh as DhcType } from '../dh/dhc-types';
import { hasErrorCode } from '../util/typeUtils';
import { ConnectionAndSession } from '../common';
import { formatTimestamp } from '../util';
import { PanelFocusManager } from './PanelFocusManager';
import { ExtendedMap, formatTimestamp } from '../util';
import { EventDispatcher } from './EventDispatcher';
import { PanelRegistry } from './PanelRegistry';

/* eslint-disable @typescript-eslint/naming-convention */
const icons = {
Expand Down Expand Up @@ -34,7 +32,7 @@ export abstract class DhService<
> extends EventDispatcher<'disconnect'> {
constructor(
serverUrl: string,
panelRegistry: PanelRegistry,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
outputChannel: vscode.OutputChannel
) {
super();
Expand All @@ -48,7 +46,7 @@ export abstract class DhService<
protected readonly subscriptions: (() => void)[] = [];

protected outputChannel: vscode.OutputChannel;
private panelRegistry: PanelRegistry;
private panelRegistry: ExtendedMap<string, vscode.WebviewPanel>;
private cachedCreateClient: Promise<TClient> | null = null;
private cachedCreateSession: Promise<ConnectionAndSession<
DhcType.IdeConnection,
Expand Down Expand Up @@ -289,7 +287,7 @@ export abstract class DhService<
// );
}

const panel = this.panelRegistry.get(title)!;
const panel = this.panelRegistry.getOrThrow(title);
lastPanel = panel;

// See @deprecated comment in PanelFocusManager.onDidChangeViewState
Expand All @@ -305,7 +303,9 @@ export abstract class DhService<
data,
this.panelRegistry
.get(title)!
.webview.postMessage.bind(this.panelRegistry.get(title)!.webview)
.webview.postMessage.bind(
this.panelRegistry.getOrThrow(title).webview
)
);
});

Expand Down
5 changes: 2 additions & 3 deletions src/services/DhServiceRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import * as vscode from 'vscode';
import { CacheService } from './CacheService';
import { DhcService, DhcServiceConstructor } from './DhcService';
import { ensureHasTrailingSlash } from '../util';
import { PanelRegistry } from './PanelRegistry';
import { ensureHasTrailingSlash, ExtendedMap } from '../util';

export class DhServiceRegistry<T extends DhcService> extends CacheService<
T,
'disconnect'
> {
constructor(
serviceFactory: DhcServiceConstructor<T>,
panelRegistry: PanelRegistry,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
outputChannel: vscode.OutputChannel
) {
super(
Expand Down
5 changes: 2 additions & 3 deletions src/services/DhcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
initDhcApi,
initDhcSession,
} from '../dh/dhc';
import { getPanelHtml } from '../util';
import { ExtendedMap, getPanelHtml } from '../util';
import { ConnectionAndSession } from '../common';
import { PanelRegistry } from './PanelRegistry';

export type DhcServiceConstructor<T extends DhcService> = new (
serverUrl: string,
panelRegistry: PanelRegistry,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
outputChannel: vscode.OutputChannel
) => T;

Expand Down
28 changes: 0 additions & 28 deletions src/services/PanelRegistry.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './Config';
export * from './DhService';
export * from './DhcService';
export * from './DhServiceRegistry';
export * from './PanelRegistry';
17 changes: 17 additions & 0 deletions src/util/ExtendedMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* ExtendedMap is a Map with additional utility methods.
*/
export class ExtendedMap<K, V> extends Map<K, V> {
/**
* Return the value for the key if it exists, otherwise throw an error.
* @param key
* @returns
*/
getOrThrow = (key: K): V => {
if (!this.has(key)) {
throw new Error(`Key not found: ${key}`);
}

return this.get(key)!;
};
}
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './downloadUtils';
export * from './ExtendedMap';
export * from './panelUtils';
export * from './polyfillUtils';
export * from './uiUtils';
Expand Down

0 comments on commit 40547b4

Please sign in to comment.