Skip to content

Commit ad685b5

Browse files
change: remove wait parameter
1 parent f7c7bb0 commit ad685b5

File tree

3 files changed

+8
-31
lines changed

3 files changed

+8
-31
lines changed

docs/s4-api-docs.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ The API provides endpoints to subscribe to the actions of an event and to send a
1010

1111
This endpoint uses HTTP Long Polling, meaning the connection will remain open until an action is available. Clients should make a request to this endpoint and wait for a response. Once a response is received, the client should immediately make another request to continue receiving actions.
1212

13-
For the initial request, it is possible to pass the query parameter `wait=false` to get the current action immediately.
14-
1513
```
1614
GET /events/:id/subscribe
1715
```

src/events/events.controller.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
NotFoundException,
77
Param,
88
Post,
9-
Query,
109
Res,
1110
} from '@nestjs/common';
1211
import { Response } from 'express';
@@ -18,21 +17,12 @@ export class EventsController {
1817
constructor(private readonly eventsService: EventsService) {}
1918

2019
@Get(':id/subscribe')
21-
poll(
22-
@Res() res: Response,
23-
@Param('id') id: string,
24-
@Query('wait') waitParam: string,
25-
) {
20+
poll(@Res() res: Response, @Param('id') id: string) {
2621
const event = events.find((e) => e.name === id);
2722
if (!event) throw new NotFoundException('Event not found');
2823

29-
const wait = waitParam === 'false';
30-
if (wait) {
31-
return res.json(this.eventsService.getCurrentAction(event.name));
32-
}
33-
34-
const cb = (data: any) => res.json(data);
35-
this.eventsService.addClient({ subscribedEvent: event.name, callback: cb });
24+
const callback = (data: any) => res.json(data);
25+
this.eventsService.addClient({ subscribedEvent: event.name, callback });
3626
}
3727

3828
@Post(':id/vote')

src/events/events.service.ts

+5-16
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export interface Vote {
1414
@Injectable()
1515
export class EventsService {
1616
private clients: Client[] = [];
17-
private currentActions: Record<string, any> = events.reduce(
18-
(acc, event) => ({ ...acc, [event.name]: { action: null } }),
19-
{},
20-
);
2117
private votes: Record<string, Vote[]> = events.reduce(
2218
(acc, event) => ({ ...acc, [event.name]: [] }),
2319
{},
@@ -31,11 +27,11 @@ export class EventsService {
3127
this.clients.push(client);
3228
}
3329

34-
notifyClients(event: string) {
30+
notifyClients(event: string, action: any) {
3531
// Notify all clients subscribed to the event
3632
this.clients
3733
.filter((client) => client.subscribedEvent === event)
38-
.forEach((client) => client.callback(this.currentActions[event]));
34+
.forEach((client) => client.callback(action));
3935

4036
// Remove clients subscribed to the event
4137
this.clients = this.clients.filter(
@@ -53,30 +49,23 @@ export class EventsService {
5349

5450
setTimeout(() => {
5551
if (selectedAction === 'flashlight') {
56-
this.currentActions[event] = { action: { type: 'flashlight' } };
52+
this.notifyClients(event, { action: { type: 'flashlight' } });
5753
} else if (selectedAction === 'vote') {
5854
const question =
5955
questions[Math.floor(Math.random() * questions.length)];
60-
this.currentActions[event] = { action: { type: 'vote', ...question } };
56+
this.notifyClients(event, { action: { type: 'vote', ...question } });
6157
}
6258

63-
this.notifyClients(event);
64-
6559
setTimeout(
6660
() => {
67-
this.currentActions[event] = { action: null };
68-
this.notifyClients(event);
61+
this.notifyClients(event, { action: null });
6962
this.scheduleNewAction(event);
7063
},
7164
selectedAction === 'flashlight' ? 5000 : 15000,
7265
);
7366
}, delay);
7467
}
7568

76-
getCurrentAction(event: string) {
77-
return this.currentActions[event];
78-
}
79-
8069
addVote(event: string, answer: 'a' | 'b') {
8170
this.votes[event].push({ answer, createdAt: new Date() });
8271
}

0 commit comments

Comments
 (0)