refactoring
This commit is contained in:
@@ -6,6 +6,6 @@ export interface ExtendedWebSocket extends WebSocket {
|
||||
payload: DecodedToken;
|
||||
isAlive: boolean;
|
||||
user: IUser;
|
||||
asyncUpdates?: NodeJS.Timeout
|
||||
asyncUpdates: Map<string, NodeJS.Timeout>
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import {SpotifyTokenService} from "../../../db/services/spotifyTokenService";
|
||||
import {UserService} from "../../../db/services/db/UserService";
|
||||
import {getCurrentlyPlaying} from "../../../db/services/spotifyApiService";
|
||||
|
||||
export const SpotifyAsyncUpdateEvent = "SPOTIFY_UPDATE";
|
||||
|
||||
export class GetSpotifyUpdatesEvent extends CustomWebsocketEvent {
|
||||
|
||||
event = WebsocketEventType.GET_SPOTIFY_UPDATES;
|
||||
@@ -12,9 +14,14 @@ export class GetSpotifyUpdatesEvent extends CustomWebsocketEvent {
|
||||
console.log("Starting Spotify updates");
|
||||
this.ws.emit(WebsocketEventType.GET_SINGLE_SPOTIFY_UPDATE, {});
|
||||
|
||||
this.ws.asyncUpdates = setInterval(() => {
|
||||
if (this.ws.asyncUpdates.has(SpotifyAsyncUpdateEvent)) {
|
||||
console.log("Spotify updates already running");
|
||||
return;
|
||||
}
|
||||
|
||||
this.ws.asyncUpdates.set(SpotifyAsyncUpdateEvent, setInterval(() => {
|
||||
this.ws.emit(WebsocketEventType.GET_SINGLE_SPOTIFY_UPDATE, {});
|
||||
}, 1000);
|
||||
}, 1000));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {WebsocketEventType} from "./websocketEventType";
|
||||
import {getCurrentWeather} from "../../../db/services/owmApiService";
|
||||
|
||||
export const WeatherAsyncUpdateEvent = "WEATHER_UPDATE";
|
||||
|
||||
export class GetWeatherUpdatesEvent extends CustomWebsocketEvent {
|
||||
|
||||
event = WebsocketEventType.GET_WEATHER_UPDATES;
|
||||
@@ -10,9 +12,16 @@ export class GetWeatherUpdatesEvent extends CustomWebsocketEvent {
|
||||
console.log("Starting weather updates");
|
||||
this.ws.emit(WebsocketEventType.GET_SINGLE_WEATHER_UPDATE);
|
||||
|
||||
this.ws.asyncUpdates = setInterval(() => {
|
||||
if (this.ws.asyncUpdates.has(WeatherAsyncUpdateEvent)) {
|
||||
console.log("Weather updates already running");
|
||||
return;
|
||||
}
|
||||
|
||||
this.ws.asyncUpdates.set(WeatherAsyncUpdateEvent, setInterval(() => {
|
||||
this.ws.emit(WebsocketEventType.GET_SINGLE_WEATHER_UPDATE);
|
||||
}, 1000 * 60);
|
||||
}, 1000 * 60));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {WebsocketEventType} from "./websocketEventType";
|
||||
import {SpotifyAsyncUpdateEvent} from "./getSpotifyUpdatesEvent";
|
||||
|
||||
export class StopSpotifyUpdatesEvent extends CustomWebsocketEvent {
|
||||
|
||||
event = WebsocketEventType.STOP_SPOTIFY_UPDATES;
|
||||
|
||||
handler = async () => {
|
||||
if (this.ws.asyncUpdates) {
|
||||
clearInterval(this.ws.asyncUpdates);
|
||||
if (this.ws.asyncUpdates.has(SpotifyAsyncUpdateEvent)) {
|
||||
clearInterval(this.ws.asyncUpdates.get(SpotifyAsyncUpdateEvent));
|
||||
this.ws.asyncUpdates.delete(SpotifyAsyncUpdateEvent);
|
||||
console.log("Spotify updates stopped");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import {WebsocketEventType} from "./websocketEventType";
|
||||
import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {UserAsyncUpdateEvent} from "./updateUserEvent";
|
||||
|
||||
export class StopUpdateUserEvent extends CustomWebsocketEvent {
|
||||
|
||||
event = WebsocketEventType.STOP_UPDATE_USER;
|
||||
|
||||
handler = async () => {
|
||||
if (this.ws.asyncUpdates.has(UserAsyncUpdateEvent)) {
|
||||
clearInterval(this.ws.asyncUpdates.get(UserAsyncUpdateEvent));
|
||||
this.ws.asyncUpdates.delete(UserAsyncUpdateEvent);
|
||||
console.log("User updates stopped");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {WebsocketEventType} from "./websocketEventType";
|
||||
import {WeatherAsyncUpdateEvent} from "./getWeatherUpdatesEvent";
|
||||
|
||||
export class StopWeatherUpdatesEvent extends CustomWebsocketEvent {
|
||||
|
||||
event = WebsocketEventType.STOP_WEATHER_UPDATES;
|
||||
|
||||
handler = async () => {
|
||||
if (this.ws.asyncUpdates) {
|
||||
clearInterval(this.ws.asyncUpdates);
|
||||
if (this.ws.asyncUpdates.has(WeatherAsyncUpdateEvent)) {
|
||||
clearInterval(this.ws.asyncUpdates.get(WeatherAsyncUpdateEvent));
|
||||
this.ws.asyncUpdates.delete(WeatherAsyncUpdateEvent);
|
||||
console.log("Weather updates stopped");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,32 @@
|
||||
import {WebsocketEventType} from "./websocketEventType";
|
||||
import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {IUser} from "../../../db/models/user";
|
||||
import {UserService} from "../../../db/services/db/UserService";
|
||||
|
||||
export const UserAsyncUpdateEvent = "USER_UPDATE";
|
||||
|
||||
export class UpdateUserEvent extends CustomWebsocketEvent {
|
||||
event = WebsocketEventType.UPDATE_USER;
|
||||
|
||||
handler = async () => {
|
||||
console.log("Starting user updates")
|
||||
if (this.ws.asyncUpdates.has(UserAsyncUpdateEvent)) {
|
||||
console.log("User updates already running");
|
||||
return;
|
||||
}
|
||||
|
||||
this.ws.asyncUpdates.set(UserAsyncUpdateEvent, setInterval(async () => {
|
||||
const userService = await UserService.create();
|
||||
const user = await userService.getUserByUUID(this.ws.payload.uuid);
|
||||
this.ws.emit(WebsocketEventType.UPDATE_USER_SINGLE, user);
|
||||
}, 1000 * 15));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export class UpdateUserSingleEvent extends CustomWebsocketEvent {
|
||||
event = WebsocketEventType.UPDATE_USER_SINGLE;
|
||||
|
||||
handler = async (data: IUser) => {
|
||||
console.log("Updating user")
|
||||
if (data) {
|
||||
|
||||
@@ -9,4 +9,7 @@ export enum WebsocketEventType {
|
||||
STOP_WEATHER_UPDATES = "STOP_WEATHER_UPDATES",
|
||||
ERROR = "ERROR",
|
||||
UPDATE_USER = "UPDATE_USER",
|
||||
UPDATE_USER_SINGLE = "UPDATE_USER_SINGLE",
|
||||
STOP_UPDATE_USER = "STOP_UPDATE_USER",
|
||||
|
||||
}
|
||||
|
||||
@@ -6,8 +6,9 @@ import {GetStateEvent} from "./getStateEvent";
|
||||
import {GetSingleWeatherUpdateEvent, GetWeatherUpdatesEvent} from "./getWeatherUpdatesEvent";
|
||||
import {StopSpotifyUpdatesEvent} from "./stopSpotifyUpdatesEvent";
|
||||
import {StopWeatherUpdatesEvent} from "./stopWeatherUpdatesEvent";
|
||||
import {UpdateUserEvent} from "./updateUserEvent";
|
||||
import {UpdateUserEvent, UpdateUserSingleEvent} from "./updateUserEvent";
|
||||
import {CustomWebsocketEvent} from "./customWebsocketEvent";
|
||||
import {StopUpdateUserEvent} from "./stopUpdateUserEvent";
|
||||
|
||||
export function getEventListeners(ws: ExtendedWebSocket): CustomWebsocketEvent[] {
|
||||
return [
|
||||
@@ -20,6 +21,8 @@ export function getEventListeners(ws: ExtendedWebSocket): CustomWebsocketEvent[]
|
||||
new GetWeatherUpdatesEvent(ws),
|
||||
new StopWeatherUpdatesEvent(ws),
|
||||
new UpdateUserEvent(ws),
|
||||
new UpdateUserSingleEvent(ws),
|
||||
new StopUpdateUserEvent(ws),
|
||||
new ErrorEvent(ws)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -21,9 +21,9 @@ export class WebsocketEventHandler {
|
||||
this.webSocket.onclose = (event) => {
|
||||
console.log("WebSocket closed:", event.code, event.reason, event.wasClean, event.type);
|
||||
console.log(`User: ${this.webSocket.payload.username} disconnected`);
|
||||
if (this.webSocket.asyncUpdates) {
|
||||
clearInterval(this.webSocket.asyncUpdates);
|
||||
console.log("Async updates stopped");
|
||||
for (const [key, value] of this.webSocket.asyncUpdates) {
|
||||
console.log("Stopping Update:", key);
|
||||
clearInterval(value);
|
||||
}
|
||||
callback();
|
||||
};
|
||||
@@ -42,7 +42,7 @@ export class WebsocketEventHandler {
|
||||
);
|
||||
}
|
||||
|
||||
public registerCustomEvent(customWebsocketEvent:CustomWebsocketEvent ) {
|
||||
public registerCustomEvent(customWebsocketEvent: CustomWebsocketEvent) {
|
||||
// bind needed?
|
||||
this.webSocket.on(customWebsocketEvent.event, customWebsocketEvent.handler.bind(customWebsocketEvent));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ export class WebsocketServerEventHandler {
|
||||
// second: set the isAlive flag to true
|
||||
ws.isAlive = true;
|
||||
|
||||
ws.asyncUpdates = new Map<string, NodeJS.Timeout>();
|
||||
|
||||
// last: call the callback function
|
||||
callback(ws, request);
|
||||
},
|
||||
|
||||
+4
-9
@@ -5,7 +5,6 @@ import {ExtendedWebSocket} from "./interfaces/extendedWebsocket";
|
||||
import {DecodedToken} from "./interfaces/decodedToken";
|
||||
import {WebsocketServerEventHandler} from "./utils/websocket/websocketServerEventHandler";
|
||||
import {WebsocketEventHandler} from "./utils/websocket/websocketEventHandler";
|
||||
import {UserService} from "./db/services/db/UserService";
|
||||
import {getEventListeners} from "./utils/websocket/websocketCustomEvents/websocketEventUtils";
|
||||
import {WebsocketEventType} from "./utils/websocket/websocketCustomEvents/websocketEventType";
|
||||
|
||||
@@ -64,15 +63,8 @@ export class ExtendedWebSocketServer {
|
||||
// Register custom events
|
||||
getEventListeners(ws).forEach(socketEventHandler.registerCustomEvent, socketEventHandler);
|
||||
|
||||
const updateUserInterval = setInterval(async () => {
|
||||
const userService = await UserService.create();
|
||||
const user = await userService.getUserByUUID(ws.payload.uuid);
|
||||
ws.emit(WebsocketEventType.UPDATE_USER, user);
|
||||
}, 15000);
|
||||
|
||||
socketEventHandler.enableDisconnectEvent(() => {
|
||||
clearInterval(updateUserInterval);
|
||||
console.log("stopped updating user");
|
||||
console.log("User disconnected");
|
||||
});
|
||||
|
||||
// send initial state and settings
|
||||
@@ -80,6 +72,9 @@ export class ExtendedWebSocketServer {
|
||||
ws.emit(WebsocketEventType.GET_STATE, {});
|
||||
ws.emit(WebsocketEventType.GET_SETTINGS, {});
|
||||
|
||||
// initiate update user event
|
||||
ws.emit(WebsocketEventType.UPDATE_USER, {});
|
||||
|
||||
const mode = ws.user.lastState?.global.mode;
|
||||
if (mode === "clock" && !ws.asyncUpdates) {
|
||||
ws.emit(WebsocketEventType.GET_WEATHER_UPDATES, {})
|
||||
|
||||
Reference in New Issue
Block a user