Skip to content

fix: optimize marker direction when metro router is relegated to fallbackRoute - #5029

Merged
GaoFuhong merged 2 commits into
masterfrom
fix-metro-router-marker-direction
Feb 4, 2026
Merged

GaoFuhong merged 2 commits into
masterfrom
fix-metro-router-marker-direction

Conversation

@GaoFuhong

Copy link
Copy Markdown
Collaborator

📝 Description

问题:metro 路由在降级到 fallbackRoute 时,返回的路径点包含目标点本身,rounded 连接器在末端生成零长度段,导致 marker 的自动方向计算取用上一个有效切线,在近距离情况下方向可能与末端走向相反。
解法:修改 metro 路由的 fallbackRoute,仅返回中间拐点,不再返回目标点,避免末段重复点导致零长度段,确保箭头方向与末端切线一致。

🖼️ Screenshot

Before After
image image

💡 Motivation and Context

🧩 Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Enhancement (changes that improvement of current feature or performance)
  • Refactoring (changes that neither fixes a bug nor adds a feature)
  • Test Case (changes that add missing tests or correct existing tests)
  • Code style optimization (changes that do not affect the meaning of the code)
  • Docs (changes that only update documentation)
  • Chore (changes that don't modify src or test files)

🔍 Self Check before Merge

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @GaoFuhong, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求解决了 metro 路由器在回退路由时,路径中包含目标点可能导致零长度段的问题。这些零长度段会引起标记方向计算不准确。修复方案修改了 fallbackRoute,使其不再包含目标点,确保只返回有效的中间拐点,从而解决了标记方向异常并提高了路径渲染的准确性。

Highlights

  • 优化 metro 路由器的 fallbackRoute 逻辑: 对 metro 路由器的 fallbackRoute 逻辑进行了精炼,以解决特定场景下的路径生成问题。
  • 避免零长度段的产生: fallbackRoute 不再在返回路径中包含目标点,从而有效避免了在路径末端生成零长度段。
  • 修正标记方向计算: 此更改解决了由于零长度段导致的标记方向计算不准确的问题,尤其是在连接距离较近时,确保箭头方向与末端切线一致。
  • 新增单元测试: 添加了新的单元测试,以验证修正后的 fallbackRoute 行为,确保生成的路径不包含起点和终点,并正确处理中间拐点。
Changelog
  • tests/registry/router/metro.spec.ts
    • 为 metro 路由器的 fallbackRoute 功能添加了新的单元测试。
    • 测试验证了 fallbackRoute 在生成的路径中不包含起点 (from) 和终点 (to)。
    • 测试确认了对中间交点的正确处理和包含。
    • 测试确保 previousDirectionAngle 被正确设置。
  • src/registry/router/metro.ts
    • 将 fallbackRoute 逻辑重构为一个独立的函数 metroFallbackRoute。
    • 修改了 metroFallbackRoute,以防止在返回路径中包含目标点 (to)。
    • 确保只有当存在且与起点/终点不同的有效中间交点时,才将其添加到回退路由中。
    • 导入了 ResolvedOptions 类型以提高类型安全性。
Activity
  • 此拉取请求由 GaoFuhong 创建。
  • 其中包含详细的问题描述、解决方案和截图。
  • 作者已将其标记为错误修复,并确认遵循了代码风格、文档(如果需要)、贡献指南,并添加/通过了测试。
  • 在提供的上下文中,没有其他人工活动(评论、审查)。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@GaoFuhong GaoFuhong linked an issue Feb 4, 2026 that may be closed by this pull request

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 PR 修复了 metro 路由在回退到 fallbackRoute 时,由于包含了目标点而导致箭头方向计算错误的问题。通过修改 fallbackRoute 不再返回目标点,解决了该问题。代码改动是有效的,并且添加了新的单元测试来覆盖这个修复,这是很好的实践。我提出了一些建议,主要关于提高代码的可读性和遵循项目类型安全规范,请查看具体的审查评论。

it('should call manhattan with metro defaults', () => {
manhattanMock.mockReturnValue([])

metro.call({} as any, [], {}, {} as any)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在这个测试文件中,as any 和 any 类型被频繁使用(例如,在第 20, 32, 41, 60, 63, 65, 83, 86, 88 行)。这违反了仓库的通用规则,即为了类型安全应避免使用 any。虽然完全模拟像 EdgeView 这样的复杂对象可能很繁琐,但请考虑为测试数据使用 Partial<T> 或定义最小化的接口,以改善类型检查。

References
  1. Avoid using as any for type casting; find the correct type definition instead to maintain type safety.

Comment thread __tests__/registry/router/metro.spec.ts Outdated
Comment thread src/registry/router/metro.ts
@codecov-commenter

codecov-commenter commented Feb 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.38%. Comparing base (cd0f438) to head (6a5acf5).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5029      +/-   ##
==========================================
+ Coverage   83.24%   83.38%   +0.13%     
==========================================
  Files         332      332              
  Lines       38453    38462       +9     
  Branches     7887     7893       +6     
==========================================
+ Hits        32009    32070      +61     
+ Misses       6421     6369      -52     
  Partials       23       23              
Flag Coverage Δ
x6 83.38% <100.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/registry/router/metro.ts 100.00% <100.00%> (+83.87%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GaoFuhong

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 修复了 metro 路由在降级到 fallbackRoute 时,由于返回路径中包含了目标点,导致末端 marker 方向计算错误的问题。解决方案是修改 fallbackRoute 不再返回目标点,这从根本上解决了问题。代码重构将 fallbackRoute 提取为独立函数,提高了可读性。此外,新增的 metro 路由单元测试覆盖了多种场景,包括此次修复的 fallbackRoute 行为,这是一个很好的实践,大大增强了代码的健壮性。

it('should call manhattan with metro defaults', () => {
manhattanMock.mockReturnValue([])

metro.call({} as any, [], {}, {} as any)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在测试中断言 metro.call 时,多处使用了 {} as any 来绕过类型检查。根据项目通用规则,应避免使用 as any。建议定义一个最小化的模拟对象或使用更具体的类型,以增强测试的类型安全性和可维护性。这个问题在文件中多次出现(例如第 32、41、60、83 行)。

References
  1. Avoid using as any for type casting; find the correct type definition instead to maintain type safety.

Comment on lines +63 to +68
const fallbackRoute = options.fallbackRoute as any

const resolvedOptions: any = {
directions: Array.from({ length: 8 }, () => ({})),
previousDirectionAngle: null,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在测试中过度使用 as any 会降低类型安全性,并可能掩盖潜在的类型相关错误。根据通用规则,应避免使用 as any。请为 fallbackRoute 和 resolvedOptions 提供更具体的类型,以提高测试的健壮性和可读性。

Suggested change
const fallbackRoute = options.fallbackRoute as any
const resolvedOptions: any = {
directions: Array.from({ length: 8 }, () => ({})),
previousDirectionAngle: null,
}
const fallbackRoute = options.fallbackRoute as (
from: Point,
to: Point,
options: { directions: any[]; previousDirectionAngle: number | null },
) => Point[]
const resolvedOptions = {
directions: Array.from({ length: 8 }, () => ({})),
previousDirectionAngle: null,
}
References
  1. Avoid using as any for type casting; find the correct type definition instead to maintain type safety.

return route
},
function metroFallbackRoute(
this: any,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

函数 metroFallbackRoute 的 this 上下文被显式指定为 any 类型。虽然在此函数中未使用 this,但根据 manhattan 路由器的选项定义,fallbackRoute 的 this 上下文应为 EdgeView 类型。为了保持类型一致性和代码清晰度,建议将其类型指定为 EdgeView 或 unknown,而不是 any。

Suggested change
this: any,
this: unknown,

@GaoFuhong
GaoFuhong merged commit 43d6164 into master Feb 4, 2026
2 checks passed
@GaoFuhong
GaoFuhong deleted the fix-metro-router-marker-direction branch February 4, 2026 09:33
lxfu1 pushed a commit that referenced this pull request Oct 15, 2026
…backRoute (#5029)

* fix: optimize marker direction when metro router is relegated to fallbackRoute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞使用metro路由创建连线时箭头方向反了

2 participants