Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 5147b41

Browse files
committed
fix: download event not updated in client
1 parent 2474535 commit 5147b41

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

cortex-js/src/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { SeedService } from './usecases/seed/seed.service';
1515
import { FileManagerModule } from './infrastructure/services/file-manager/file-manager.module';
1616
import { AppLoggerMiddleware } from './infrastructure/middlewares/app.logger.middleware';
1717
import { EventEmitterModule } from '@nestjs/event-emitter';
18-
import { AppController } from './infrastructure/controllers/app.controller';
1918
import { DownloadManagerModule } from './download-manager/download-manager.module';
19+
import { EventsController } from './infrastructure/controllers/events.controller';
2020

2121
@Module({
2222
imports: [
@@ -40,7 +40,7 @@ import { DownloadManagerModule } from './download-manager/download-manager.modul
4040
ModelRepositoryModule,
4141
DownloadManagerModule,
4242
],
43-
controllers: [AppController],
43+
controllers: [EventsController],
4444
providers: [SeedService],
4545
})
4646
export class AppModule implements NestModule {

cortex-js/src/download-manager/download-manager.service.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ export class DownloadManagerService {
1919
constructor(
2020
private readonly httpService: HttpService,
2121
private readonly eventEmitter: EventEmitter2,
22-
) {
23-
// start emitting download state each 500ms
24-
// setInterval(() => {
25-
// this.eventEmitter.emit('download.event', this.allDownloadStates);
26-
// }, 500);
27-
}
22+
) {}
2823

2924
async abortDownload(downloadId: string) {
3025
if (!this.abortControllers[downloadId]) {
@@ -37,6 +32,7 @@ export class DownloadManagerService {
3732
this.allDownloadStates = this.allDownloadStates.filter(
3833
(downloadState) => downloadState.id !== downloadId,
3934
);
35+
this.eventEmitter.emit('download.event.aborted', this.allDownloadStates);
4036
}
4137

4238
async submitDownloadRequest(
@@ -160,6 +156,7 @@ export class DownloadManagerService {
160156
(downloadState) => downloadState.id !== downloadId,
161157
);
162158
}
159+
this.eventEmitter.emit('download.event', this.allDownloadStates);
163160
});
164161

165162
writer.on('error', (error) => {
@@ -186,6 +183,7 @@ export class DownloadManagerService {
186183
this.allDownloadStates = this.allDownloadStates.filter(
187184
(downloadState) => downloadState.id !== downloadId,
188185
);
186+
this.eventEmitter.emit('download.event', this.allDownloadStates);
189187
});
190188

191189
response.data.on('data', (chunk: any) => {
@@ -202,8 +200,13 @@ export class DownloadManagerService {
202200
if (downloadItem) {
203201
downloadItem.size.transferred = transferredBytes;
204202
}
203+
this.eventEmitter.emit('download.event', this.allDownloadStates);
205204
});
206205

207206
response.data.pipe(writer);
208207
}
208+
209+
getDownloadStates() {
210+
return this.allDownloadStates;
211+
}
209212
}

cortex-js/src/infrastructure/controllers/app.controller.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
DownloadState,
3+
DownloadStateEvent,
4+
} from '@/domain/models/download.interface';
5+
import { DownloadManagerService } from '@/download-manager/download-manager.service';
6+
import { Controller, Sse } from '@nestjs/common';
7+
import { EventEmitter2 } from '@nestjs/event-emitter';
8+
import { Observable, fromEvent, map, merge, of, throttleTime } from 'rxjs';
9+
10+
@Controller('events')
11+
export class EventsController {
12+
constructor(
13+
private readonly downloadManagerService: DownloadManagerService,
14+
private readonly eventEmitter: EventEmitter2,
15+
) {}
16+
17+
@Sse('download')
18+
downloadEvent(): Observable<DownloadStateEvent> {
19+
// Welcome message Observable
20+
const latestDownloadState$: Observable<DownloadStateEvent> = of({
21+
data: this.downloadManagerService.getDownloadStates(),
22+
});
23+
24+
const downloadAbortEvent$ = fromEvent<DownloadState[]>(
25+
this.eventEmitter,
26+
'download.event.aborted',
27+
).pipe(map((downloadState) => ({ data: downloadState })));
28+
29+
const downloadEvent$ = fromEvent<DownloadState[]>(
30+
this.eventEmitter,
31+
'download.event',
32+
).pipe(
33+
map((downloadState) => ({ data: downloadState })),
34+
throttleTime(1000),
35+
);
36+
37+
return merge(
38+
latestDownloadState$,
39+
downloadEvent$,
40+
downloadAbortEvent$,
41+
).pipe();
42+
}
43+
}

0 commit comments

Comments
 (0)