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

Use generics to remove code duplication #246

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Changes from all 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
40 changes: 18 additions & 22 deletions src/managers/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AtisLetterSettings } from "@actions/atisLetter";
import { HotlineSettings } from "@actions/hotline";
import { PushToTalkSettings } from "@actions/pushToTalk";
import { StationSettings } from "@actions/stationStatus";
import { TrackAudioStatusSettings } from "@actions/trackAudioStatus";
import {
Expand Down Expand Up @@ -27,11 +28,10 @@ import {
} from "@interfaces/messages";
import trackAudioManager from "@managers/trackAudio";
import { handleAsyncException } from "@root/utils/handleAsyncException";
import mainLogger from "@utils/logger";
import debounce from "debounce";
import { EventEmitter } from "events";
import vatsimManager from "./vatsim";
import { PushToTalkSettings } from "@actions/pushToTalk";
import mainLogger from "@utils/logger";

const logger = mainLogger.child({ service: "action" });

Expand Down Expand Up @@ -650,59 +650,55 @@ class ActionManager extends EventEmitter {
return this.actions;
}

/**
* Returns a list of controllers that match the type guard.
* @param typeGuard Function that returns true if the Controller is the correct type
* @returns A list of controllers matching the type guard
*/
public getControllers<T extends Controller>(
typeGuard: (action: Controller) => action is T
): T[] {
return this.actions.filter(typeGuard);
}

/**
* Retrieves the list of all tracked StationStatusControllers.
* @returns An array of StationStatusControllers
*/
public getStationStatusControllers(): StationStatusController[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.actions.filter((action) =>
isStationStatusController(action)
) as StationStatusController[];
return this.getControllers(isStationStatusController);
}

/**
* Retrieves the list of all tracked PushToTalkControllers.
* @returns An array of PushToTalkControllers
*/
public getPushToTalkControllers(): PushToTalkController[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.actions.filter((action) =>
isPushToTalkController(action)
) as PushToTalkController[];
return this.getControllers(isPushToTalkController);
}

/**
* Retrieves the list of all tracked HotlineControllers.
* @returns An array of HotlineControllers
*/
public getHotlineControllers(): HotlineController[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.actions.filter((action) =>
isHotlineController(action)
) as HotlineController[];
return this.getControllers(isHotlineController);
}

/**
* Retrieves the list of all tracked AtisLetterControllers.
* @returns An array of AtisLetterControllers
*/
public getAtisLetterControllers(): AtisLetterController[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.actions.filter((action) =>
isAtisLetterController(action)
) as AtisLetterController[];
return this.getControllers(isAtisLetterController);
}

/**
* Retrieves the list of all tracked TrackAudioStatusControllers.
* @returns An array of TrackAudioStatusControllers
*/
public getTrackAudioStatusControllers(): TrackAudioStatusController[] {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return this.actions.filter((action) =>
isTrackAudioStatusController(action)
) as TrackAudioStatusController[];
return this.getControllers(isTrackAudioStatusController);
}

/**
Expand Down