Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit b4cc270

Browse files
author
cesmec
committed
Added realtime updates
1 parent e335ceb commit b4cc270

File tree

2 files changed

+66
-34
lines changed

2 files changed

+66
-34
lines changed

src/app/pages/betterrepl/betterrepl.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ <h2 class="mdl-card__title-text">Start / Stop / Log</h2>
284284
Show log
285285
</button>
286286
</div>
287-
<div id="instanceLog" *ngIf="instanceLogOutput?.length > 0">{{ instanceLogOutput }}</div>
287+
<div #instanceLog id="instanceLog" style="white-space: pre-wrap" *ngIf="instanceLogOutput?.length > 0">{{ instanceLogOutput.join("\n") }}</div>
288288
</base-card-body>
289289
</base-card>
290290
</div>

src/app/pages/betterrepl/betterrepl.component.ts

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, HostBinding} from "@angular/core";
1+
import {Component, HostBinding, ViewChild, ElementRef} from "@angular/core";
22
import {UpgradableComponent} from "theme/components/upgradable";
33
import {
44
ResultMessage,
@@ -22,7 +22,10 @@ import {
2222
PluginLogMessageDTO
2323
} from "chatoverflow-api";
2424
import {CryptoService} from "../../../crypto.service";
25-
import {interval} from "rxjs";
25+
26+
interface PluginInstanceWithLog extends PluginInstance {
27+
log?: string[];
28+
}
2629

2730
@Component({
2831
selector: 'better-repl',
@@ -31,6 +34,7 @@ import {interval} from "rxjs";
3134
})
3235
export class BetterREPLComponent extends UpgradableComponent {
3336
@HostBinding('class.mdl-grid') private readonly mdlGrid = true;
37+
@ViewChild('instanceLog') private readonly instanceLog: ElementRef<HTMLDivElement>;
3438

3539
private lastRequestCommand = "...";
3640
private lastRequestMessage = "Please send a request to the server...";
@@ -44,7 +48,7 @@ export class BetterREPLComponent extends UpgradableComponent {
4448
private pluginTypes: Array<PluginType>;
4549

4650
private connectorKeys: Array<ConnectorKey>;
47-
private pluginInstances: Array<PluginInstance>;
51+
private pluginInstances: Array<PluginInstanceWithLog>;
4852

4953
private instanceLogOutput: Array<string>;
5054
private instanceRequirements: Array<Requirement>;
@@ -65,9 +69,8 @@ export class BetterREPLComponent extends UpgradableComponent {
6569
private changeReqTypeValue = "";
6670
private changeReqValueValue = "";
6771

68-
private pingpongCounter = interval(5000);
69-
private pingpongStarted = false;
70-
private pingPongSubscriber = null;
72+
private eventsStarted = false;
73+
private eventSource: EventSource = null;
7174

7275
constructor(private configService: ConfigService, private typeService: TypeService,
7376
private connectorService: ConnectorService, private instanceService: InstanceService,
@@ -155,45 +158,74 @@ export class BetterREPLComponent extends UpgradableComponent {
155158
this.lastPassword = password;
156159
}
157160
this.reloadEverything(!response.success);
158-
this.handlePingpong(true);
161+
this.handleEvents(true);
159162
}, error => {
160163
this.logGenericError("postLogin");
161164
this.reloadEverything(true);
162165
});
163166
}
164167

165-
handlePingpong(start: boolean) {
168+
handleEvents(start: boolean) {
169+
if (!("EventSource" in window)) {
170+
console.warn("Event source is not supported");
171+
return;
172+
}
173+
166174
if (start) {
167-
if (!this.pingpongStarted) {
168-
this.pingPongSubscriber = this.pingpongCounter.subscribe(n => this.pingpong());
169-
this.pingpongStarted = true;
175+
if (!this.eventsStarted) {
176+
//todo find a better way to get the basePath
177+
this.eventSource = new EventSource(`${this.configService["basePath"]}/events?authKey=${this.authKey}`);
178+
this.eventSource.onerror = () => {
179+
console.log("Lost connection. Trying to reconnect...");
180+
setTimeout(() => this.login(this.lastPassword), 1000);
181+
};
182+
this.eventSource.addEventListener("instance", (e: MessageEvent) => {
183+
const { name, action, data } = JSON.parse(e.data) as { name: string, action: string, data?: { message: string, timestamp: string } };
184+
const instance = this.pluginInstances.find(i => i.instanceName === name);
185+
if (!instance)
186+
return;
187+
188+
switch (action) {
189+
case "start":
190+
instance.isRunning = true;
191+
break;
192+
case "stop":
193+
instance.isRunning = false;
194+
break;
195+
case "log":
196+
if (data) {
197+
if (!instance.log)
198+
instance.log = [];
199+
200+
instance.log.push(`${new Date(data.timestamp).toLocaleTimeString()} - ${data.message}`);
201+
if (name === this.instanceNameSSValue) {
202+
this.instanceLogOutput = instance.log;
203+
if (this.instanceLog && this.instanceLog.nativeElement) {
204+
const element = this.instanceLog.nativeElement;
205+
if (element.scrollTop + element.clientHeight === element.scrollHeight)
206+
setTimeout(() => element.scrollTop = element.scrollHeight, 0);
207+
}
208+
} else if (this.instanceLogOutput === instance.log) {
209+
this.instanceLogOutput = [];
210+
}
211+
}
212+
break;
213+
}
214+
});
215+
this.eventSource.addEventListener("close", () => {
216+
this.eventSource.close();
217+
this.eventSource = null;
218+
});
219+
this.eventsStarted = true;
170220
}
171221
} else {
172-
if (this.pingPongSubscriber != null) {
173-
this.pingPongSubscriber.unsubscribe();
222+
if (this.eventSource) {
223+
this.eventSource.close();
174224
}
175-
this.pingpongStarted = false;
225+
this.eventsStarted = false;
176226
}
177227
}
178228

179-
pingpong() {
180-
this.configService.postPing(this.authKey).subscribe((response: ResultMessage) => {
181-
if (response.success) {
182-
// Pong
183-
} else {
184-
this.reloadEverything(true);
185-
}
186-
}, error => {
187-
if (error.status == 401 || error.status == 400) {
188-
// Try to login again
189-
this.login(this.lastPassword);
190-
191-
} else if (error.status == 0) {
192-
this.reloadEverything(true);
193-
}
194-
})
195-
}
196-
197229
register(password: string) {
198230
this.configService.postRegister({password: password}).subscribe((response: ResultMessage) => {
199231
this.logResultMessage("postRegister", response);
@@ -202,7 +234,7 @@ export class BetterREPLComponent extends UpgradableComponent {
202234
this.reloadEverything(false);
203235
}
204236
this.reloadEverything(!response.success);
205-
this.handlePingpong(true);
237+
this.handleEvents(true);
206238
}, error => {
207239
this.logGenericError("postRegister");
208240
this.reloadEverything(true);

0 commit comments

Comments
 (0)