|
1 | 1 | import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; |
2 | 2 | import { Server } from 'socket.io'; |
3 | | -import { TestRun } from '@prisma/client'; |
| 3 | +import { Build, TestRun } from '@prisma/client'; |
4 | 4 | import { BuildDto } from '../../builds/dto/build.dto'; |
| 5 | +import { debounce } from 'lodash'; |
| 6 | +import { PrismaService } from '../../prisma/prisma.service'; |
5 | 7 |
|
6 | 8 | @WebSocketGateway() |
7 | 9 | export class EventsGateway { |
8 | 10 | @WebSocketServer() |
9 | 11 | server: Server; |
10 | 12 |
|
| 13 | + constructor(private prismaService: PrismaService) {} |
| 14 | + |
| 15 | + private debounceTimeout = 1500; |
| 16 | + private maxWait = 3000; |
| 17 | + private testRunsCreatedQueued: Array<TestRun> = []; |
| 18 | + private testRunsDeletedQueued: Array<TestRun> = []; |
| 19 | + private testRunsUpdatedQueued: Array<TestRun> = []; |
| 20 | + private buildsUpdatedQueued: Array<string> = []; |
| 21 | + |
11 | 22 | buildCreated(build: BuildDto): void { |
12 | 23 | this.server.emit('build_created', build); |
13 | 24 | } |
14 | 25 |
|
15 | | - buildUpdated(build: BuildDto): void { |
16 | | - this.server.emit('build_updated', build); |
| 26 | + buildUpdated(id: string): void { |
| 27 | + this.buildsUpdatedQueued.push(id); |
| 28 | + this.buildUpdatedDebounced(); |
17 | 29 | } |
18 | 30 |
|
19 | 31 | testRunCreated(testRun: TestRun): void { |
20 | | - this.server.emit('testRun_created', testRun); |
| 32 | + this.testRunsCreatedQueued.push(testRun); |
| 33 | + this.testRunCreatedDebounced(); |
| 34 | + this.buildUpdated(testRun.buildId); |
21 | 35 | } |
22 | 36 |
|
23 | 37 | testRunUpdated(testRun: TestRun): void { |
24 | | - this.server.emit('testRun_updated', testRun); |
| 38 | + this.testRunsUpdatedQueued.push(testRun); |
| 39 | + this.testRunUpdatedDebounced(); |
| 40 | + this.buildUpdated(testRun.buildId); |
25 | 41 | } |
26 | 42 |
|
27 | 43 | testRunDeleted(testRun: TestRun): void { |
28 | | - this.server.emit('testRun_deleted', testRun); |
| 44 | + this.testRunsDeletedQueued.push(testRun); |
| 45 | + this.testRunDeletedDebounced(); |
| 46 | + this.buildUpdated(testRun.buildId); |
29 | 47 | } |
| 48 | + |
| 49 | + private testRunUpdatedDebounced = debounce( |
| 50 | + () => { |
| 51 | + this.server.emit('testRun_updated', this.testRunsUpdatedQueued); |
| 52 | + this.testRunsUpdatedQueued = []; |
| 53 | + }, |
| 54 | + this.debounceTimeout, |
| 55 | + { |
| 56 | + leading: true, |
| 57 | + maxWait: this.maxWait, |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + private testRunCreatedDebounced = debounce( |
| 62 | + () => { |
| 63 | + this.server.emit('testRun_created', this.testRunsCreatedQueued); |
| 64 | + this.testRunsCreatedQueued = []; |
| 65 | + }, |
| 66 | + this.debounceTimeout, |
| 67 | + { |
| 68 | + leading: true, |
| 69 | + maxWait: this.maxWait, |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + private testRunDeletedDebounced = debounce( |
| 74 | + () => { |
| 75 | + this.server.emit('testRun_deleted', this.testRunsDeletedQueued); |
| 76 | + this.testRunsDeletedQueued = []; |
| 77 | + }, |
| 78 | + this.debounceTimeout, |
| 79 | + { |
| 80 | + leading: true, |
| 81 | + maxWait: this.maxWait, |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + private buildUpdatedDebounced = debounce( |
| 86 | + () => { |
| 87 | + this.prismaService.build |
| 88 | + .findMany({ |
| 89 | + where: { |
| 90 | + id: { |
| 91 | + in: this.buildsUpdatedQueued, |
| 92 | + }, |
| 93 | + }, |
| 94 | + include: { |
| 95 | + testRuns: true, |
| 96 | + }, |
| 97 | + }) |
| 98 | + .then((builds: Array<Build>) => { |
| 99 | + this.server.emit( |
| 100 | + 'build_updated', |
| 101 | + builds.map((build: Build) => new BuildDto(build)) |
| 102 | + ); |
| 103 | + }); |
| 104 | + this.buildsUpdatedQueued = []; |
| 105 | + }, |
| 106 | + this.debounceTimeout, |
| 107 | + { |
| 108 | + leading: true, |
| 109 | + maxWait: this.maxWait, |
| 110 | + } |
| 111 | + ); |
30 | 112 | } |
0 commit comments