forked from herraristotle/BookLore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.ts
More file actions
97 lines (92 loc) · 4.14 KB
/
app.component.ts
File metadata and controls
97 lines (92 loc) · 4.14 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {RxStompService} from './shared/websocket/rx-stomp.service';
import {BookService} from './book/service/book.service';
import {NotificationEventService} from './shared/websocket/notification-event.service';
import {parseLogNotification} from './shared/websocket/model/log-notification.model';
import {ConfirmDialog} from 'primeng/confirmdialog';
import {Toast} from 'primeng/toast';
import {RouterOutlet} from '@angular/router';
import {AuthInitializationService} from './auth-initialization-service';
import {AppConfigService} from './core/service/app-config.service';
import {MetadataBatchProgressNotification} from './core/model/metadata-batch-progress.model';
import {MetadataProgressService} from './core/service/metadata-progress-service';
import {BookdropFileNotification, BookdropFileService} from './bookdrop/bookdrop-file.service';
import {DuplicateFileNotification} from './shared/websocket/model/duplicate-file-notification.model';
import {DuplicateFileService} from './shared/websocket/duplicate-file.service';
import {Subscription} from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
standalone: true,
imports: [ConfirmDialog, Toast, RouterOutlet]
})
export class AppComponent implements OnInit, OnDestroy {
loading = true;
private subscriptions: Subscription[] = [];
private subscriptionsInitialized = false; // Prevent multiple subscription setups
private authInit = inject(AuthInitializationService);
private bookService = inject(BookService);
private rxStompService = inject(RxStompService);
private notificationEventService = inject(NotificationEventService);
private metadataProgressService = inject(MetadataProgressService);
private bookdropFileService = inject(BookdropFileService);
private duplicateFileService = inject(DuplicateFileService);
private appConfigService = inject(AppConfigService); // Keep it here to ensure the service is initialized
ngOnInit(): void {
this.authInit.initialized$.subscribe(ready => {
this.loading = !ready;
if (ready && !this.subscriptionsInitialized) {
this.setupWebSocketSubscriptions();
this.subscriptionsInitialized = true;
}
});
}
private setupWebSocketSubscriptions(): void {
this.subscriptions.push(
this.rxStompService.watch('/user/queue/book-add').subscribe(msg =>
this.bookService.handleNewlyCreatedBook(JSON.parse(msg.body))
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/books-remove').subscribe(msg =>
this.bookService.handleRemovedBookIds(JSON.parse(msg.body))
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/book-metadata-update').subscribe(msg =>
this.bookService.handleBookUpdate(JSON.parse(msg.body))
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/book-metadata-batch-update').subscribe(msg =>
this.bookService.handleMultipleBookUpdates(JSON.parse(msg.body))
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/book-metadata-batch-progress').subscribe(msg =>
this.metadataProgressService.handleIncomingProgress(JSON.parse(msg.body) as MetadataBatchProgressNotification)
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/log').subscribe(msg => {
const logNotification = parseLogNotification(msg.body);
this.notificationEventService.handleNewNotification(logNotification);
})
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/duplicate-file').subscribe(msg =>
this.duplicateFileService.addDuplicateFile(JSON.parse(msg.body) as DuplicateFileNotification)
)
);
this.subscriptions.push(
this.rxStompService.watch('/user/queue/bookdrop-file').subscribe(msg => {
const notification = JSON.parse(msg.body) as BookdropFileNotification;
this.bookdropFileService.handleIncomingFile(notification);
})
);
}
ngOnDestroy(): void {
this.subscriptions.forEach(sub => sub.unsubscribe());
}
}