forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-events.service.ts
More file actions
78 lines (66 loc) · 2.26 KB
/
Copy pathbatch-events.service.ts
File metadata and controls
78 lines (66 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Injectable, Logger } from "@nestjs/common";
import { OnEvent } from "@nestjs/event-emitter";
import { WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
import { Server } from "socket.io";
import { BatchProgressEventDto } from "./dto/batch-status.dto";
@Injectable()
@WebSocketGateway({
namespace: "batch",
cors: {
origin: "*",
},
})
export class BatchEventsService {
private readonly logger = new Logger(BatchEventsService.name);
@WebSocketServer()
server!: Server;
/**
* Handle batch progress events and emit via WebSocket
*/
@OnEvent("batch.progress")
handleBatchProgress(event: BatchProgressEventDto): void {
this.logger.debug(`Emitting progress for batch ${event.batchId}: ${event.progress}%`);
// Emit to specific batch room
this.server.to(`batch:${event.batchId}`).emit("progress", event);
// Emit to user's batch updates (if user context is available)
this.server.emit("batch:update", event);
}
/**
* Handle batch completion events
*/
@OnEvent("batch.completed")
handleBatchCompleted(event: BatchProgressEventDto): void {
this.logger.log(`Batch ${event.batchId} completed`);
this.server.to(`batch:${event.batchId}`).emit("completed", event);
this.server.emit("batch:completed", event);
}
/**
* Handle batch failure events
*/
@OnEvent("batch.failed")
handleBatchFailed(event: BatchProgressEventDto & { error?: string }): void {
this.logger.warn(`Batch ${event.batchId} failed: ${event.error}`);
this.server.to(`batch:${event.batchId}`).emit("failed", event);
this.server.emit("batch:failed", event);
}
/**
* Subscribe a client to batch updates
*/
subscribeToBatch(clientId: string, batchId: string): void {
const client = this.server.sockets.sockets.get(clientId);
if (client) {
client.join(`batch:${batchId}`);
this.logger.debug(`Client ${clientId} subscribed to batch ${batchId}`);
}
}
/**
* Unsubscribe a client from batch updates
*/
unsubscribeFromBatch(clientId: string, batchId: string): void {
const client = this.server.sockets.sockets.get(clientId);
if (client) {
client.leave(`batch:${batchId}`);
this.logger.debug(`Client ${clientId} unsubscribed from batch ${batchId}`);
}
}
}