From 3ef335860f0ade736db8f5cea9e91d60ed0234de Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Thu, 21 Nov 2019 14:00:35 +0100 Subject: [PATCH] GH-6598: Exposed all handlers for a command. Closes #6598. Signed-off-by: Akos Kitta --- packages/core/src/common/command.spec.ts | 25 ++++++++++++++++++++++++ packages/core/src/common/command.ts | 11 ++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/core/src/common/command.spec.ts b/packages/core/src/common/command.spec.ts index 192d502ad741e..08aa483d6499e 100644 --- a/packages/core/src/common/command.spec.ts +++ b/packages/core/src/common/command.spec.ts @@ -21,6 +21,8 @@ import * as chai from 'chai'; const expect = chai.expect; let commandRegistry: CommandRegistry; +/* tslint:disable:no-unused-expression */ + describe('Commands', () => { beforeEach(() => { @@ -120,6 +122,25 @@ describe('Commands', () => { commandRegistry.clearCommandHistory(); expect(commandRegistry.recent.length).equal(0); }); + + it('should return with an empty array of handlers if the command is not registered', () => { + expect(commandRegistry.getCommand('missing')).to.be.undefined; + expect(commandRegistry.getAllHandlers('missing')).to.be.empty; + }); + + it('should return with an empty array of handlers if the command has no registered handlers', () => { + commandRegistry.registerCommand({ id: 'id' }); + expect(commandRegistry.getCommand('id')).to.be.not.undefined; + expect(commandRegistry.getAllHandlers('id')).to.be.empty; + }); + + it('should return all handlers including the non active ones', () => { + commandRegistry.registerCommand({ id: 'id' }); + commandRegistry.registerHandler('id', new StubCommandHandler()); + commandRegistry.registerHandler('id', new NeverActiveStubCommandHandler()); + expect(commandRegistry.getAllHandlers('id').length).to.be.equal(2); + }); + }); class EmptyContributionProvider implements ContributionProvider { @@ -141,3 +162,7 @@ class ConcatCommandHandler implements CommandHandler { class StubCommandHandler implements CommandHandler { execute(...args: string[]): undefined { return undefined; } } + +class NeverActiveStubCommandHandler extends StubCommandHandler { + isEnabled(): boolean { return false; } +} diff --git a/packages/core/src/common/command.ts b/packages/core/src/common/command.ts index 2dff471b9f696..3ad6eb3fc2188 100644 --- a/packages/core/src/common/command.ts +++ b/packages/core/src/common/command.ts @@ -136,7 +136,7 @@ export interface CommandService { // tslint:disable-next-line:no-any executeCommand(command: string, ...args: any[]): Promise; /** - * An event is emmited when a command is about to be executed. + * An event is emitted when a command is about to be executed. * * It can be used to install or activate a command handler. */ @@ -348,6 +348,15 @@ export class CommandRegistry implements CommandService { return undefined; } + /** + * Returns with all handlers for the given command. If the command does not have any handlers, + * or the command is not registered, returns an empty array. + */ + getAllHandlers(commandId: string): CommandHandler[] { + const handlers = this._handlers[commandId]; + return handlers ? handlers.slice() : []; + } + /** * Get all registered commands. */