Skip to content

Commit

Permalink
Merge pull request #246 from neilenns/neilenns/issue245
Browse files Browse the repository at this point in the history
Use generics to remove code duplication
  • Loading branch information
neilenns authored Aug 21, 2024
2 parents 0beb730 + 527a53f commit 4809f04
Showing 1 changed file with 18 additions and 22 deletions.
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

0 comments on commit 4809f04

Please sign in to comment.