-
Notifications
You must be signed in to change notification settings - Fork 29.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230046 from microsoft/tyriar/addon_loader_class
Encapsulate xterm addon importing into own class
- Loading branch information
Showing
6 changed files
with
77 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/vs/workbench/contrib/terminal/browser/xterm/xtermAddonImporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import type { ClipboardAddon as ClipboardAddonType } from '@xterm/addon-clipboard'; | ||
import type { ImageAddon as ImageAddonType } from '@xterm/addon-image'; | ||
import type { SearchAddon as SearchAddonType } from '@xterm/addon-search'; | ||
import type { SerializeAddon as SerializeAddonType } from '@xterm/addon-serialize'; | ||
import type { Unicode11Addon as Unicode11AddonType } from '@xterm/addon-unicode11'; | ||
import type { WebglAddon as WebglAddonType } from '@xterm/addon-webgl'; | ||
import { importAMDNodeModule } from '../../../../../amdX.js'; | ||
|
||
export interface IXtermAddonNameToCtor { | ||
clipboard: typeof ClipboardAddonType; | ||
image: typeof ImageAddonType; | ||
search: typeof SearchAddonType; | ||
serialize: typeof SerializeAddonType; | ||
unicode11: typeof Unicode11AddonType; | ||
webgl: typeof WebglAddonType; | ||
} | ||
|
||
// This interface lets a maps key and value be linked with generics | ||
interface IImportedXtermAddonMap extends Map<keyof IXtermAddonNameToCtor, IXtermAddonNameToCtor[keyof IXtermAddonNameToCtor]> { | ||
get<K extends keyof IXtermAddonNameToCtor>(name: K): IXtermAddonNameToCtor[K] | undefined; | ||
set<K extends keyof IXtermAddonNameToCtor>(name: K, value: IXtermAddonNameToCtor[K]): this; | ||
} | ||
|
||
const importedAddons: IImportedXtermAddonMap = new Map(); | ||
|
||
/** | ||
* Exposes a simple interface to consumers, encapsulating the messy import xterm | ||
* addon import and caching logic. | ||
*/ | ||
export class XtermAddonImporter { | ||
async importAddon<T extends keyof IXtermAddonNameToCtor>(name: T): Promise<IXtermAddonNameToCtor[T]> { | ||
let addon = importedAddons.get(name); | ||
if (!addon) { | ||
switch (name) { | ||
case 'clipboard': addon = (await importAMDNodeModule<typeof import('@xterm/addon-clipboard')>('@xterm/addon-clipboard', 'lib/addon-clipboard.js')).ClipboardAddon as IXtermAddonNameToCtor[T]; break; | ||
case 'image': addon = (await importAMDNodeModule<typeof import('@xterm/addon-image')>('@xterm/addon-image', 'lib/addon-image.js')).ImageAddon as IXtermAddonNameToCtor[T]; break; | ||
case 'search': addon = (await importAMDNodeModule<typeof import('@xterm/addon-search')>('@xterm/addon-search', 'lib/addon-search.js')).SearchAddon as IXtermAddonNameToCtor[T]; break; | ||
case 'serialize': addon = (await importAMDNodeModule<typeof import('@xterm/addon-serialize')>('@xterm/addon-serialize', 'lib/addon-serialize.js')).SerializeAddon as IXtermAddonNameToCtor[T]; break; | ||
case 'unicode11': addon = (await importAMDNodeModule<typeof import('@xterm/addon-unicode11')>('@xterm/addon-unicode11', 'lib/addon-unicode11.js')).Unicode11Addon as IXtermAddonNameToCtor[T]; break; | ||
case 'webgl': addon = (await importAMDNodeModule<typeof import('@xterm/addon-webgl')>('@xterm/addon-webgl', 'lib/addon-webgl.js')).WebglAddon as IXtermAddonNameToCtor[T]; break; | ||
} | ||
if (!addon) { | ||
throw new Error(`Could not load addon ${name}`); | ||
} | ||
importedAddons.set(name, addon); | ||
} | ||
return addon as IXtermAddonNameToCtor[T]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters