From 120ef51c82c9c6a0d8cc3703247f3c8e31d9c715 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozlov Date: Sun, 29 Mar 2020 22:51:57 +0300 Subject: [PATCH] $EDIX-4591: return unsubscribe method --- src/API.ts | 6 +++++- src/extensionSlot.ts | 17 +++++++++++++---- src/index.ts | 3 ++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/API.ts b/src/API.ts index b23b357c..d8f906ae 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'