Procházet zdrojové kódy

feat: only update current time on time update

Fela Maslen před 4 roky
rodič
revize
a83da194d3

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

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

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

@@ -33,6 +33,7 @@ describe(globalReducer.name, () => {
         const actionFromOtherClient: ActionStateSetRemote = {
           type: ActionTypeRemote.StateSet,
           fromClient: 'other-client',
+          priority: 0,
           payload: {
             songId: 123,
             playing: true,
@@ -60,6 +61,23 @@ describe(globalReducer.name, () => {
             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', () => {

+ 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));
 }
 
+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 {
-  const nextPlayer = action.payload ?? nullPlayer;
+  const nextPlayer = getNextRemotePlayer(state.player, action);
   const seekTime = shouldSetSeekTime(state, action) ? nextPlayer.seekTime : -1;
 
   const nextPlayerWithSeekTime: MusicPlayer = { ...nextPlayer, seekTime };