From 58cd785da620c6bd0144e3782099fcbb36bff0c3 Mon Sep 17 00:00:00 2001 From: hjlarry Date: Thu, 11 Sep 2025 09:36:22 +0800 Subject: [PATCH] use const for cursor move config --- .../collaboration/hooks/use-collaboration.ts | 8 ++----- .../collaboration/services/cursor-service.ts | 21 ++++++------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/web/app/components/workflow/collaboration/hooks/use-collaboration.ts b/web/app/components/workflow/collaboration/hooks/use-collaboration.ts index 6caeefb596..42cff00ebb 100644 --- a/web/app/components/workflow/collaboration/hooks/use-collaboration.ts +++ b/web/app/components/workflow/collaboration/hooks/use-collaboration.ts @@ -19,12 +19,8 @@ export function useCollaboration(appId: string, reactFlowStore?: any) { let connectionId: string | null = null - if (!cursorServiceRef.current) { - cursorServiceRef.current = new CursorService({ - minMoveDistance: 10, - throttleMs: 300, - }) - } + if (!cursorServiceRef.current) + cursorServiceRef.current = new CursorService() const initCollaboration = async () => { connectionId = await collaborationManager.connect(appId, reactFlowStore) diff --git a/web/app/components/workflow/collaboration/services/cursor-service.ts b/web/app/components/workflow/collaboration/services/cursor-service.ts index 2a86e37697..0d86992ca0 100644 --- a/web/app/components/workflow/collaboration/services/cursor-service.ts +++ b/web/app/components/workflow/collaboration/services/cursor-service.ts @@ -2,10 +2,8 @@ import type { RefObject } from 'react' import type { CursorPosition } from '../types/collaboration' import type { ReactFlowInstance } from 'reactflow' -export type CursorServiceConfig = { - minMoveDistance?: number - throttleMs?: number -} +const CURSOR_MIN_MOVE_DISTANCE = 10 +const CURSOR_THROTTLE_MS = 500 export class CursorService { private containerRef: RefObject | null = null @@ -15,14 +13,6 @@ export class CursorService { private onEmitPosition: ((position: CursorPosition) => void) | null = null private lastEmitTime = 0 private lastPosition: { x: number; y: number } | null = null - private config: Required - - constructor(config: CursorServiceConfig = {}) { - this.config = { - minMoveDistance: config.minMoveDistance ?? 5, - throttleMs: config.throttleMs ?? 300, - } - } startTracking( containerRef: RefObject, @@ -78,10 +68,11 @@ export class CursorService { // Always emit cursor position (remove boundary check since world coordinates can be negative) const now = Date.now() - const timeThrottled = now - this.lastEmitTime > this.config.throttleMs + const timeThrottled = now - this.lastEmitTime > CURSOR_THROTTLE_MS + const minDistance = CURSOR_MIN_MOVE_DISTANCE / (this.reactFlowInstance?.getZoom() || 1) const distanceThrottled = !this.lastPosition - || (Math.abs(x - this.lastPosition.x) > this.config.minMoveDistance / (this.reactFlowInstance?.getZoom() || 1)) - || (Math.abs(y - this.lastPosition.y) > this.config.minMoveDistance / (this.reactFlowInstance?.getZoom() || 1)) + || (Math.abs(x - this.lastPosition.x) > minDistance) + || (Math.abs(y - this.lastPosition.y) > minDistance) if (timeThrottled && distanceThrottled) { this.lastPosition = { x, y }