Skip to content

Commit c16b5e8

Browse files
atscottalxhub
authored andcommitted
fix(core): Multiple subscribers to ApplicationRef.isStable should all see values (#53541)
The behavior of `ApplicationRef.isStable` changed in 16.1 due to 28c68f7. This change added a `share` to the `isStable` observable, which prevents additional subscribers from getting a value until a new one emits. One solution to the problem would be `shareReplay(1)`. However, that would increase the bundle size since we do not use `shareReplay` elsewhere. Instead, we don't even really need to share the observable. The `Observable` available in `ApplicationRef.isStable` before the above commit was the zone stable observable, without a `share`. The new behavior adds only an additional observable to the stream, `hasPendingTasks` (a `BehaviorSubject`). The observables in this stream are not expensive to subscribe to. The only one with side effects is the `isStable` (because it subscribes to onStable), but that one already has the `share` operator on it. Omitting the `share` in `ApplicationRef` also means that applications on `zoneless` will not have to pay the cost of the operator when we make zones optional because the zone stable observable is the only place we use it. PR Close #53541
1 parent 79d8736 commit c16b5e8

File tree

13 files changed

+10
-35
lines changed

13 files changed

+10
-35
lines changed

packages/core/src/application/application_ref.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import '../util/ng_jit_mode';
1010

1111
import {setThrowInvalidWriteToSignalError} from '@angular/core/primitives/signals';
1212
import {Observable, of} from 'rxjs';
13-
import {distinctUntilChanged, first, share, switchMap} from 'rxjs/operators';
13+
import {distinctUntilChanged, first, switchMap} from 'rxjs/operators';
1414

1515
import {getCompilerFacade, JitCompilerUsage} from '../compiler/compiler_facade';
1616
import {Console} from '../console';
@@ -351,7 +351,6 @@ export class ApplicationRef {
351351
.hasPendingTasks.pipe(
352352
switchMap(hasPendingTasks => hasPendingTasks ? of(false) : this.zoneIsStable),
353353
distinctUntilChanged(),
354-
share(),
355354
);
356355

357356
private readonly _injector = inject(EnvironmentInjector);

packages/core/test/application_ref_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {ServerModule} from '@angular/platform-server';
2121
import {ApplicationRef} from '../src/application/application_ref';
2222
import {NoopNgZone} from '../src/zone/ng_zone';
2323
import {ComponentFixtureNoNgZone, inject, TestBed, waitForAsync, withModule} from '../testing';
24+
import {take} from 'rxjs/operators';
2425

2526
let serverPlatformModule: Promise<Type<ServerModule>>|null = null;
2627
if (isNode) {
@@ -795,6 +796,14 @@ describe('AppRef', () => {
795796
expectStableTexts(MacroMicroTaskComp, ['111']);
796797
}));
797798

799+
it('isStable can be subscribed to many times', async () => {
800+
const appRef: ApplicationRef = TestBed.inject(ApplicationRef);
801+
// Create stable subscription but do not unsubscribe before the second subscription is made
802+
appRef.isStable.subscribe();
803+
await expectAsync(appRef.isStable.pipe(take(1)).toPromise()).toBeResolved();
804+
stableCalled = true;
805+
});
806+
798807
describe('unstable', () => {
799808
let unstableCalled = false;
800809

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,9 +1406,6 @@
14061406
{
14071407
"name": "setupStaticAttributes"
14081408
},
1409-
{
1410-
"name": "share"
1411-
},
14121409
{
14131410
"name": "shareSubjectFactory"
14141411
},

packages/core/test/bundling/animations/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,9 +1478,6 @@
14781478
{
14791479
"name": "setupStaticAttributes"
14801480
},
1481-
{
1482-
"name": "share"
1483-
},
14841481
{
14851482
"name": "shareSubjectFactory"
14861483
},

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,9 +1178,6 @@
11781178
{
11791179
"name": "setupStaticAttributes"
11801180
},
1181-
{
1182-
"name": "share"
1183-
},
11841181
{
11851182
"name": "shareSubjectFactory"
11861183
},

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,9 +2336,6 @@
23362336
{
23372337
"name": "setupStaticAttributes"
23382338
},
2339-
{
2340-
"name": "share"
2341-
},
23422339
{
23432340
"name": "shareSubjectFactory"
23442341
},

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,9 +1649,6 @@
16491649
{
16501650
"name": "setupStaticAttributes"
16511651
},
1652-
{
1653-
"name": "share"
1654-
},
16551652
{
16561653
"name": "shareSubjectFactory"
16571654
},

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,9 +1625,6 @@
16251625
{
16261626
"name": "setupStaticAttributes"
16271627
},
1628-
{
1629-
"name": "share"
1630-
},
16311628
{
16321629
"name": "shareSubjectFactory"
16331630
},

packages/core/test/bundling/hello_world/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,9 +932,6 @@
932932
{
933933
"name": "setUpAttributes"
934934
},
935-
{
936-
"name": "share"
937-
},
938935
{
939936
"name": "shareSubjectFactory"
940937
},

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,9 +1274,6 @@
12741274
{
12751275
"name": "setUpAttributes"
12761276
},
1277-
{
1278-
"name": "share"
1279-
},
12801277
{
12811278
"name": "shareSubjectFactory"
12821279
},

0 commit comments

Comments
 (0)