-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(sockets movement): smoother throttling + fix re-render bug #541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -323,8 +323,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) { | |
| } | ||
| }, [socket, currentWorkflowId]) | ||
|
|
||
| // Position update throttling at 60fps (16ms) | ||
| const THROTTLE_DELAY = 16 // 60fps standard | ||
| // Light throttling for position updates to ensure smooth collaborative movement | ||
| const positionUpdateTimeouts = useRef<Map<string, number>>(new Map()) | ||
| const pendingPositionUpdates = useRef<Map<string, any>>(new Map()) | ||
|
|
||
|
|
@@ -333,47 +332,33 @@ export function SocketProvider({ children, user }: SocketProviderProps) { | |
| (operation: string, target: string, payload: any) => { | ||
| if (!socket || !currentWorkflowId) return | ||
|
|
||
| // Check if this is a position update that should be throttled | ||
| // Apply light throttling only to position updates for smooth collaborative experience | ||
| const isPositionUpdate = operation === 'update-position' && target === 'block' | ||
|
|
||
| if (isPositionUpdate && payload.id) { | ||
| const blockId = payload.id | ||
|
|
||
| // Store the latest position update for this block | ||
| // Store the latest position update | ||
| pendingPositionUpdates.current.set(blockId, { | ||
| operation, | ||
| target, | ||
| payload, | ||
| timestamp: Date.now(), | ||
| }) | ||
|
|
||
| // Check if we have an active interval for this block | ||
| const existingTimeout = positionUpdateTimeouts.current.get(blockId) | ||
|
|
||
| if (!existingTimeout) { | ||
| // No active interval - start emitting at regular intervals | ||
| const intervalId = window.setInterval(() => { | ||
| // Check if we already have a pending timeout for this block | ||
| if (!positionUpdateTimeouts.current.has(blockId)) { | ||
| // Schedule emission with light throttling (120fps = ~8ms) | ||
| const timeoutId = window.setTimeout(() => { | ||
| const latestUpdate = pendingPositionUpdates.current.get(blockId) | ||
| if (latestUpdate) { | ||
| socket.emit('workflow-operation', latestUpdate) | ||
| pendingPositionUpdates.current.delete(blockId) | ||
| } else { | ||
| // No more updates pending - stop the interval | ||
| clearInterval(intervalId) | ||
| positionUpdateTimeouts.current.delete(blockId) | ||
| } | ||
| }, THROTTLE_DELAY) | ||
|
|
||
| positionUpdateTimeouts.current.set(blockId, intervalId) | ||
| positionUpdateTimeouts.current.delete(blockId) | ||
| }, 8) // 120fps for smooth movement | ||
|
|
||
| // Set a cleanup timeout to stop the interval if no updates come in | ||
| setTimeout(() => { | ||
| if (positionUpdateTimeouts.current.get(blockId) === intervalId) { | ||
| clearInterval(intervalId) | ||
| positionUpdateTimeouts.current.delete(blockId) | ||
| pendingPositionUpdates.current.delete(blockId) | ||
| } | ||
| }, 50) // Stop interval after 50ms of no updates | ||
| positionUpdateTimeouts.current.set(blockId, timeoutId) | ||
| } | ||
| } else { | ||
| // For all non-position updates, emit immediately | ||
|
|
@@ -411,14 +396,14 @@ export function SocketProvider({ children, user }: SocketProviderProps) { | |
| [socket, currentWorkflowId] | ||
| ) | ||
|
|
||
| // Throttled cursor updates (lower priority than position updates) | ||
| // Minimal cursor throttling (reduced from 30fps to 120fps) | ||
| const lastCursorEmit = useRef(0) | ||
| const emitCursorUpdate = useCallback( | ||
| (cursor: { x: number; y: number }) => { | ||
| if (socket && currentWorkflowId) { | ||
| const now = performance.now() | ||
| // Throttle cursor updates to 30fps to reduce noise | ||
| if (now - lastCursorEmit.current >= 33) { | ||
| // Very light throttling at 120fps (8ms) to prevent excessive spam | ||
| if (now - lastCursorEmit.current >= 8) { | ||
| socket.emit('cursor-update', { cursor }) | ||
| lastCursorEmit.current = now | ||
| } | ||
|
Comment on lines
+405
to
409
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Use a config constant for the throttle delay (8ms) to maintain consistency with other timing values |
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider using requestAnimationFrame instead of setTimeout for better performance and synchronization with browser render cycles