From 4667a283f196ac88d01d3ed1b5a69c7004422f30 Mon Sep 17 00:00:00 2001 From: Sandarsh Sridhar Date: Tue, 29 Nov 2022 14:02:37 -0600 Subject: [PATCH] refactor: separation of concerns --- src/handlers/emitters/dance-to-spotify-emitter.ts | 8 +++++--- src/handlers/listeners/dance-to-spotify-listener.ts | 4 ++-- src/handlers/route-handlers/dance-to-spotify-router.ts | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/handlers/emitters/dance-to-spotify-emitter.ts b/src/handlers/emitters/dance-to-spotify-emitter.ts index 2f1e8e0..b176d4d 100644 --- a/src/handlers/emitters/dance-to-spotify-emitter.ts +++ b/src/handlers/emitters/dance-to-spotify-emitter.ts @@ -10,7 +10,7 @@ import { container } from '../../utils/inversify-orchestrator.js'; import { Logger } from '../../utils/logger.js'; import { TYPES } from '../../utils/types.js'; -export const emitDanceToSpotifyEvent = async (roomIds: Array): Promise => { +export const emitDanceToSpotifyEvent = async (): Promise => { const eventBus = container.get(TYPES.EventBus); const logger = container.get(TYPES.Logger); @@ -34,7 +34,7 @@ export const emitDanceToSpotifyEvent = async (roomIds: Array): Promise): Promise { - const elapsedTimeMs = (process.hrtime(timer)[0] * 1000000000 + process.hrtime(timer)[1]) / 1000000; + const billion = 1000000000; + const million = 1000000; + const elapsedTimeMs = (process.hrtime(timer)[0] * billion + process.hrtime(timer)[1]) / million; return elapsedTimeMs > apiConfig.pollingDelayMs; }; diff --git a/src/handlers/listeners/dance-to-spotify-listener.ts b/src/handlers/listeners/dance-to-spotify-listener.ts index ea8d51a..97f630c 100644 --- a/src/handlers/listeners/dance-to-spotify-listener.ts +++ b/src/handlers/listeners/dance-to-spotify-listener.ts @@ -4,10 +4,10 @@ import { setRoom } from '../../services/wiz/lights-service.js'; import { container } from '../../utils/inversify-orchestrator.js'; import { TYPES } from '../../utils/types.js'; -export const listenToDanceToSpotifyEvent = async () => { +export const listenToDanceToSpotifyEvent = async (roomIds: Array) => { const eventBus = container.get(TYPES.EventBus); - eventBus.on('changeLights', async (roomIds: Array, brightness: number, colorSpace: ColorSpace) => { + eventBus.on('changeLights', async (brightness: number, colorSpace: ColorSpace) => { const bulb: Bulb = { state: true, brightness diff --git a/src/handlers/route-handlers/dance-to-spotify-router.ts b/src/handlers/route-handlers/dance-to-spotify-router.ts index 83b8bb9..f2b1261 100644 --- a/src/handlers/route-handlers/dance-to-spotify-router.ts +++ b/src/handlers/route-handlers/dance-to-spotify-router.ts @@ -57,8 +57,8 @@ danceToSpotifyRouter.get('/dance-to-spotify', async (req: Request) => { roomIds = Object.keys(cacheManager.get('rooms')); } - await listenToDanceToSpotifyEvent(); - await emitDanceToSpotifyEvent(roomIds); + await listenToDanceToSpotifyEvent(roomIds); + await emitDanceToSpotifyEvent(); cacheManager.set('instance', 'stopped'); } catch (err: any) {