Merge remote-tracking branch 'upstream/develop/3.5' into edge-log-fix

This commit is contained in:
Volodymyr Babak 2023-04-06 16:10:56 +03:00
commit 14b0728018

View File

@ -16,7 +16,7 @@
import { BehaviorSubject, ReplaySubject } from 'rxjs'; import { BehaviorSubject, ReplaySubject } from 'rxjs';
import { CmdUpdate, CmdUpdateMsg, CmdUpdateType, WebsocketCmd } from '@shared/models/telemetry/telemetry.models'; import { CmdUpdate, CmdUpdateMsg, CmdUpdateType, WebsocketCmd } from '@shared/models/telemetry/telemetry.models';
import { first, map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { NgZone } from '@angular/core'; import { NgZone } from '@angular/core';
import { isDefinedAndNotNull } from '@core/utils'; import { isDefinedAndNotNull } from '@core/utils';
import { Notification } from '@shared/models/notification.models'; import { Notification } from '@shared/models/notification.models';
@ -47,12 +47,19 @@ export class NotificationsUpdate extends CmdUpdate {
export class NotificationSubscriber extends WsSubscriber { export class NotificationSubscriber extends WsSubscriber {
private notificationCountSubject = new ReplaySubject<NotificationCountUpdate>(1); private notificationCountSubject = new ReplaySubject<NotificationCountUpdate>(1);
private notificationsSubject = new BehaviorSubject<NotificationsUpdate>(null); private notificationsSubject = new BehaviorSubject<NotificationsUpdate>({
cmdId: 0,
cmdUpdateType: undefined,
errorCode: 0,
errorMsg: '',
notifications: [],
totalUnreadCount: 0
});
public messageLimit = 10; public messageLimit = 10;
public notificationCount$ = this.notificationCountSubject.asObservable().pipe(map(msg => msg.totalUnreadCount)); public notificationCount$ = this.notificationCountSubject.asObservable().pipe(map(msg => msg.totalUnreadCount));
public notifications$ = this.notificationsSubject.asObservable().pipe(map(msg => msg?.notifications || [])); public notifications$ = this.notificationsSubject.asObservable().pipe(map(msg => msg.notifications ));
public static createNotificationCountSubscription(notificationWsService: NotificationWebsocketService, public static createNotificationCountSubscription(notificationWsService: NotificationWebsocketService,
zone: NgZone): NotificationSubscriber { zone: NgZone): NotificationSubscriber {
@ -109,35 +116,27 @@ export class NotificationSubscriber extends WsSubscriber {
} }
onNotificationsUpdate(message: NotificationsUpdate) { onNotificationsUpdate(message: NotificationsUpdate) {
this.notificationsSubject.asObservable().pipe( const currentNotifications = this.notificationsSubject.value;
first() let processMessage = message;
).subscribe((value) => { if (isDefinedAndNotNull(currentNotifications) && message.update) {
let saveMessage; currentNotifications.notifications.unshift(message.update);
if (isDefinedAndNotNull(value) && message.update) { if (currentNotifications.notifications.length > this.messageLimit) {
const findIndex = value.notifications.findIndex(item => item.id.id === message.update.id.id); currentNotifications.notifications.pop();
if (findIndex !== -1) {
value.notifications.push(message.update);
value.notifications.sort((a, b) => b.createdTime - a.createdTime);
if (value.notifications.length > this.messageLimit) {
value.notifications.pop();
} }
} processMessage = currentNotifications;
saveMessage = value; processMessage.totalUnreadCount = message.totalUnreadCount;
} else {
saveMessage = message;
} }
if (this.zone) { if (this.zone) {
this.zone.run( this.zone.run(
() => { () => {
this.notificationsSubject.next(saveMessage); this.notificationsSubject.next(processMessage);
this.notificationCountSubject.next(saveMessage); this.notificationCountSubject.next(processMessage);
} }
); );
} else { } else {
this.notificationsSubject.next(saveMessage); this.notificationsSubject.next(processMessage);
this.notificationCountSubject.next(saveMessage); this.notificationCountSubject.next(processMessage);
} }
});
} }
} }