Remove a callback read's pending task right after the callback instead of one `setTimeout(fn, 0)` later

未关闭 适合新手
#3,836 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
angular, firebase, typescript

调研方向

阅读 src/zones.ts,重点查看第 123、153 和 190 行附近的回调包装以及待处理任务的移除。跟踪首次回调和取消订阅的路径,然后检查它们各自如何移除任务,以及回调抛出异常时的行为。完成标准是:任务在首次回调后立即移除,或在取消订阅时移除,不使用定时器,并且符合所述的标记顺序。

由索引模型根据 Issue 内容生成。

描述

comp: ssr comp: zones type: feature version: current (v17+)
Version info

Angular: 22.2

Firebase: 12.19.0

AngularFire: 22.0.0-rc.0, and main

Description

Every callback-taking read that holds a pending task keeps it one setTimeout(fn, 0) delay longer than it needs to:

  • ɵzoneWrap registers a pending task when a function wrapped with blockUntilFirst true, such as onSnapshot, onAuthStateChanged or onValue, is handed a callback, and removes it with setTimeout(taskDone, 0) scheduled right before the first callback runs (src/zones.ts:123) and again when the returned unsubscribe is called (:190).
  • So ApplicationRef.isStable can turn true only after that timer callback has run, one macrotask after the first value arrived. The comment next to the wrapping call, // TODO create a microtask to track callback functions (:153), asks for a microtask in its place.
  • #3833 moved the Observable-returning reads off their timer. The callback reads are the remaining branch.
  • Removing the task in a finally right after the callback body needs no timer and keeps a throwing callback from leaving its task behind.
How to reproduce these conditions

On a fresh Angular 22.2 app with @angular/fire@next, wrap a callback-taking function with the real ɵzoneWrap, invoke the callback from a setTimeout(fn, 0) outside Angular, and inside that same setTimeout callback, before invoking it, register a second setTimeout(fn, 0) and a queueMicrotask as markers. Observe the moment the wrapper calls the remover function that PendingTasks.add() returned:

  • The marker setTimeout callback has already run, in a zone.js app and a zoneless one, in Chrome and Firefox, for the first callback and for unsubscribe.
  • With the task removed in a finally after the callback body, the remover is called before either marker has run, in every one of those runs.
  • The real onSnapshot against the Firestore emulator, with the markers registered by a raw SDK listener attached first to the same document, reads the same way: the remover runs after the marker setTimeout callback today, and before it with the change.
Expected behavior

A callback read's pending task is removed as soon as its first callback has run, or as soon as it is unsubscribed.

Actual behavior

The task is removed one setTimeout(fn, 0) delay later.

主要语言
TypeScript
星标
7.8k
派生
2.2k
平均合并
2 天 21 小时
30 天内合并 PR
24

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angularfire 的其他 Issue

查看 angular/angularfire 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。