diff --git a/src/API.ts b/src/API.ts index 8d3299a8..52b92d21 100644 --- a/src/API.ts +++ b/src/API.ts @@ -46,12 +46,16 @@ export interface EntryPointOrPackagesMap { [name: string]: EntryPointOrPackage } +export interface Contribution { + unsubscribe(): void +} + export type ExtensionItemFilter = (extensionItem: ExtensionItem) => boolean export interface ExtensionSlot { readonly name: string readonly host: AppHost readonly declaringShell?: Shell - contribute(shell: Shell, item: T, condition?: ContributionPredicate): void + contribute(shell: Shell, item: T, condition?: ContributionPredicate): Contribution getItems(forceAll?: boolean): ExtensionItem[] getSingleItem(): ExtensionItem getItemByName(name: string): ExtensionItem diff --git a/src/extensionSlot.ts b/src/extensionSlot.ts index ea923ecd..392af00a 100644 --- a/src/extensionSlot.ts +++ b/src/extensionSlot.ts @@ -7,7 +7,8 @@ import { Shell, SlotKey, CustomExtensionSlot, - CustomExtensionSlotHandler + CustomExtensionSlotHandler, + Contribution } from './API' import _ from 'lodash' @@ -32,13 +33,21 @@ export function createExtensionSlot(key: SlotKey, host: AppHost, declaring discardBy } - function contribute(fromShell: Shell, item: T, condition?: ContributionPredicate): void { - items.push({ + function contribute(fromShell: Shell, item: T, condition?: ContributionPredicate): Contribution { + const contribution = { shell: fromShell, contribution: item, condition: condition || alwaysTrue, uniqueId: _.uniqueId(`${fromShell.name}_extItem_`) - }) + } + + items.push(contribution) + + return { + unsubscribe: () => { + items = items.filter(i => i !== contribution) + } + } } function getItems(forceAll: boolean = false): ExtensionItem[] { diff --git a/src/index.ts b/src/index.ts index 6afe3a2d..bdb6291c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,8 @@ export { ShellLoggerSpan, HostLogger, LogSeverity, - APILayer + APILayer, + Contribution } from './API' export { AppMainView } from './appMainView'