Skip to content

fix(pc-list): manual scan button refreshes state instead of killing mDNS - #51

Merged
qiin2333 merged 2 commits into
masterfrom
fix/pc-list-scan-button-refresh
Jun 25, 2026
Merged

fix(pc-list): manual scan button refreshes state instead of killing mDNS#51
qiin2333 merged 2 commits into
masterfrom
fix/pc-list-scan-button-refresh

Conversation

@qiin2333

@qiin2333 qiin2333 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

问题

首页「扫描网络」按钮不仅没用,还会把后台 mDNS 发现杀掉

调用链:按钮 `scanNetwork()` → `ComputerManager.scanNetwork()` → `MdnsDiscovery.discover(5000)`:

```ts
async discover(timeout = 5000) {
await this.startDiscovery(); // (a)
await new Promise(r => setTimeout(r, timeout)); // (b) 干等 5s
this.stopDiscovery(); // (c)
return Array.from(this.discoveredComputers.values());
}
```

而 `startDiscovery()` 开头:

```ts
if (this.isDiscovering) {
console.info('MdnsDiscovery: 已经在发现中');
return; // ← 后台扫描已在跑,直接返回
}
```

由于 `aboutToAppear → initializeAsync()` 已经调了 `startBackgroundScan()`,`isDiscovering` 恒为 `true`,所以:

  • (a) 直接 early-return,不发起新的 mDNS 查询;
  • (b) 空等 5s;
  • (c) `stopDiscovery()` 把后台扫描整个干掉(`disposed=true`、`stopSearchingMDNS()`、`off` 所有监听、`discoveryService=null`、`isDiscovering=false`)。

更糟的是它恢复不了:`onPageShow` 只重启 `startPolling()`,不重启 `startBackgroundScan()`(后者只在 `aboutToAppear` 调过一次)。所以点过一次按钮后,当前页面生命周期内 mDNS 自动发现新主机的能力就永久失效了,toast 却显示「扫描完成」。

修复

既然后台 mDNS 已经是持续发现,手动按钮对「发现新主机」本就是冗余的。把按钮改成纯状态刷新:

  • `scanNetwork()` → 重命名 `refreshKnownHosts()`,改调 `refreshAllComputers()` 重新轮询已知主机的在线状态;
  • 保留 `isScanning` loading 态 + toast(底部按钮的 spinner 读这个 flag);
  • 加注释说明为何不再调 `discover()`(避免后人重新踩坑);
  • 文案:按钮 `扫描网络` → `刷新`,空状态说明改为「自动发现局域网内运行 Foundation Sunshine 的电脑」(新主机仍由后台 mDNS 自动发现),toast `扫描完成/失败` → `刷新完成/失败`。

新主机发现仍由 `startBackgroundScan()` 持续负责,行为不变。

与安卓对比(背景)

安卓 `ComputerManagerService` 同样不做子网扫描,而是「持续 mDNS 发现 + 持续轮询已知主机」,架构与本仓库一致(离线阈值 2/3 等常量也是照抄)。所以这里把按钮降级为刷新,不影响与安卓的架构对齐。

遗留(可选后续)

`ComputerManager.scanNetwork()`(`ComputerManager.ets:1091`)现在是死代码——它就是触发上述 bug 的入口,且已无任何引用(grep 已确认)。建议单独清理(连同判断 `MdnsDiscovery.discover()` 是否还需要保留)。

测试

  • 点「刷新」按钮:toast 显示「刷新完成」,已知主机状态立即更新,后台 mDNS 发现不受影响(退出再进页面仍能自动发现新主机)。
  • 空状态:首次使用时仍能由后台 mDNS 自动发现新主机,「手动添加」可用。

Summary by CodeRabbit

  • 新功能

    • 将空状态和底部操作栏的主要动作调整为“刷新”,支持手动更新已知主机的在线状态。
    • 刷新过程中会显示加载状态,并防止重复触发。
  • 改进

    • 更新了空列表提示文案,让引导更贴近当前的发现方式。
    • 在不支持扫码时,提示内容改为引导用户使用刷新或手动添加。

