Skip to content

Commit 7e1bc51

Browse files
alxhubdylhunn
authored andcommitted
fix(common): untrack subscription and unsubscription in async pipe (#50522)
This commit wraps the actual subscription/unsubscription in the `async` pipe with `untracked`, to ensure that any signal reads/writes which might take place in `Observable` side effects are not attributed to the template. Fixes #50382 PR Close #50522
1 parent 8ce28db commit 7e1bc51

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎packages/common/src/pipes/async_pipe.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, ɵisPromise, ɵisSubscribable} from '@angular/core';
9+
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, untracked, ɵisPromise, ɵisSubscribable} from '@angular/core';
1010
import {Observable, Subscribable, Unsubscribable} from 'rxjs';
1111

1212
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
@@ -19,16 +19,24 @@ interface SubscriptionStrategy {
1919

2020
class SubscribableStrategy implements SubscriptionStrategy {
2121
createSubscription(async: Subscribable<any>, updateLatestValue: any): Unsubscribable {
22-
return async.subscribe({
22+
// Subscription can be side-effectful, and we don't want any signal reads which happen in the
23+
// side effect of the subscription to be tracked by a component's template when that
24+
// subscription is triggered via the async pipe. So we wrap the subscription in `untracked` to
25+
// decouple from the current reactive context.
26+
//
27+
// `untracked` also prevents signal _writes_ which happen in the subscription side effect from
28+
// being treated as signal writes during the template evaluation (which throws errors).
29+
return untracked(() => async.subscribe({
2330
next: updateLatestValue,
2431
error: (e: any) => {
2532
throw e;
2633
}
27-
});
34+
}));
2835
}
2936

3037
dispose(subscription: Unsubscribable): void {
31-
subscription.unsubscribe();
38+
// See the comment in `createSubscription` above on the use of `untracked`.
39+
untracked(() => subscription.unsubscribe());
3240
}
3341
}
3442

‎packages/common/test/pipes/async_pipe_spec.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99
import {AsyncPipe} from '@angular/common';
10-
import {ChangeDetectorRef, Component, EventEmitter} from '@angular/core';
10+
import {ChangeDetectorRef, Component, computed, EventEmitter, signal} from '@angular/core';
1111
import {TestBed} from '@angular/core/testing';
12-
import {of, Subscribable, Unsubscribable} from 'rxjs';
12+
import {Observable, of, Subscribable, Unsubscribable} from 'rxjs';
1313

1414
{
1515
describe('AsyncPipe', () => {
@@ -114,6 +114,29 @@ import {of, Subscribable, Unsubscribable} from 'rxjs';
114114
expect(firstResult).toBeNaN();
115115
expect(secondResult).toBeNaN();
116116
});
117+
118+
it('should not track signal reads in subscriptions', () => {
119+
const trigger = signal(false);
120+
121+
const obs = new Observable(() => {
122+
// Whenever `obs` is subscribed, synchronously read `trigger`.
123+
trigger();
124+
});
125+
126+
let trackCount = 0;
127+
const tracker = computed(() => {
128+
// Subscribe to `obs` within this `computed`. If the subscription side effect runs
129+
// within the computed, then changes to `trigger` will invalidate this computed.
130+
pipe.transform(obs);
131+
132+
// The computed returns how many times it's run.
133+
return ++trackCount;
134+
});
135+
136+
expect(tracker()).toBe(1);
137+
trigger.set(true);
138+
expect(tracker()).toBe(1);
139+
});
117140
});
118141

119142
describe('ngOnDestroy', () => {

0 commit comments

Comments
 (0)