瀏覽代碼

feat: only update current time on time update

Fela Maslen 4 年之前
父節點
當前提交
a83da194d3
共有 3 個文件被更改,包括 32 次插入2 次删除
  1. 1 1
      gmus-web/src/components/app.tsx
  2. 18 0
      gmus-web/src/reducer/reducer.spec.ts
  3. 13 1
      gmus-web/src/reducer/reducer.ts

+ 1 - 1
gmus-web/src/components/app.tsx

@@ -45,7 +45,7 @@ export const App: React.FC<Props> = ({
 
 
   const onTimeUpdate = useCallback(
   const onTimeUpdate = useCallback(
     (currentTime: number): void => {
     (currentTime: number): void => {
-      dispatch(stateSet({ currentTime }));
+      dispatch(stateSet({ currentTime }, 1));
     },
     },
     [dispatch],
     [dispatch],
   );
   );

+ 18 - 0
gmus-web/src/reducer/reducer.spec.ts

@@ -33,6 +33,7 @@ describe(globalReducer.name, () => {
         const actionFromOtherClient: ActionStateSetRemote = {
         const actionFromOtherClient: ActionStateSetRemote = {
           type: ActionTypeRemote.StateSet,
           type: ActionTypeRemote.StateSet,
           fromClient: 'other-client',
           fromClient: 'other-client',
+          priority: 0,
           payload: {
           payload: {
             songId: 123,
             songId: 123,
             playing: true,
             playing: true,
@@ -60,6 +61,23 @@ describe(globalReducer.name, () => {
             shuffleMode: false,
             shuffleMode: false,
           });
           });
         });
         });
+
+        describe('when the priority was 1', () => {
+          const actionWithLowerPriority: ActionStateSetRemote = {
+            ...actionFromOtherClient,
+            priority: 1,
+          };
+
+          it('should only update the currentTime', () => {
+            expect.assertions(1);
+            const result = globalReducer(stateMaster, actionWithLowerPriority);
+
+            expect(result.player).toStrictEqual<MusicPlayer>({
+              ...stateMaster.player,
+              currentTime: 75,
+            });
+          });
+        });
       });
       });
 
 
       describe('and the action came from ourselves', () => {
       describe('and the action came from ourselves', () => {

+ 13 - 1
gmus-web/src/reducer/reducer.ts

@@ -37,8 +37,20 @@ function shouldSetSeekTime(state: GlobalState, action: ActionStateSetRemote): bo
   return willBeMaster(state, action) && !(isMaster(state) && isFromOurselves(state, action));
   return willBeMaster(state, action) && !(isMaster(state) && isFromOurselves(state, action));
 }
 }
 
 
+function getNextRemotePlayer(
+  existingPlayer: MusicPlayer,
+  action: ActionStateSetRemote,
+): MusicPlayer {
+  if (!action.payload) {
+    return action.priority > 0 ? existingPlayer : nullPlayer;
+  }
+  return action.priority > 0
+    ? { ...existingPlayer, currentTime: action.payload.currentTime }
+    : action.payload;
+}
+
 function onRemoteStateSet(state: GlobalState, action: ActionStateSetRemote): GlobalState {
 function onRemoteStateSet(state: GlobalState, action: ActionStateSetRemote): GlobalState {
-  const nextPlayer = action.payload ?? nullPlayer;
+  const nextPlayer = getNextRemotePlayer(state.player, action);
   const seekTime = shouldSetSeekTime(state, action) ? nextPlayer.seekTime : -1;
   const seekTime = shouldSetSeekTime(state, action) ? nextPlayer.seekTime : -1;
 
 
   const nextPlayerWithSeekTime: MusicPlayer = { ...nextPlayer, seekTime };
   const nextPlayerWithSeekTime: MusicPlayer = { ...nextPlayer, seekTime };