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. */