Skip to content

Commit 830d5c4

Browse files
committed
Fixes microsoft#34913: Wait for command activation event if command is unknown
1 parent 448eaa2 commit 830d5c4

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

src/vs/platform/commands/common/commandService.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { TPromise } from 'vs/base/common/winjs.base';
88
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
9-
import { ICommandService, ICommand, ICommandEvent, CommandsRegistry } from 'vs/platform/commands/common/commands';
9+
import { ICommandService, ICommandEvent, CommandsRegistry } from 'vs/platform/commands/common/commands';
1010
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
1111
import Event, { Emitter } from 'vs/base/common/event';
1212
import { Disposable } from 'vs/base/common/lifecycle';
@@ -33,17 +33,19 @@ export class CommandService extends Disposable implements ICommandService {
3333
executeCommand<T>(id: string, ...args: any[]): TPromise<T> {
3434
// we always send an activation event, but
3535
// we don't wait for it when the extension
36-
// host didn't yet start
36+
// host didn't yet start and the command is already registered
3737

3838
const activation = this._extensionService.activateByEvent(`onCommand:${id}`);
3939

40-
return this._extensionHostIsReady
41-
? activation.then(_ => this._tryExecuteCommand(id, args))
42-
: this._tryExecuteCommand(id, args);
40+
if (!this._extensionHostIsReady && CommandsRegistry.getCommand(id)) {
41+
return this._tryExecuteCommand(id, args);
42+
} else {
43+
return activation.then(_ => this._tryExecuteCommand(id, args));
44+
}
4345
}
4446

4547
private _tryExecuteCommand(id: string, args: any[]): TPromise<any> {
46-
const command = this._getCommand(id);
48+
const command = CommandsRegistry.getCommand(id);
4749
if (!command) {
4850
return TPromise.wrapError(new Error(`command '${id}' not found`));
4951
}
@@ -61,8 +63,4 @@ export class CommandService extends Disposable implements ICommandService {
6163
return TPromise.wrapError(err);
6264
}
6365
}
64-
65-
private _getCommand(id: string): ICommand {
66-
return CommandsRegistry.getCommand(id);
67-
}
6866
}

src/vs/platform/commands/test/commandService.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,32 @@ suite('CommandService', function () {
101101
}
102102
}, new ContextKeyService(new SimpleConfigurationService()));
103103

104-
return service.executeCommand('bar').then(() => {
104+
service.executeCommand('bar');
105+
assert.equal(callCounter, 1);
106+
reg.dispose();
107+
});
108+
109+
test('issue #34913: !onReady, unknown command', function () {
110+
111+
let callCounter = 0;
112+
let resolveFunc: Function;
113+
// let reg = CommandsRegistry.registerCommand('bar', () => callCounter += 1);
114+
115+
let service = new CommandService(new InstantiationService(), new class extends SimpleExtensionService {
116+
onReady() {
117+
return new TPromise<boolean>(_resolve => { resolveFunc = _resolve; });
118+
}
119+
}, new ContextKeyService(new SimpleConfigurationService()));
120+
121+
let r = service.executeCommand('bar');
122+
assert.equal(callCounter, 0);
123+
124+
let reg = CommandsRegistry.registerCommand('bar', () => callCounter += 1);
125+
resolveFunc(true);
126+
127+
return r.then(() => {
105128
reg.dispose();
106129
assert.equal(callCounter, 1);
107-
108130
});
109131
});
110132

0 commit comments

Comments
 (0)