Skip to content

Commit

Permalink
chore: fire an event upon the completion of the refreshSObjects comma…
Browse files Browse the repository at this point in the history
…nd to notify E4D (#5584)

* chore: add event emitter for refresh sobject command

* fix: fix lint

* chore: rename

* chore: remove static

* test: add tests

* test: update test description

* chore: delete commandEventdispather

* chore: add commandEventDispacther back

* chore: update imports reference

* test: update nits

---------

Co-authored-by: Mingxuan Zhang <132491513+mingxuanzhangsfdx@users.noreply.github.com>
Co-authored-by: gbockus-sf <76090802+gbockus-sf@users.noreply.github.com>
  • Loading branch information
3 people committed May 10, 2024
1 parent 96a1954 commit 91eac1a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export class SObjectRefreshGatherer
}

export class RefreshSObjectsExecutor extends SfCommandletExecutor<{}> {
public static readonly refreshSObjectsCommandCompletionEventEmitter =
new vscode.EventEmitter();
public static readonly onRefreshSObjectsCommandCompletion =
RefreshSObjectsExecutor.refreshSObjectsCommandCompletionEventEmitter.event;
private static isActive = false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public build(data: {}): Command {
Expand Down Expand Up @@ -167,6 +171,11 @@ export class RefreshSObjectsExecutor extends SfCommandletExecutor<{}> {
customObjects: result.data.customObjects ?? 0
}
);
RefreshSObjectsExecutor.refreshSObjectsCommandCompletionEventEmitter.fire(
{
exitCode: LocalCommandExecution.SUCCESS_CODE
}
);
} catch (error) {
console.log('Generate error ' + error.error);
telemetryService.sendException(error.name, error.error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, 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 * as vscode from 'vscode';
import { RefreshSObjectsExecutor } from '..';

export class CommandEventDispatcher implements vscode.Disposable {
protected static instance: CommandEventDispatcher;

public static getInstance(): CommandEventDispatcher {
if (!CommandEventDispatcher.instance) {
CommandEventDispatcher.instance = new CommandEventDispatcher();
}
return CommandEventDispatcher.instance;
}

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

public dispose() {
RefreshSObjectsExecutor.refreshSObjectsCommandCompletionEventEmitter.dispose();
}
}
6 changes: 5 additions & 1 deletion packages/salesforcedx-vscode-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ import {
SfCommandletExecutor,
SfWorkspaceChecker
} from './commands/util';

import { CommandEventDispatcher } from './commands/util/commandEventDispatcher';
import {
PersistentStorageService,
registerConflictView,
Expand Down Expand Up @@ -639,6 +641,7 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
const commands = registerCommands(extensionContext);
extensionContext.subscriptions.push(commands);
extensionContext.subscriptions.push(registerConflictView());
extensionContext.subscriptions.push(CommandEventDispatcher.getInstance());

const api: any = {
channelService,
Expand All @@ -663,7 +666,8 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
ChannelService,
SalesforceProjectConfig,
TelemetryService,
WorkspaceContext
WorkspaceContext,
CommandEventDispatcher
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SObjectTransformer,
SObjectTransformerFactory
} from '@salesforce/salesforcedx-sobjects-faux-generator';
import { LocalCommandExecution } from '@salesforce/salesforcedx-utils-vscode';
import { EventEmitter } from 'events';
import * as fs from 'fs';
import { channelService } from '../../../src/channels';
Expand Down Expand Up @@ -46,6 +47,19 @@ describe('RefreshSObjectsExecutor', () => {
expect(channelServiceSpy).not.toHaveBeenCalled();
});

it('should fire the command completion event once the command is finished successfully', async () => {
const fireSpy = jest.spyOn(
RefreshSObjectsExecutor.refreshSObjectsCommandCompletionEventEmitter,
'fire'
);

await doExecute(SObjectRefreshSource.Startup, SObjectCategory.STANDARD);

expect(fireSpy).toHaveBeenCalledWith({
exitCode: LocalCommandExecution.SUCCESS_CODE
});
});

const doExecute = async (
source: SObjectRefreshSource,
category?: SObjectCategory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, 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 * as vscode from 'vscode';
import { RefreshSObjectsExecutor } from '../../../../src/commands';
import { CommandEventDispatcher } from '../../../../src/commands/util/commandEventDispatcher';

describe('CommandEventDispatcher', () => {
describe('getInstance', () => {
it('should return the instance of CommandEventDispatcher', () => {
const instance = CommandEventDispatcher.getInstance();

expect(instance).toBeDefined();
});

it('should return the same instance of CommandEventDispatcher', () => {
const instance1 = CommandEventDispatcher.getInstance();
const instance2 = CommandEventDispatcher.getInstance();

expect(instance1).toBe(instance2);
});
});

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

beforeEach(() => {
(RefreshSObjectsExecutor as any).onRefreshSObjectsCommandCompletion = jest
.fn()
.mockReturnValue(mockDisposable);
});

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

expect(disposable).toBe(mockDisposable);
expect(
(RefreshSObjectsExecutor as any).onRefreshSObjectsCommandCompletion
).toHaveBeenCalledWith(listener);
});
});

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

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

expect(
(RefreshSObjectsExecutor as any)
.refreshSObjectsCommandCompletionEventEmitter.dispose
).toHaveBeenCalled();
});
});
});

0 comments on commit 91eac1a

Please sign in to comment.