Notifications caching + DTOs

This commit is contained in:
Lorenz Hilpert
2021-09-27 14:06:42 +02:00
parent 5041be7831
commit 2f1584436a
6 changed files with 67 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr';
import { Observable } from 'rxjs';
import { shareReplay } from 'rxjs/operators';
import { SignalRHubOptions } from './signalr-hub-options';
export abstract class SignalrHub {
@@ -64,6 +65,6 @@ export abstract class SignalrHub {
return () => {
this._hubConnection.off(methodName, handler);
};
});
}).pipe(shareReplay());
}
}

View File

@@ -0,0 +1,15 @@
import { TargetDTO } from './target.dto';
export interface EnvelopeDTO<T> {
uId?: string;
timestamp?: string;
sequence?: number;
target?: TargetDTO;
action?: string;
data?: T;
}

View File

@@ -0,0 +1,5 @@
// start:ng42.barrel
export * from './envelope.dto';
export * from './message-board-item.dto';
export * from './target.dto';
// end:ng42.barrel

View File

@@ -0,0 +1,19 @@
import { QueryTokenDTO } from '@swagger/cat';
export interface MessageBoardItemDTO {
uId?: string;
type?: string;
category?: string;
command?: string;
headline?: string;
text?: string;
queryToken?: QueryTokenDTO;
expirationDate?: string;
}

View File

@@ -0,0 +1,5 @@
export interface TargetDTO {
area?: string;
branches?: { id: number }[];
users?: { id: number }[];
}

View File

@@ -1,5 +1,8 @@
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { SignalrHub, SignalRHubOptions } from '@core/signalr';
import { merge, of } from 'rxjs';
import { filter, tap } from 'rxjs/operators';
import { EnvelopeDTO, MessageBoardItemDTO } from './defs';
export const NOTIFICATIONS_HUB_OPTIONS = new InjectionToken<SignalRHubOptions>('hub.notifications.options');
@@ -9,5 +12,22 @@ export class NotificationsHub extends SignalrHub {
super(options);
}
notifications$ = this.listen<{ data: any[] }>('messageBoard');
notifications$ = merge(
of(this._getNotifications()).pipe(filter((f) => !!f)),
this.listen<EnvelopeDTO<MessageBoardItemDTO[]>>('messageBoard')
).pipe(tap((data) => this._storeNotifactions(data)));
private _storeNotifactions(data: EnvelopeDTO<MessageBoardItemDTO[]>) {
if (data) {
localStorage.setItem('NOTIFICATIONS_BOARD', JSON.stringify(data));
}
}
private _getNotifications(): EnvelopeDTO<MessageBoardItemDTO[]> {
const stringData = localStorage.getItem('NOTIFICATIONS_BOARD');
if (stringData) {
return JSON.parse(stringData);
}
return undefined;
}
}