From faedd266816f8d35d9a010e6b6533566267b55bc Mon Sep 17 00:00:00 2001
From: jojohoch <joachim.hoch@iqb.hu-berlin.de>
Date: Tue, 16 Aug 2022 17:16:23 +0200
Subject: [PATCH] [player] Avoid sending change notification twice in
 PlayerStateDirective

- Refactor subscriptions for verona commands
---
 .../app/directives/player-state.directive.ts  | 27 ++++++++++---------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/projects/player/src/app/directives/player-state.directive.ts b/projects/player/src/app/directives/player-state.directive.ts
index 366af7045..68d71f751 100644
--- a/projects/player/src/app/directives/player-state.directive.ts
+++ b/projects/player/src/app/directives/player-state.directive.ts
@@ -1,8 +1,12 @@
-import { Directive, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
+import {
+  Directive, Input, OnChanges, OnInit, SimpleChanges
+} from '@angular/core';
 import {
   PlayerState, RunningState
 } from 'player/modules/verona/models/verona';
-import { BehaviorSubject, Subject } from 'rxjs';
+import {
+  BehaviorSubject, map, merge, Subject
+} from 'rxjs';
 import { VeronaSubscriptionService } from 'player/modules/verona/services/verona-subscription.service';
 import { VeronaPostService } from 'player/modules/verona/services/verona-post.service';
 import { takeUntil } from 'rxjs/operators';
@@ -25,7 +29,6 @@ export class PlayerStateDirective implements OnInit, OnChanges {
 
   ngOnInit(): void {
     this.initSubscriptions();
-    this.sendVopStateChangedNotification();
   }
 
   ngOnChanges(changes: SimpleChanges): void {
@@ -35,15 +38,15 @@ export class PlayerStateDirective implements OnInit, OnChanges {
   }
 
   private initSubscriptions(): void {
-    this.veronaSubscriptionService.vopContinueCommand
-      .pipe(takeUntil(this.ngUnsubscribe))
-      .subscribe(() => this.setAndSendRunningState(true));
-    this.veronaSubscriptionService.vopStopCommand
-      .pipe(takeUntil(this.ngUnsubscribe))
-      .subscribe(()  => this.setAndSendRunningState(false));
-    this.veronaSubscriptionService.vopGetStateRequest
-      .pipe(takeUntil(this.ngUnsubscribe))
-      .subscribe(message => this.setAndSendRunningState((!message.stop && this.state === 'running')));
+    merge(
+      this.veronaSubscriptionService.vopContinueCommand
+        .pipe(map(() => true)),
+      this.veronaSubscriptionService.vopStopCommand
+        .pipe(map(() => false)),
+      this.veronaSubscriptionService.vopGetStateRequest
+        .pipe(map(message => (!message.stop && this.state === 'running')))
+    ).pipe(takeUntil(this.ngUnsubscribe))
+      .subscribe(isRunning => this.setAndSendRunningState(isRunning));
   }
 
   private get state(): RunningState {
-- 
GitLab