Skip to content

Commit

Permalink
Bugfix: only set currentTIme for playing video once
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Aug 30, 2020
1 parent 6c26f5c commit 4c42a65
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/connection/DynamicScene/DynamicSceneConnected/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const DynamicSceneConnected: React.FC<DynamicSceneConnectedProps> = ({
);

const emitRoomData = useRoomDataEmit(emit, roomContextData.roomData);
const roomContextValue = { ...roomContextData, emitRoomData };
const roomContextValue = {
...roomContextData,
emitRoomData,
originalRoomData: roomData,
};

useRoomDataSyncing(roomContextValue, socket);

Expand Down
11 changes: 11 additions & 0 deletions src/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type RoomContextValueTypes = Readonly<{
*/
occupants: ReadonlyMap<PersonId, RoomPerson>;

/**
* Current (most recently updated) snapshot of the server state.
*/
roomData: RoomData;

roomName: string;
Expand Down Expand Up @@ -41,5 +44,13 @@ export type AsGettersAndSetters<Values> = {
export type RoomContextData = AsGettersAndSetters<RoomContextValueTypes>;

export type RoomContextValue = RoomContextData & {
/**
* Updates local room data and emits any changes to the server.
*/
emitRoomData: (newRoomData: Partial<RoomData>) => void;

/**
* Snapshot of the room data as it was when first joined.
*/
originalRoomData: RoomData;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useRoomContext } from "@connection/RoomContext";
import { useEmitOnClick } from "../useEmitOnClick";

export const usePlayPauseControl = () => {
const { roomData } = useRoomContext();
const { originalRoomData, roomData } = useRoomContext();
const { currentTime, playing } = roomData.get();

useEmitOnClick(controls.playPauseButton, (oldRoomData) => ({
Expand All @@ -18,10 +18,17 @@ export const usePlayPauseControl = () => {
controls.playPauseButton.setAttribute("src", playing ? "#pause" : "#play");

if (playing) {
videoElement.currentTime = currentTime;
videoElement.play();
} else {
videoElement.pause();
}
}, [currentTime, playing]);

useEffect(() => {
videoElement.currentTime = originalRoomData.currentTime;

if (originalRoomData.playing) {
videoElement.play();
}
}, [originalRoomData]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export const useTimeSynchronization = () => {
const { emitRoomData, roomData } = useRoomContext();
const { currentTime, playing } = roomData.get();

// Constantly update the server on the current time of the video
useInterval(() => {
emitRoomData({
currentTime: videoElement.currentTime,
});
}, videoElementSyncInterval);

// If the video is paused, we can safely match its time to currentTime
useEffect(() => {
if (!playing) {
videoElement.currentTime = currentTime;
Expand Down

0 comments on commit 4c42a65

Please sign in to comment.