瀏覽代碼

fix: dispatch to pubsub when skipping songs from slave

Fela Maslen 5 年之前
父節點
當前提交
f8f5756a6b
共有 2 個文件被更改,包括 73 次插入0 次删除
  1. 58 0
      gmus-web/src/effects/effects.spec.ts
  2. 15 0
      gmus-web/src/effects/effects.ts

+ 58 - 0
gmus-web/src/effects/effects.spec.ts

@@ -5,9 +5,11 @@ import {
   masterSet,
   playPaused,
   seeked,
+  songInfoFetched,
   stateSet,
 } from '../actions';
 import { GlobalState, initialState } from '../reducer';
+import { Song } from '../types';
 import { MusicPlayer } from '../types/state';
 import { globalEffects } from './effects';
 
@@ -167,4 +169,60 @@ describe(globalEffects.name, () => {
       });
     });
   });
+
+  describe(ActionTypeLocal.SongInfoFetched, () => {
+    const statePriorMaster: GlobalState = {
+      ...initialState,
+      player: {
+        songId: 123,
+        playing: true,
+        currentTime: 83,
+        seekTime: 5,
+        master: 'some-master-client',
+      },
+      myClientName: 'some-master-client',
+    };
+
+    const action = songInfoFetched({ id: 185 } as Song, true);
+
+    describe('when the client is master', () => {
+      it('should return null', () => {
+        expect.assertions(1);
+        expect(globalEffects(statePriorMaster, playPaused())).toBeNull();
+      });
+    });
+
+    describe('when the client is a slave', () => {
+      const stateSlave: GlobalState = {
+        ...statePriorMaster,
+        myClientName: 'some-slave-client',
+      };
+
+      it('should return a StateSet action informing other clients of the changed song', () => {
+        expect.assertions(1);
+        const result = globalEffects(stateSlave, action);
+
+        expect(result).toStrictEqual<ActionStateSetRemote>({
+          type: ActionTypeRemote.StateSet,
+          payload: {
+            songId: 185,
+            playing: true,
+            currentTime: 0,
+            seekTime: 0,
+            master: 'some-master-client',
+          },
+        });
+      });
+
+      describe('when the action is not set to replace the current song', () => {
+        const actionNoReplace = songInfoFetched({ id: 185 } as Song, false);
+
+        it('should return null', () => {
+          expect.assertions(1);
+          const result = globalEffects(stateSlave, actionNoReplace);
+          expect(result).toBeNull();
+        });
+      });
+    });
+  });
 });

+ 15 - 0
gmus-web/src/effects/effects.ts

@@ -50,6 +50,21 @@ export function globalEffects(prevState: GlobalState, action: LocalAction): Remo
         },
       };
 
+    case ActionTypeLocal.SongInfoFetched:
+      if (isMaster(prevState) || !action.payload.replace) {
+        return null;
+      }
+      return {
+        type: ActionTypeRemote.StateSet,
+        payload: {
+          ...prevState.player,
+          songId: action.payload.song?.id ?? null,
+          playing: !!action.payload.song,
+          currentTime: 0,
+          seekTime: 0,
+        },
+      };
+
     default:
       return null;
   }