Merge branch 'feature/SUPPORT-9130_socket_msg_unsubscribe' into develop
This commit is contained in:
commit
e5280c1a3f
3 changed files with 39 additions and 22 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import {ChangeDetectionStrategy, ChangeDetectorRef, Component} from "@angular/core";
|
||||
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy} from "@angular/core";
|
||||
import {AuthorizationService} from "../service/authorization.service";
|
||||
import {WebsocketService} from "../websocket/websocket.service";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
|
||||
@Component({
|
||||
moduleId: module.id,
|
||||
|
|
@ -7,18 +9,35 @@ import {AuthorizationService} from "../service/authorization.service";
|
|||
templateUrl: "../../../../../src/resources/template/app/component/app_header.html",
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
})
|
||||
export class AppHeaderComponent {
|
||||
export class AppHeaderComponent implements OnDestroy {
|
||||
|
||||
name: string;
|
||||
realm: string;
|
||||
|
||||
constructor(protected authService: AuthorizationService,
|
||||
protected cd: ChangeDetectorRef) {
|
||||
constructor(protected authService: AuthorizationService, protected cd: ChangeDetectorRef,
|
||||
protected websocketService: WebsocketService, protected httpClient: HttpClient) {
|
||||
authService.onSessionUpdate
|
||||
.subscribe(session => {
|
||||
this.name = session.name;
|
||||
this.realm = session.realm;
|
||||
cd.markForCheck()
|
||||
cd.markForCheck();
|
||||
|
||||
if (this.authService.hasRole('security_administrator')) {
|
||||
this.websocketService.subscribe(({data}) => {
|
||||
let parsedObj = JSON.parse(data);
|
||||
|
||||
if (parsedObj && parsedObj.traceId) {
|
||||
if (parsedObj.className === 'update' || parsedObj.className === 'processError') {
|
||||
this.httpClient.put('status', parsedObj).toPromise()
|
||||
.catch(err => console.log('failed to update application status', err));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.websocketService.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import {Injectable} from "@angular/core";
|
||||
import {Subject} from "rxjs";
|
||||
import {HttpClient} from "@angular/common/http";
|
||||
import {WebsocketService} from "../websocket/websocket.service";
|
||||
|
||||
export interface UserSession {
|
||||
userId: string,
|
||||
|
|
@ -18,7 +17,7 @@ export class AuthorizationService {
|
|||
|
||||
public onSessionUpdate: Subject<UserSession> = new Subject<UserSession>();
|
||||
|
||||
constructor(protected httpClient: HttpClient, protected websocketService: WebsocketService) {}
|
||||
constructor(protected httpClient: HttpClient) {}
|
||||
|
||||
public getCurrentSession(): Promise<any> {
|
||||
if (this.session) return new Promise(resolve => resolve(this.session));
|
||||
|
|
@ -27,19 +26,6 @@ export class AuthorizationService {
|
|||
.then((session: UserSession) => {
|
||||
this.session = session;
|
||||
this.onSessionUpdate.next(session);
|
||||
|
||||
if (this.hasRole('security_administrator')) {
|
||||
this.websocketService.listen(({data}) => {
|
||||
let parsedObj = JSON.parse(data);
|
||||
|
||||
if (parsedObj && parsedObj.traceId) {
|
||||
if (parsedObj.className === 'update' || parsedObj.className === 'processError') {
|
||||
this.httpClient.put('status', parsedObj).toPromise()
|
||||
.catch(err => console.log('failed to update application status', err));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return session;
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,18 @@ import {Injectable} from "@angular/core";
|
|||
@Injectable({providedIn: 'root'})
|
||||
export class WebsocketService {
|
||||
|
||||
public listen(fn){
|
||||
private initialData;
|
||||
|
||||
public subscribe(fn: Function): void {
|
||||
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
|
||||
const data = property.get;
|
||||
this.initialData = data;
|
||||
|
||||
// wrapper that replaces getter
|
||||
function lookAtMessage() {
|
||||
let socket = this.currentTarget instanceof WebSocket;
|
||||
|
||||
if (!socket) {
|
||||
if (!socket || !this.currentTarget.url.endsWith('notifier.message.send.push')) {
|
||||
return data.call(this);
|
||||
}
|
||||
let msg = data.call(this);
|
||||
|
|
@ -22,4 +25,13 @@ export class WebsocketService {
|
|||
property.get = lookAtMessage;
|
||||
Object.defineProperty(MessageEvent.prototype, "data", property);
|
||||
}
|
||||
|
||||
public unsubscribe(): void {
|
||||
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
|
||||
|
||||
if (this.initialData) {
|
||||
property.get = this.initialData;
|
||||
Object.defineProperty(MessageEvent.prototype, "data", property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue