Newer
Older
import {Inject, Injectable, OnDestroy} from '@angular/core';
import {of, Subject, Subscription, timer} from 'rxjs';
import {Command, commandKeywords, isKnownCommand, TestControllerState} from './test-controller.interfaces';
import {TestControllerService} from './test-controller.service';
import {
concatMap,
distinctUntilChanged,
filter,
ignoreElements,
map,
mergeMap,
startWith,
switchMap, tap
} from 'rxjs/operators';
import {WebsocketBackendService} from '../shared/websocket-backend.service';
import {HttpClient} from '@angular/common/http';
type TestStartedOrStopped = 'started' | 'terminated' | '';
@Injectable({
providedIn: 'root'
})
export class CommandService extends WebsocketBackendService<Command[]> implements OnDestroy {
public command$: Subject<Command> = new Subject<Command>();

paf
committed
protected initialData = [];
protected pollingEndpoint = '';
protected pollingInterval = 5000;
protected wsChannelName = 'commands';
private commandReceived$: Subject<Command> = new Subject<Command>();
private commandSubscription: Subscription;
private testStartedSubscription: Subscription;
private executedCommandIds: number[] = [];

paf
committed
constructor (
@Inject('IS_PRODUCTION_MODE') public isProductionMode,
private tcs: TestControllerService,
@Inject('SERVER_URL') serverUrl: string,

paf
committed
protected http: HttpClient
super(serverUrl, http);
if (!this.isProductionMode) {
this.setUpGlobalCommandsForDebug();
}
// as services don't have a OnInit Hook (see: https://v9.angular.io/api/core/OnInit) we subscribe here
this.subscribeReceivedCommands();
private static commandToString(command: Command): string {
return `[${command.id}] ${command.keyword} ` + command.arguments.join(' ');
private static testStartedOrStopped(testStatus: TestControllerState): TestStartedOrStopped {
if ((testStatus === TestControllerState.RUNNING) || (testStatus === TestControllerState.PAUSED)) {
if ((testStatus === TestControllerState.FINISHED) || (testStatus === TestControllerState.ERROR)) {
return 'terminated';
}
return '';
}
// services are normally meant to live forever, so unsubscription *should* be unnecessary
// this unsubscriptions are only for the case, the project's architecture will be changed dramatically once
// while not having a OnInit-hook services *do have* an OnDestroy-hook (see: https://v9.angular.io/api/core/OnDestroy)
ngOnDestroy() {
this.commandSubscription.unsubscribe();
private subscribeReceivedCommands() {
.pipe(
filter((command: Command) => (this.executedCommandIds.indexOf(command.id) < 0)),
concatMap((command: Command) => timer(1000).pipe(ignoreElements(), startWith(command))), // min delay between items
mergeMap((command: Command) => {
console.log('try to execute' + CommandService.commandToString(command));
return this.http.patch(`${this.serverUrl}test/${this.tcs.testId}/command/${command.id}/executed`, {})
.pipe(
map(() => command),
tap(cmd => this.executedCommandIds.push(cmd.id))
);
})
).subscribe(command => this.command$.next(command));
private subscribeTestStarted() {
if (typeof this.testStartedSubscription !== 'undefined') {
this.testStartedSubscription.unsubscribe();
this.testStartedSubscription = this.tcs.testStatus$
map(CommandService.testStartedOrStopped),
filter(testStartedOrStopped => testStartedOrStopped !== ''),
map(testStartedOrStopped => (testStartedOrStopped === 'started') ? `test/${this.tcs.testId}/commands` : ''),
filter(newPollingEndpoint => newPollingEndpoint !== this.pollingEndpoint),
switchMap((pollingEndpoint: string) => {
this.pollingEndpoint = pollingEndpoint;
if (this.pollingEndpoint) {
return this.observeEndpointAndChannel();
} else {
this.cutConnection();
return of([]);
}
}),
switchMap(commands => of(...commands))

paf
committed
).subscribe(this.commandReceived$);
private setUpGlobalCommandsForDebug() {
window['tc'] = {};
commandKeywords.forEach((keyword: string) => {

paf
committed
window['tc'][keyword] = (args) => {this.commandFromTerminal(keyword, args); };
});

paf
committed
private commandFromTerminal(keyword: string, args: string[]): void {
args = (typeof args === 'undefined') ? [] : args;

paf
committed
const id = Math.round(Math.random() * -10000000);
const command = {keyword, arguments: args, id, timestamp: Date.now()};
if (!isKnownCommand(keyword)) {
console.warn(`Unknown command: ` + CommandService.commandToString(command));
return;
}
this.command$.next(command);
// this.commandReceived$.next(command);