Skip to content

[Feature] 重构:清理 src/pages/content/index.tsx 里的大量重复性代码 #897

Description

@andy-ish

🥰 需求描述 | Feature Description

现存 src/pages/content/index.tsx 在 window.addEventListener('beforeunload', () => { /* … */} (第 649 行)中有大量的重复样板代码来处理内存泄露问题,相应代码节选如下:

// Setup cleanup on page unload to prevent memory leaks
    window.addEventListener('beforeunload', () => {
      try {
        window.removeEventListener('unhandledrejection', onUnhandledRejection);
        window.removeEventListener('error', onWindowError);
        // Disconnect watermark-remover observers.
        stopWatermarkRemover();
        if (folderManagerInstance) {
          folderManagerInstance.destroy();
          folderManagerInstance = null;
        }
        if (promptManagerInstance) {
          promptManagerInstance.destroy();
          promptManagerInstance = null;
        }
        if (slashPromptFeatureInstance) {
          slashPromptFeatureInstance.destroy();
          slashPromptFeatureInstance = null;
        }
        if (quoteReplyCleanup) {
          quoteReplyCleanup();
          quoteReplyCleanup = null;
        }
// ……

大量的代码被用来重复地执行清理逻辑,违反了代码的 DRY (Don't Repeat Yourself) 原则。因此,本 Issue 提出对此进行重构。

🧐 解决方案 | Proposed Solution

src/core/utils 模块新增一个管理类,集中式地封装记录和执行清理函数的逻辑。

✅ 验收标准 | Acceptance Criteria

实际上,代码修改后,用户应该能感知到 0 处变动,因为这是一个不影响代码功能的重构。

🔄 替代方案 | Alternatives Considered

No response

📝 补充信息 | Additional Information

我已经在本地完成了相应改动并测试通过(点击跳转),若仓库 Owner approve,我可以直接开启 PR。

💻 贡献意愿 | Contribution

  • 我愿意为该功能贡献代码 / I am willing to contribute code for this feature
  • 💡 若 Issue 未标记 community-only,其他贡献者可评论 /claim 认领 / Other contributors may use /claim unless the issue is labeled community-only

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions