Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add eventEmitter on libraryCommand #5768

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export abstract class LibraryCommandletExecutor<T>
protected showChannelOutput = true;
protected readonly telemetry = new TelemetryBuilder();

public static readonly libraryCommandCompletionEventEmitter = new vscode.EventEmitter<boolean>();
public static readonly onLibraryCommandCompletion: vscode.Event<boolean> =
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter.event;

/**
* @param executionName Name visible to user while executing.
* @param logName Name for logging purposes such as telemetry.
Expand Down Expand Up @@ -220,13 +224,15 @@ export abstract class LibraryCommandletExecutor<T>
properties,
measurements
);
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter.fire(!!success);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it is meaningful. LibraryCommandletExecutor is an abstract class to be extended by other real executors such as orgLogoutDefault or orgLoginAccessToken. How does the subscriber know the result from which command it is listening to with only a boolean value? Can you help me understand that? @angelamuliu @peternhale

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch @mingxuanzhangsfdx, there is no way to know. If two commands are run concurrently and are listening for the completion event, knowing which event belongs any given listener is indeterminant.

} catch (e) {
if (e instanceof Error) {
telemetryService.sendException(e.name, e.message);
notificationService.showFailedExecution(this.executionName);
channelService.appendLine(e.message);
}
channelService.showChannelOutput();
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter.fire(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Progress, CancellationToken } from 'vscode';
import * as vscode from 'vscode';
import { ContinueResponse, LibraryCommandletExecutor } from '../../../src';
import { ChannelService } from '../../../src/commands/channelService';
import { SettingsService } from '../../../src/settings';

class SimpleTestLibraryCommandletExecutor<T> extends LibraryCommandletExecutor<T> {
public run(
response: ContinueResponse<T>,
progress?: Progress<{ message?: string | undefined; increment?: number | undefined }>,
token?: CancellationToken): Promise<boolean> {
return new Promise(resolve => {
resolve(true);
});
}
}
jest.mock('../../../src/commands/channelService');

describe('LibraryCommandletExecutor', () => {

it('should fire the onLibraryCommandCompletion event once the library command is done', async () => {
const fireSpy = jest.spyOn(
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter,
'fire'
);
const channel = vscode.window.createOutputChannel('simpleExecutorChannel');
const executor = new SimpleTestLibraryCommandletExecutor('simpleExecutor', 'logName', channel);

await executor.execute({} as ContinueResponse<{}>);

expect(fireSpy).toHaveBeenCalledWith(false);
});

it('should not fire onLibraryCommandCompletion event if an error is thrown before try catch block', async () => {
const fireSpy = jest.spyOn(
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter,
'fire'
);
try {
jest
.spyOn(SettingsService, 'getEnableClearOutputBeforeEachCommand')
.mockImplementation(() => {
throw new Error();
});
const channel = vscode.window.createOutputChannel('simpleExecutorChannel');
const executor = new SimpleTestLibraryCommandletExecutor('simpleExecutor', 'logName', channel);

await executor.execute({} as ContinueResponse<{}>);
} catch(e) {
expect(fireSpy).not.toHaveBeenCalled();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { LibraryCommandletExecutor } from '@salesforce/salesforcedx-utils-vscode';
import * as vscode from 'vscode';
import { RefreshSObjectsExecutor } from '..';

Expand All @@ -23,7 +24,14 @@ export class CommandEventDispatcher implements vscode.Disposable {
return RefreshSObjectsExecutor.onRefreshSObjectsCommandCompletion(listener);
}

public onLibraryCommandCompletion(
listener: (event: unknown) => unknown
): vscode.Disposable {
return LibraryCommandletExecutor.onLibraryCommandCompletion(listener);
}

public dispose() {
RefreshSObjectsExecutor.refreshSObjectsCommandCompletionEventEmitter.dispose();
LibraryCommandletExecutor.libraryCommandCompletionEventEmitter.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { LibraryCommandletExecutor } from '@salesforce/salesforcedx-utils-vscode';
import * as vscode from 'vscode';
import { RefreshSObjectsExecutor } from '../../../../src/commands';
import { CommandEventDispatcher } from '../../../../src/commands/util/commandEventDispatcher';
Expand Down Expand Up @@ -46,11 +47,31 @@ describe('CommandEventDispatcher', () => {
});
});

describe('onLibraryCommandCompletion', () => {
const mockDisposable = new vscode.Disposable(() => {});

beforeEach(() => {
(LibraryCommandletExecutor as any).onLibraryCommandCompletion = jest
.fn()
.mockReturnValue(mockDisposable);
});

it('should call onLibraryCommandCompletion event and return the disposable', () => {
const dispatcher = CommandEventDispatcher.getInstance();
const listener = () => {};
const disposable = dispatcher.onLibraryCommandCompletion(listener);

expect(disposable).toBe(mockDisposable);
expect((LibraryCommandletExecutor as any).onLibraryCommandCompletion).toHaveBeenCalledWith(listener);
});
});

describe('dispose', () => {
beforeEach(() => {
(
RefreshSObjectsExecutor as any
).refreshSObjectsCommandCompletionEventEmitter = { dispose: jest.fn() };
(LibraryCommandletExecutor as any).libraryCommandCompletionEventEmitter = { dispose: jest.fn() };
});

it('should dispose the refreshSObjectsCommandCompletionEventEmitter', () => {
Expand All @@ -62,5 +83,13 @@ describe('CommandEventDispatcher', () => {
.refreshSObjectsCommandCompletionEventEmitter.dispose
).toHaveBeenCalled();
});

it('should dispose the libraryCommandCompletionEventEmitter', () => {
const dispatcher = CommandEventDispatcher.getInstance();
dispatcher.dispose();

expect(
(LibraryCommandletExecutor as any).libraryCommandCompletionEventEmitter.dispose).toHaveBeenCalled();
});
});
});
Loading