refactor(core): log viewer behavior

This commit is contained in:
Wroud
2020-05-15 13:52:52 +03:00
parent 4d5cb2eba9
commit 51d823bff4
@@ -20,7 +20,11 @@ import { ILogEntry } from './ILogEntry';
@injectable()
export class LogViewerService {
@observable isActive = false;
@observable _isActive = false;
get isActive() {
return this._isActive;
}
@observable private log: ILogEntry[] = [];
private interval: any = null;
@@ -35,15 +39,10 @@ export class LogViewerService {
}
toggle() {
if (this.isActive) {
if (this._isActive) {
this.stopLog();
this.isActive = false;
} else {
if (!this.isLogViewerAvailable()) {
throw new Error('Access denied');
}
this.startLog();
this.isActive = true;
}
}
@@ -52,9 +51,13 @@ export class LogViewerService {
}
async startLog() {
if (this.interval) {
if (this._isActive) {
return;
}
if (!this.isLogViewerAvailable()) {
throw new Error('Access denied');
}
this._isActive = true;
await this.updateLog();
const refreshInterval = this.coreSettingsService.settings.getValue('app.logViewer.refreshInterval');
this.interval = setInterval(() => {
@@ -67,6 +70,7 @@ export class LogViewerService {
clearInterval(this.interval);
this.interval = null;
}
this._isActive = false;
}
@action
@@ -74,15 +78,6 @@ export class LogViewerService {
this.log = [];
}
@action
addNewEntries(entries: ILogEntry[]) {
this.log.unshift(...entries.reverse());
const maxLogEntries = this.coreSettingsService.settings.getValue('app.logViewer.maxLogEntries');
if (this.log.length > maxLogEntries) {
this.log.splice(maxLogEntries, this.log.length - maxLogEntries);
}
}
async loadLog(): Promise<ILogEntry[]> {
const maxLogEntries = this.coreSettingsService.settings.getValue('app.logViewer.maxLogEntries');
const { log } = await this.graphQLService.gql.readSessionLog({
@@ -96,6 +91,15 @@ export class LogViewerService {
return entries;
}
@action
private addNewEntries(entries: ILogEntry[]) {
this.log.unshift(...entries.reverse());
const maxLogEntries = this.coreSettingsService.settings.getValue('app.logViewer.maxLogEntries');
if (this.log.length > maxLogEntries) {
this.log.splice(maxLogEntries, this.log.length - maxLogEntries);
}
}
private async updateLog() {
if (!this.isLogViewerAvailable()) {
return;
@@ -110,8 +114,8 @@ export class LogViewerService {
}
private stopIfHasNoPermission() {
if (this.isActive && !this.isLogViewerAvailable()) {
this.toggle();
if (this._isActive && !this.isLogViewerAvailable()) {
this.stopLog();
}
}