diff --git a/src/helpers/useNetworkedState.js b/src/helpers/useNetworkedState.js index c2d0857..e005fca 100644 --- a/src/helpers/useNetworkedState.js +++ b/src/helpers/useNetworkedState.js @@ -14,9 +14,13 @@ function useNetworkedState( // Used to control whether the state needs to be sent to the socket const dirtyRef = useRef(false); + // Used to force a full update + const forceUpdateRef = useRef(false); + // Update dirty at the same time as state - const setState = useCallback((update, sync = true) => { + const setState = useCallback((update, sync = true, force = false) => { dirtyRef.current = sync; + forceUpdateRef.current = force; _setState(update); }, []); @@ -30,7 +34,12 @@ function useNetworkedState( useEffect(() => { if (session.socket && dirtyRef.current) { // If partial updates enabled, send just the changes to the socket - if (lastSyncedStateRef.current && debouncedState && partialUpdates) { + if ( + lastSyncedStateRef.current && + debouncedState && + partialUpdates && + !forceUpdateRef.current + ) { const changes = diff(lastSyncedStateRef.current, debouncedState); if (changes) { session.socket.emit(`${eventName}_update`, changes); @@ -39,6 +48,7 @@ function useNetworkedState( session.socket.emit(eventName, debouncedState); } dirtyRef.current = false; + forceUpdateRef.current = false; lastSyncedStateRef.current = debouncedState; } }, [session.socket, eventName, debouncedState, partialUpdates]); diff --git a/src/network/NetworkedMapAndTokens.js b/src/network/NetworkedMapAndTokens.js index 93d1098..810cfee 100644 --- a/src/network/NetworkedMapAndTokens.js +++ b/src/network/NetworkedMapAndTokens.js @@ -171,7 +171,7 @@ function NetworkedMapAndTokens({ session }) { }, [currentMap, debouncedMapState, userId, database]); function handleMapChange(newMap, newMapState) { - setCurrentMapState(newMapState); + setCurrentMapState(newMapState, true, true); setCurrentMap(newMap); if (newMap && newMap.type === "file") { @@ -189,7 +189,7 @@ function NetworkedMapAndTokens({ session }) { } function handleMapStateChange(newMapState) { - setCurrentMapState(newMapState); + setCurrentMapState(newMapState, true, true); } function addMapDrawActions(actions, indexKey, actionsKey) {