Refactor ProcessManagerService to be stateless

This commit is contained in:
Kristóf Tóth 2018-02-27 15:55:23 +01:00
parent 21043ee74e
commit ffcc608a97
2 changed files with 15 additions and 13 deletions

View File

@ -1,3 +1,4 @@
export class ProcessCommand {
command: string;
process_name: string;
}

View File

@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { WebSocketService } from './websocket.service';
import { ProcessCommand } from './processcommand';
import { filter } from 'rxjs/operators';
@Injectable()
@ -11,31 +12,31 @@ export class ProcessManagerService {
constructor(private webSocketService: WebSocketService) {}
init(process_name: string) {
this.process_name = process_name;
init() {
this.webSocketService.connect();
}
subscribeCallback(callback: (event: any) => void) {
this.webSocketService.observeKey<ProcessCommand>(this.key).subscribe(callback);
subscribeCallback(process_name: string, callback: (event: any) => void) {
this.webSocketService.observeKey<ProcessCommand>(this.key)
.pipe(filter(message => message.data.process_name === process_name)).subscribe(callback);
}
sendCommand(command: string, callback?: (event: any) => void) {
sendCommand(command: string, process_name: string, callback?: (event: any) => void) {
if (callback) {
this.subscribeCallback(callback);
this.subscribeCallback(process_name, callback);
}
this.webSocketService.send(this.key, {'command': command});
this.webSocketService.send(this.key, {'command': command, 'process_name': process_name});
}
startProcess(callback?: (event: any) => void) {
this.sendCommand('start', callback);
startProcess(process_name: string, callback?: (event: any) => void) {
this.sendCommand('start', process_name, callback);
}
stopProcess(callback?: (event: any) => void) {
this.sendCommand('stop', callback);
stopProcess(process_name: string, callback?: (event: any) => void) {
this.sendCommand('stop', process_name, callback);
}
restartProcess(callback?: (event: any) => void) {
this.sendCommand('restart', callback);
restartProcess(process_name: string, callback?: (event: any) => void) {
this.sendCommand('restart', process_name, callback);
}
}