src/lib/service/system-status/system-status.service.ts
Methods |
constructor(eventService: EventService, backend: BackendService, appCache: AppCacheService, ngZone: NgZone)
|
|||||||||||||||
Parameters :
|
init | ||||||
init(pollIntervalMilliSeconds: )
|
||||||
Parameters :
Returns :
void
|
stop |
stop()
|
Returns :
void
|
import {Injectable, NgZone} from '@angular/core';
import {EventService} from '../events/event.service';
import {BackendService} from '../backend/backend.service';
import {EnaioEvent} from '../events/events';
import {Subject, Subscription, timer} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {AppCacheService} from '../cache/app-cache.service';
import {ISystemStatus} from './system.status.interface';
import {SystemStatusEnum} from './system.status.enum';
@Injectable({
providedIn: 'root'
})
export class SystemStatusService {
private currentStatus: ISystemStatus;
private statusCacheKey = 'eo.framework.cache.system.status';
private hasBeenStopped: boolean;
private pollingStopTrigger$ = new Subject<void>();
constructor(private eventService: EventService, private backend: BackendService, private appCache: AppCacheService, private ngZone: NgZone) {
this.appCache
.getItem(this.statusCacheKey)
.subscribe(status => this.currentStatus = status);
}
init(pollIntervalMilliSeconds = -1) {
if (pollIntervalMilliSeconds > -1) {
this.pollingStopTrigger$ = new Subject<void>();
this.startPolling(pollIntervalMilliSeconds);
}
}
stop() {
this.hasBeenStopped = true;
this.pollingStopTrigger$.next();
this.pollingStopTrigger$.complete();
}
private startPolling(pollIntervalMilliSeconds) {
this.ngZone.runOutsideAngular(() => {
let requestSubscription: Subscription;
timer(0, pollIntervalMilliSeconds)
.pipe(takeUntil(this.pollingStopTrigger$))
.subscribe(() => {
this.ngZone.run(() => {
// temporary fix while status service is disabled
this.eventService.trigger(EnaioEvent.SYSTEM_STATUS_INBOX_CHANGED, {});
});
// if (requestSubscription) {
// requestSubscription.unsubscribe();
// }
// requestSubscription = this.backend
// .get('/status', this.backend.getStatusBase())
// .subscribe((res: ISystemStatus) => {
// this.handleStatusResult(res);
// }, err => {
// this.handleStatusError(err);
// });
});
});
}
private handleStatusResult(res: ISystemStatus) {
const eventsToBeTriggered: { type: string, data: any }[] = [];
Object.keys(res).forEach(val => {
if (this.statusCheck(res, val)) {
eventsToBeTriggered.push({
type: this.statusEvent(val),
data: res
});
}
});
if (eventsToBeTriggered.length || this.hasBeenStopped) {
// if polling has been stopped and get activated again, we need
// angular to detect changes immediately
this.ngZone.run(() => {
eventsToBeTriggered.forEach(e => {
this.eventService.trigger(e.type, e.data);
});
});
}
if (eventsToBeTriggered.length || !this.currentStatus) {
this.currentStatus = res;
this.appCache.setItem(this.statusCacheKey, res);
}
}
private handleStatusError(err) {
// execute within zone so components are able to be checked
// Getting a 401 would otherwise not trigger a route change (redirect to login state)
this.ngZone.run(() => {
setTimeout(_ => {
this.stop();
});
});
}
private statusEvent(val: string): string {
return EnaioEvent[Object.keys(SystemStatusEnum).find(key => SystemStatusEnum[key] === val)];
}
private statusCheck(res: ISystemStatus, statusType: string): boolean {
return this.currentStatus && (this.currentStatus[statusType] < res[statusType]);
}
}