Skip to content

Commit

Permalink
fix(core): Multiple subscribers to ApplicationRef.isStable should all…
Browse files Browse the repository at this point in the history
… see values (angular#53541)

The behavior of `ApplicationRef.isStable` changed in 16.1 due to
angular@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 angular#53541
  • Loading branch information
atscott authored and alxhub committed Dec 13, 2023
1 parent 48c471c commit 629343f
Show file tree
Hide file tree
Showing 13 changed files with 10 additions and 35 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/application/application_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../util/ng_jit_mode';

import {setThrowInvalidWriteToSignalError} from '@angular/core/primitives/signals';
import {Observable, of} from 'rxjs';
import {distinctUntilChanged, first, share, switchMap} from 'rxjs/operators';
import {distinctUntilChanged, first, switchMap} from 'rxjs/operators';

import {getCompilerFacade, JitCompilerUsage} from '../compiler/compiler_facade';
import {Console} from '../console';
Expand Down Expand Up @@ -351,7 +351,6 @@ export class ApplicationRef {
.hasPendingTasks.pipe(
switchMap(hasPendingTasks => hasPendingTasks ? of(false) : this.zoneIsStable),
distinctUntilChanged(),
share(),
);

private readonly _injector = inject(EnvironmentInjector);
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/application_ref_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {ServerModule} from '@angular/platform-server';
import {ApplicationRef} from '../src/application/application_ref';
import {NoopNgZone} from '../src/zone/ng_zone';
import {ComponentFixtureNoNgZone, inject, TestBed, waitForAsync, withModule} from '../testing';
import {take} from 'rxjs/operators';

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

it('isStable can be subscribed to many times', async () => {
const appRef: ApplicationRef = TestBed.inject(ApplicationRef);
// Create stable subscription but do not unsubscribe before the second subscription is made
appRef.isStable.subscribe();
await expectAsync(appRef.isStable.pipe(take(1)).toPromise()).toBeResolved();
stableCalled = true;
});

describe('unstable', () => {
let unstableCalled = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1406,9 +1406,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,9 +1478,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/defer/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -2336,9 +2336,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1649,9 +1649,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1625,9 +1625,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,9 +932,6 @@
{
"name": "setUpAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,9 +1280,6 @@
{
"name": "setUpAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1970,9 +1970,6 @@
{
"name": "shallowEqual"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,6 @@
{
"name": "setUpAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1418,9 +1418,6 @@
{
"name": "setupStaticAttributes"
},
{
"name": "share"
},
{
"name": "shareSubjectFactory"
},
Expand Down

0 comments on commit 629343f

Please sign in to comment.