Skip to content

Commit

Permalink
refactor: separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
sandarshsridhar committed Nov 29, 2022
1 parent 55422b1 commit 4667a28
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/handlers/emitters/dance-to-spotify-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>): Promise<void> => {
export const emitDanceToSpotifyEvent = async (): Promise<void> => {
const eventBus = container.get<EventEmitter>(TYPES.EventBus);
const logger = container.get<Logger>(TYPES.Logger);

Expand All @@ -34,7 +34,7 @@ export const emitDanceToSpotifyEvent = async (roomIds: Array<string>): Promise<v
const beats = getBeats(beatsMap, song);
const lights = translateBeatsToLights(beats);

eventBus.emit('changeLights', roomIds, alternateBrightness ? lights.brightness : 10, lights.colorSpace);
eventBus.emit('changeLights', alternateBrightness ? lights.brightness : 10, lights.colorSpace);

await sleep(lights.delayMs);

Expand Down Expand Up @@ -63,7 +63,9 @@ export const emitDanceToSpotifyEvent = async (roomIds: Array<string>): Promise<v
};

const isLaterThanPollingDelay = (timer: [number, number]) => {
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;
};
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/listeners/dance-to-spotify-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) => {
const eventBus = container.get<EventEmitter>(TYPES.EventBus);

eventBus.on('changeLights', async (roomIds: Array<string>, brightness: number, colorSpace: ColorSpace) => {
eventBus.on('changeLights', async (brightness: number, colorSpace: ColorSpace) => {
const bulb: Bulb = {
state: true,
brightness
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/route-handlers/dance-to-spotify-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ danceToSpotifyRouter.get('/dance-to-spotify', async (req: Request) => {
roomIds = Object.keys(<never>cacheManager.get('rooms'));
}

await listenToDanceToSpotifyEvent();
await emitDanceToSpotifyEvent(roomIds);
await listenToDanceToSpotifyEvent(roomIds);
await emitDanceToSpotifyEvent();

cacheManager.set('instance', 'stopped');
} catch (err: any) {
Expand Down

0 comments on commit 4667a28

Please sign in to comment.