feat: Update CustomWebsocketEvent to support async handlers and refactor event registration
This commit is contained in:
@@ -2,7 +2,7 @@ import { ExtendedWebSocket } from "../../../interfaces/extendedWebsocket";
|
||||
|
||||
export abstract class CustomWebsocketEvent<T = object> {
|
||||
abstract event: string;
|
||||
abstract handler: (data: T) => void;
|
||||
abstract handler: (data: T) => void | Promise<void>;
|
||||
protected ws: ExtendedWebSocket;
|
||||
|
||||
public constructor(ws: ExtendedWebSocket) {
|
||||
|
||||
@@ -7,15 +7,24 @@ import { GetWeatherUpdatesEvent } from "./getWeatherUpdatesEvent";
|
||||
import { StopSpotifyUpdatesEvent } from "./stopSpotifyUpdatesEvent";
|
||||
import { StopWeatherUpdatesEvent } from "./stopWeatherUpdatesEvent";
|
||||
import { UpdateUserSingleEvent } from "./updateUserEvent";
|
||||
import { CustomWebsocketEvent } from "./customWebsocketEvent";
|
||||
import { SpotifyPollingService } from "../../../services/spotifyPollingService";
|
||||
import { WeatherPollingService } from "../../../services/weatherPollingService";
|
||||
|
||||
export type WebsocketEvent =
|
||||
| GetStateEvent
|
||||
| GetSettingsEvent
|
||||
| GetSpotifyUpdatesEvent
|
||||
| StopSpotifyUpdatesEvent
|
||||
| GetWeatherUpdatesEvent
|
||||
| StopWeatherUpdatesEvent
|
||||
| UpdateUserSingleEvent
|
||||
| ErrorEvent;
|
||||
|
||||
export function getEventListeners(
|
||||
ws: ExtendedWebSocket,
|
||||
spotifyPollingService: SpotifyPollingService,
|
||||
weatherPollingService: WeatherPollingService
|
||||
): CustomWebsocketEvent[] {
|
||||
): WebsocketEvent[] {
|
||||
return [
|
||||
new GetStateEvent(ws),
|
||||
new GetSettingsEvent(ws),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ExtendedWebSocket } from "../../interfaces/extendedWebsocket";
|
||||
import { CustomWebsocketEvent } from "./websocketCustomEvents/customWebsocketEvent";
|
||||
import { getEventListeners } from "./websocketCustomEvents/websocketEventUtils";
|
||||
import { getEventListeners, WebsocketEvent } from "./websocketCustomEvents/websocketEventUtils";
|
||||
import { SpotifyPollingService } from "../../services/spotifyPollingService";
|
||||
import { WeatherPollingService } from "../../services/weatherPollingService";
|
||||
import logger from "../../utils/logger";
|
||||
@@ -54,7 +54,7 @@ export class WebsocketEventHandler {
|
||||
events.forEach(this.registerCustomEvent, this);
|
||||
}
|
||||
|
||||
private registerCustomEvent(customWebsocketEvent: CustomWebsocketEvent) {
|
||||
private registerCustomEvent(customWebsocketEvent: WebsocketEvent) {
|
||||
this.webSocket.on(customWebsocketEvent.event, customWebsocketEvent.handler.bind(customWebsocketEvent));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user