Explorar o código

feat: dispatch masterSet action instead of stateSet, and remove 5 second timer

Fela Maslen %!s(int64=4) %!d(string=hai) anos
pai
achega
eb88d24789

+ 0 - 2
gmus-web/src/constants/system.ts

@@ -1,3 +1 @@
 export const socketKeepaliveTimeout = 20000;
-
-export const masterStateUpdateTimeout = 5000;

+ 2 - 81
gmus-web/src/hooks/master.spec.tsx

@@ -1,8 +1,7 @@
 import { act, render, RenderResult } from '@testing-library/react';
 import React from 'react';
 
-import { masterSet, stateSet } from '../actions';
-import { masterStateUpdateTimeout } from '../constants/system';
+import { masterSet } from '../actions';
 import { DispatchContext, StateContext } from '../context/state';
 import { GlobalState, initialState, nullPlayer } from '../reducer';
 
@@ -46,7 +45,7 @@ describe(useMaster.name, () => {
       });
 
       expect(dispatch).toHaveBeenCalledTimes(1);
-      expect(dispatch).toHaveBeenCalledWith(stateSet({ master: 'my-client-name' }));
+      expect(dispatch).toHaveBeenCalledWith(masterSet('my-client-name'));
 
       unmount();
       jest.useRealTimers();
@@ -143,82 +142,4 @@ describe(useMaster.name, () => {
       });
     });
   });
-
-  describe('when the client is master', () => {
-    const stateMaster: GlobalState = {
-      ...initialState,
-      myClientName: 'the-master-client',
-      clientList: [{ name: 'the-master-client', lastPing: 0 }],
-      player: {
-        ...nullPlayer,
-        master: 'the-master-client',
-      },
-    };
-
-    it('should continually refresh the server with the current state', () => {
-      expect.assertions(6);
-      const clock = jest.useFakeTimers();
-      const { unmount } = setup(stateMaster);
-      act(() => {
-        clock.runOnlyPendingTimers();
-      });
-
-      dispatch.mockClear();
-
-      act(() => {
-        clock.runTimersToTime(masterStateUpdateTimeout - 1);
-      });
-
-      expect(dispatch).toHaveBeenCalledTimes(0);
-      act(() => {
-        clock.runTimersToTime(1);
-      });
-
-      expect(dispatch).toHaveBeenCalledTimes(1);
-      expect(dispatch).toHaveBeenCalledWith(stateSet());
-
-      dispatch.mockClear();
-      expect(dispatch).toHaveBeenCalledTimes(0);
-
-      act(() => {
-        clock.runTimersToTime(masterStateUpdateTimeout);
-      });
-
-      expect(dispatch).toHaveBeenCalledTimes(1);
-      expect(dispatch).toHaveBeenCalledWith(stateSet());
-
-      unmount();
-      jest.useRealTimers();
-    });
-  });
-
-  describe('when the client is a slave', () => {
-    const stateSlave: GlobalState = {
-      ...initialState,
-      myClientName: 'a-slave-client',
-      clientList: [
-        { name: 'the-master-client', lastPing: 0 },
-        { name: 'a-slave-client', lastPing: 0 },
-      ],
-      player: {
-        ...nullPlayer,
-        master: 'the-master-client',
-      },
-    };
-
-    it('should not send state updates periodically', () => {
-      expect.assertions(1);
-      const clock = jest.useFakeTimers();
-      const { unmount } = setup(stateSlave);
-
-      act(() => {
-        clock.runTimersToTime(masterStateUpdateTimeout);
-      });
-
-      expect(dispatch).not.toHaveBeenCalled();
-
-      unmount();
-      jest.useRealTimers();
-    });
-  });
 });

+ 3 - 17
gmus-web/src/hooks/master.ts

@@ -1,7 +1,6 @@
 import { useContext, useEffect, useRef, useState } from 'react';
 
-import { masterSet, stateSet } from '../actions';
-import { masterStateUpdateTimeout } from '../constants/system';
+import { masterSet } from '../actions';
 import { DispatchContext, StateContext } from '../context/state';
 import { isMaster } from '../selectors';
 
@@ -10,23 +9,10 @@ export function useMaster(): void {
   const dispatch = useContext(DispatchContext);
   const clientIsMaster = isMaster(state);
 
-  const masterUpdateTimer = useRef<number>(0);
-  useEffect(() => {
-    if (clientIsMaster) {
-      masterUpdateTimer.current = window.setInterval(() => {
-        dispatch(stateSet());
-      }, masterStateUpdateTimeout);
-    }
-
-    return (): void => {
-      window.clearInterval(masterUpdateTimer.current);
-    };
-  }, [dispatch, clientIsMaster]);
-
   const clientList = state.clientList;
   useEffect(() => {
     if (clientIsMaster) {
-      dispatch(stateSet({ master: state.myClientName }));
+      dispatch(masterSet(state.myClientName));
     }
   }, [clientList, dispatch, clientIsMaster, state.myClientName]);
 
@@ -43,7 +29,7 @@ export function useMaster(): void {
   const shouldInitMaster = initialised && !state.player.master;
   useEffect(() => {
     if (shouldInitMaster) {
-      dispatch(stateSet({ master: state.myClientName }));
+      dispatch(masterSet(state.myClientName));
     }
   }, [dispatch, shouldInitMaster, state.myClientName]);