-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26193b5
commit 1260c9c
Showing
3 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import { Controller, Get, Query, Req, Res } from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { Controller, Get, Param, Query, Res } from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import { events } from './constants'; | ||
import { EventsService } from './events.service'; | ||
|
||
@Controller('events') | ||
export class EventsController { | ||
constructor(private readonly eventsService: EventsService) {} | ||
|
||
@Get('/subscribe') | ||
@Get('/:id/subscribe') | ||
poll( | ||
@Req() req: Request, | ||
@Res() res: Response, | ||
@Param('id') id: string, | ||
@Query('isInitialRequest') isInitialRequestParam: string, | ||
) { | ||
const event = events.find((e) => e.name === id); | ||
if (!event) return res.status(404).json({ message: 'Event not found' }); | ||
|
||
const isInitialRequest = isInitialRequestParam === 'true'; | ||
if (isInitialRequest) { | ||
return res.json(this.eventsService.getCurrentAction()); | ||
return res.json(this.eventsService.getCurrentAction(event.name)); | ||
} | ||
|
||
const client = (data: any) => res.json(data); | ||
this.eventsService.addClient(client); | ||
req.on('close', () => this.eventsService.removeClient(client)); | ||
const cb = (data: any) => res.json(data); | ||
this.eventsService.addClient({ subscribedEvent: event.name, callback: cb }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { events, questions } from './constants'; | ||
|
||
interface Client { | ||
subscribedEvent: string; | ||
callback: (data: any) => void; | ||
} | ||
|
||
@Injectable() | ||
export class EventsService { | ||
private clients: Array<(data: any) => void> = []; | ||
private data: any = { action: null }; | ||
private clients: Client[] = []; | ||
private currentActions: Record<string, any> = events.reduce( | ||
(acc, event) => ({ ...acc, [event.name]: { action: null } }), | ||
{}, | ||
); | ||
|
||
constructor() { | ||
this.scheduleNewAction(); | ||
events.forEach((event) => this.scheduleNewAction(event.name)); | ||
} | ||
|
||
addClient(client: (data: any) => void) { | ||
addClient(client: Client) { | ||
this.clients.push(client); | ||
} | ||
|
||
removeClient(client: (data: any) => void) { | ||
this.clients = this.clients.filter((c) => c !== client); | ||
} | ||
notifyClients(event: string) { | ||
// Notify all clients subscribed to the event | ||
this.clients | ||
.filter((client) => client.subscribedEvent === event) | ||
.forEach((client) => client.callback(this.currentActions[event])); | ||
|
||
notifyClients() { | ||
this.clients.forEach((c) => c(this.data)); | ||
this.clients = []; | ||
// Remove clients subscribed to the event | ||
this.clients = this.clients.filter( | ||
(client) => client.subscribedEvent !== event, | ||
); | ||
} | ||
|
||
scheduleNewAction() { | ||
const actions = ['flashlight', 'vote']; | ||
const selectedAction = actions[Math.floor(Math.random() * actions.length)]; | ||
const delay = Math.floor(Math.random() * 15000) + 5000; // 5-20 seconds | ||
scheduleNewAction(event: string) { | ||
const availableActions = events.find( | ||
(e) => e.name === event, | ||
).availableActions; | ||
const selectedAction = | ||
availableActions[Math.floor(Math.random() * availableActions.length)]; | ||
const delay = Math.floor(Math.random() * 15000) + 15000; // 15-30 seconds | ||
|
||
setTimeout(() => { | ||
this.data = { action: selectedAction }; | ||
this.notifyClients(); | ||
if (selectedAction === 'flashlight') { | ||
this.currentActions[event] = { action: { type: 'flashlight' } }; | ||
} else if (selectedAction === 'vote') { | ||
const question = | ||
questions[Math.floor(Math.random() * questions.length)]; | ||
this.currentActions[event] = { action: { type: 'vote', ...question } }; | ||
} | ||
|
||
this.notifyClients(event); | ||
|
||
setTimeout( | ||
() => { | ||
this.data = { action: null }; | ||
this.notifyClients(); | ||
this.scheduleNewAction(); | ||
this.currentActions[event] = { action: null }; | ||
this.notifyClients(event); | ||
this.scheduleNewAction(event); | ||
}, | ||
selectedAction === 'flashlight' ? 5000 : 15000, | ||
); | ||
}, delay); | ||
} | ||
|
||
getCurrentAction() { | ||
if (Object.keys(this.data).length > 0) return this.data; | ||
return { action: null }; | ||
getCurrentAction(event: string) { | ||
return this.currentActions[event]; | ||
} | ||
} |