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 { export class ProcessCommand {
command: string; command: string;
process_name: string;
} }

View File

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