The manual scan button called MdnsDiscovery.discover(), but the background mDNS scan already set isDiscovering=true, so startDiscovery() early-returned and no new query fired. discover() then calls stopDiscovery(), which tears down the ongoing background scan (disposed=true, listeners off, discoveryService nulled). onPageShow only restarts polling, not the background scan, so mDNS auto-discovery of new hosts stayed dead for the rest of the page lifetime.

Repurpose the button into a pure state refresh: call refreshAllComputers() to re-poll known hosts with no side effects on the background scan. Rename scanNetwork -> refreshKnownHosts and update label/toast/description to match.
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4070091d-6543-492b-a509-e32201ef03ff

📥 Commits

Reviewing files that changed from the base of the PR and between de77a21 and e6f31c9.

📒 Files selected for processing (1)
  • entry/src/main/ets/pages/PcListPageV2.ets
🚧 Files skipped from review as they are similar to previous changes (1)
  • entry/src/main/ets/pages/PcListPageV2.ets

📝 Walkthrough

Walkthrough

PcListPageV2 将空状态和底部按钮的操作从扫描网络改为刷新已知主机,相关文案同步更新。刷新方法改为调用 computerManager.refreshAllComputers(),并处理加载态、成功和失败提示。

Changes

PcListPageV2 刷新入口调整

Layer / File(s) Summary
刷新已知主机状态
entry/src/main/ets/pages/PcListPageV2.ets
refreshKnownHosts()isScanning 时直接返回,并调用 computerManager.refreshAllComputers();成功、失败和 finally 分别更新 Toast 与加载状态。
空状态与工具栏刷新入口
entry/src/main/ets/pages/PcListPageV2.ets
空状态标题、快速操作按钮、底部圆形按钮和扫码不支持提示都改为刷新相关文案,并把点击回调切到 refreshKnownHosts()

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了本次改动:将手动扫描按钮改为刷新已知主机状态,并避免中断 mDNS。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pc-list-scan-button-refresh

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

本 PR 修复 PcList 首页「扫描网络」按钮会误杀后台 mDNS 发现的问题:将手动操作降级为“刷新已知主机状态”,避免触发 MdnsDiscovery.discover() 导致后台扫描被 stopDiscovery() 停止,从而保证新主机仍可由后台持续 mDNS 自动发现。

Changes:

  • 将按钮行为从 scanNetwork() 改为 refreshKnownHosts(),内部改调用 ComputerManager.refreshAllComputers() 刷新已知主机在线状态。
  • 更新空状态与按钮文案(“扫描网络”→“刷新”,“扫描完成/失败”→“刷新完成/失败”)。
  • 增加注释说明为何不再调用 mDNS discover(避免后续回归同类问题)。

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 591 to +595
.height(48)
.padding({ left: AppSpacing.XLarge, right: AppSpacing.XLarge })
.borderRadius(AppSizes.RadiusLarge)
.backgroundColor(AppColors.Primary)
.onClick(() => this.scanNetwork())
.onClick(() => this.refreshKnownHosts())
.fillColor(AppColors.TextOnPrimary)
.margin({ right: 8 })
Text('扫描网络')
Text('刷新')
Comment on lines 581 to +585
.width(20)
.height(20)
.fillColor(AppColors.TextOnPrimary)
.margin({ right: 8 })
Text('扫描网络')
Text('刷新')
Comment on lines 718 to +722
.width(44)
.height(44)
.backgroundColor(0x1A000000) // 与 SaveButton 保持一致的半透明背景
.enabled(!this.isScanning)
.onClick(() => this.scanNetwork())
.onClick(() => this.refreshKnownHosts())
@qiin2333
qiin2333 merged commit 3c4822a into master Jun 25, 2026
2 checks passed
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.

2 participants