Skip to content

Commit 57fbe02

Browse files
committed
fix(logbook): resolve duplicated comments
1 parent fc74eb4 commit 57fbe02

3 files changed

Lines changed: 42 additions & 35 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject } from 'rxjs';
2+
import { Subject } from 'rxjs';
33
import { ChangeStreamNotification } from './changestreamnotification.model';
44

55
@Injectable({
6-
providedIn: 'root',
6+
providedIn: 'root'
77
})
88
export class AddContentService {
9-
msg: ChangeStreamNotification = {};
10-
private messageSource = new BehaviorSubject(this.msg);
11-
currentMessage = this.messageSource.asObservable();
12-
constructor() {}
9+
10+
private messageSource = new Subject<ChangeStreamNotification>();
11+
12+
currentMessage$ = this.messageSource.asObservable();
13+
14+
constructor() { }
1315

1416
changeMessage(message: ChangeStreamNotification) {
1517
this.messageSource.next(message);
16-
console.log(this.msg);
17-
this.messageSource.next(null);
18-
console.log(this.msg);
1918
}
2019
}
20+

scilog/src/app/logbook/core/add-content/add-content.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class AddContentComponent implements OnInit, OnDestroy {
102102
ngOnInit(): void {
103103
this.setupComponent();
104104
this.subscriptions.push(
105-
this.dataService.currentMessage.subscribe((message) => {
105+
this.dataService.currentMessage$.subscribe((message) => {
106106
console.log(message);
107107
if (message != null) {
108108
// this.message = message;

scilog/src/app/logbook/widgets/logbook-item/logbook-item.component.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -319,36 +319,43 @@ export class LogbookItemComponent implements OnInit, AfterViewInit, OnDestroy {
319319
}
320320

321321
startNotificationManager() {
322-
if (typeof this.config != 'undefined') {
323-
if (this.changeStreamSubscriptions.length > 0) {
324-
this.changeStreamSubscriptions.forEach((sub) => sub.unsubscribe());
325-
}
326-
let logbooks = [this.config.filter.targetId, ...this.config.filter?.additionalLogbooks];
327-
console.log('Subscribing to the following logbooks: ', logbooks);
328-
logbooks.forEach((log) => {
329-
this.changeStreamSubscriptions.push(
330-
this.notificationService.getNotification(log, this.config).subscribe((notification) => {
331-
console.log(notification);
332-
this.notifications.push(notification);
333-
this.parseNotification(notification);
334-
}),
335-
);
336-
});
322+
if (!this.config) return; // Clean old changeStream subscriptions
323+
if (this.changeStreamSubscriptions.length > 0) {
324+
this.changeStreamSubscriptions.forEach((s) => s.unsubscribe());
325+
this.changeStreamSubscriptions = [];
326+
}
327+
const logbooks = [
328+
this.config.filter.targetId,
329+
...(this.config.filter?.additionalLogbooks ?? []),
330+
];
337331

338-
if (this.dataService == null) {
339-
this.dataService = this.data.currentMessage.subscribe((message) => {
340-
// console.log(message);
341-
this.message = message;
342-
if (message != null && Object.keys(this.message).length != 0) {
343-
// console.log(this.message);
344-
this.submitContent(message);
345-
this.message = null;
346-
}
332+
logbooks.forEach((log) => {
333+
const sub = this.notificationService
334+
.getNotification(log, this.config)
335+
.subscribe((notification) => {
336+
this.parseNotification(notification);
347337
});
348-
}
338+
339+
this.changeStreamSubscriptions.push(sub);
340+
});
341+
342+
// ALWAYS unsubscribe previous message subscription
343+
if (this.dataService) {
344+
this.dataService.unsubscribe();
345+
this.dataService = null;
349346
}
347+
348+
this.dataService = this.data.currentMessage$.subscribe((message) => {
349+
if (!message) return;
350+
351+
// Only main widget should submit
352+
if (this.configIndex !== 1) return;
353+
354+
this.submitContent(message);
355+
});
350356
}
351357

358+
352359
async parseNotification(notification: ChangeStreamNotification) {
353360
switch (notification.operationType) {
354361
case 'update':

0 commit comments

Comments
 (0)