Skip to content

Commit

Permalink
chore: add docs and remove unneeded type
Browse files Browse the repository at this point in the history
  • Loading branch information
peternhale committed Mar 26, 2024
1 parent 01ceb3d commit b12768f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .git2gus/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type:feedback": "",
"type:duplicate": ""
},
"defaultBuild": "offcore.tooling.56",
"defaultBuild": "offcore.tooling.60",
"hideWorkItemUrl": "true",
"statusWhenClosed": "CLOSED"
}
102 changes: 76 additions & 26 deletions src/services/serviceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@
import { ServiceParams, ServiceReturnType, ServiceType } from '../types';
import * as vscode from 'vscode';

/**
* The ServiceProvider class is a utility class that provides services of different types.
* Each service type can have multiple instances, identified by their instance names.
* @class
*/
export class ServiceProvider {
private static serviceMap: Map<
ServiceType,
Map<string, ServiceReturnType<ServiceType>>
> = new Map();

/**
* Retrieves a service instance of the specified type and instance name.
* If the service instance does not exist, it will be created.
* @param type - The type of the service.
* @param instanceName - The name of the service instance.
* @param rest - Additional parameters for the service.
* @returns The service instance.
*/
static async getService<T extends ServiceType>(
type: T,
instanceName: string,
Expand Down Expand Up @@ -41,10 +54,21 @@ export class ServiceProvider {
return serviceInstance;
}

/**
* Checks if a service of the specified type exists.
* @param type - The type of the service.
* @returns True if the service exists, false otherwise.
*/
static hasService<T extends ServiceType>(type: T): boolean {
return ServiceProvider.serviceMap.has(type);
}

/**
* Checks if a service instance of the specified type and instance name exists.
* @param type - The type of the service.
* @param instanceName - The name of the service instance.
* @returns True if the service instance exists, false otherwise.
*/
static has<T extends ServiceType>(type: T, instanceName: string): boolean {
if (ServiceProvider.serviceMap.has(type)) {
const serviceInstances = ServiceProvider.serviceMap.get(type);
Expand All @@ -53,6 +77,58 @@ export class ServiceProvider {
return false;
}

/**
* Removes all instances of a service of the specified type.
* @param type - The type of the service.
*/
static clear<T extends ServiceType>(type: T): void {
if (ServiceProvider.serviceMap.has(type)) {
ServiceProvider.serviceMap.get(type)?.clear();
}
}

/**
* Removes a service instance of the specified type and instance name.
* @param type - The type of the service.
* @param instanceName - The name of the service instance.
*/
static remove<T extends ServiceType>(type: T, instanceName: string): void {
if (ServiceProvider.serviceMap.has(type)) {
const serviceInstances = ServiceProvider.serviceMap.get(type);

if (serviceInstances?.has(instanceName)) {
serviceInstances.delete(instanceName);
}
}
}

/**
* Removes a service of the specified type, including all its instances.
* @param type - The type of the service.
*/
static removeService<T extends ServiceType>(type: T): void {
if (ServiceProvider.serviceMap.has(type)) {
ServiceProvider.serviceMap.delete(type);
}
}

/**
* Removes all services, including all their instances.
*/
static clearAllServices(): void {
ServiceProvider.serviceMap.clear();
}

/**
* Materializes a service instance of the specified type and instance name.
* If the service instance does not exist, it will be created.
* @private
* @param type - The type of the service.
* @param instanceName - The name of the service instance.
* @param rest - Additional parameters for the service.
* @returns The service instance.
* @throws {Error} If the service type is unsupported or if the service instance could not be created.
*/
private static async materializeService<T extends ServiceType>(
type: T,
instanceName: string,
Expand Down Expand Up @@ -85,30 +161,4 @@ export class ServiceProvider {

return serviceInstance;
}

static clear<T extends ServiceType>(type: T): void {
if (ServiceProvider.serviceMap.has(type)) {
ServiceProvider.serviceMap.get(type)?.clear();
}
}

static remove<T extends ServiceType>(type: T, instanceName: string): void {
if (ServiceProvider.serviceMap.has(type)) {
const serviceInstances = ServiceProvider.serviceMap.get(type);

if (serviceInstances?.has(instanceName)) {
serviceInstances.delete(instanceName);
}
}
}

static removeService<T extends ServiceType>(type: T): void {
if (ServiceProvider.serviceMap.has(type)) {
ServiceProvider.serviceMap.delete(type);
}
}

static clearAllServices(): void {
ServiceProvider.serviceMap.clear();
}
}
23 changes: 0 additions & 23 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

/**
* The common set of `Logger` options.
*/
export interface LoggerOptions {
/**
* The logger name.
*/
name: string;

/**
* The desired log level.
*/
level?: LoggerLevelValue;

/**
* Create a logger with the fields set
*/
fields?: Fields;

/** log to memory instead of to a file. Intended for Unit Testing */
useMemoryLogger?: boolean;
}

/**
* Standard `Logger` levels.
*
Expand Down

0 comments on commit b12768f

Please sign in to comment.