Skip to content

Commit 7fbe9ec

Browse files
feat: add isServiceAvailable method
1 parent f8b1427 commit 7fbe9ec

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/services/serviceProvider.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ export class ServiceProvider {
2525
Map<string, ServiceReturnType<ServiceType>>
2626
> = new Map();
2727

28+
/**
29+
* Checks if the service is available to be created and returned from the {@link getService} method.
30+
* Specifically, this method checks if there even is a command associated with the specified
31+
* @{link ServiceType} registered within VS Code.
32+
* @param type - The type of the service.
33+
* @returns The value true if the service is available and false otherwise.
34+
*/
35+
static async isServiceAvailable<T extends ServiceType>(
36+
type: T
37+
): Promise<boolean> {
38+
return (await this.getCommands()).includes(this.getCommandString(type));
39+
}
40+
2841
/**
2942
* Retrieves a service instance of the specified type and instance name.
3043
* If the service instance does not exist, it will be created.
@@ -99,7 +112,7 @@ export class ServiceProvider {
99112
}
100113

101114
/**
102-
* Checks if a service of the specified type exists.
115+
* Checks if a service of the specified type exists yet within the cache of the ServiceProvider.
103116
* @param type - The type of the service.
104117
* @returns True if the service exists, false otherwise.
105118
*/
@@ -108,7 +121,7 @@ export class ServiceProvider {
108121
}
109122

110123
/**
111-
* Checks if a service instance of the specified type and instance name exists.
124+
* Checks if a service instance of the specified type and instance name exists yet within the cache of the ServiceProvider.
112125
* @param type - The type of the service.
113126
* @param instanceName - The name of the service instance.
114127
* @returns True if the service instance exists, false otherwise.

test/services/serviceProvider.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ describe('ServiceProvider', () => {
110110
});
111111
});
112112

113+
it('should correctly identify that a service is available if the associated command is registered', async () => {
114+
const isAvailable = await ServiceProvider.isServiceAvailable(
115+
ServiceType.Telemetry
116+
);
117+
expect(isAvailable).toBe(true);
118+
});
119+
120+
it('should correctly identify that a service is not available if the associated command is not registered', async () => {
121+
jest
122+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
123+
.spyOn(ServiceProvider as any, 'getCommands')
124+
.mockImplementation(() => {
125+
return Promise.resolve([loggerCommand]);
126+
});
127+
const isAvailable = await ServiceProvider.isServiceAvailable(
128+
ServiceType.Telemetry
129+
);
130+
expect(isAvailable).toBe(false);
131+
});
132+
113133
it('should get a service', async () => {
114134
(vscode.commands.executeCommand as jest.Mock).mockResolvedValue(
115135
'mockService'

0 commit comments

Comments
 (0)