Skip to content

Commit

Permalink
feat(core): migrate ExperimentalPendingTasks to PendingTasks
Browse files Browse the repository at this point in the history
This commit promotes the `ExperimentalPendingTasks` service from
experimental to developer preview and includes a migration schematic for
the rename.

BREAKING CHANGE: `ExperimentalPendingTasks` has been renamed to
`PendingTasks`.
  • Loading branch information
atscott committed Aug 27, 2024
1 parent 76b9e2b commit 0067ae2
Show file tree
Hide file tree
Showing 39 changed files with 328 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
Type,
ViewContainerRef,
ViewEncapsulation,
ɵPendingTasks as PendingTasks,
PendingTasks,
EventEmitter,
Output,
} from '@angular/core';
Expand Down Expand Up @@ -84,16 +84,16 @@ export class DocViewer implements OnChanges {

// tslint:disable-next-line:no-unused-variable
private animateContent = false;
private readonly pendingRenderTasks = inject(PendingTasks);
private readonly pendingTasks = inject(PendingTasks);

private countOfExamples = 0;

async ngOnChanges(changes: SimpleChanges): Promise<void> {
const taskId = this.pendingRenderTasks.add();
const removeTask = this.pendingTasks.add();
if ('docContent' in changes) {
await this.renderContentsAndRunClientSetup(this.docContent!);
}
this.pendingRenderTasks.remove(taskId);
removeTask();
}

async renderContentsAndRunClientSetup(content?: string): Promise<void> {
Expand Down Expand Up @@ -195,7 +195,7 @@ export class DocViewer implements OnChanges {
const preview = Boolean(placeholder.getAttribute('preview'));
const title = placeholder.getAttribute('header') ?? undefined;
const firstCodeSnippetTitle =
snippets.length > 0 ? snippets[0].title ?? snippets[0].name : undefined;
snippets.length > 0 ? (snippets[0].title ?? snippets[0].name) : undefined;
const exampleRef = this.viewContainer.createComponent(ExampleViewer);

this.countOfExamples++;
Expand Down
6 changes: 3 additions & 3 deletions adev/src/content/guide/zoneless.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ Zoneless applications. In fact, removing these calls can lead to performance reg
are used in applications that still rely on ZoneJS.
</docs-callout>

### `ExperimentalPendingTasks` for Server Side Rendering (SSR)
### `PendingTasks` for Server Side Rendering (SSR)

If you are using SSR with Angular, you may know that it relies on ZoneJS to help determine when the application
is "stable" and can be serialized. If there are asynchronous tasks that should prevent serialization, an application
not using ZoneJS will need to make Angular aware of these with the `ExperimentalPendingTasks` service. Serialization
not using ZoneJS will need to make Angular aware of these with the `PendingTasks` service. Serialization
will wait for the first moment that all pending tasks have been removed.

```typescript
const taskService = inject(ExperimentalPendingTasks);
const taskService = inject(PendingTasks);
const taskCleanup = taskService.add();
await doSomeWorkThatNeedsToBeRendered();
taskCleanup();
Expand Down
2 changes: 1 addition & 1 deletion adev/src/content/reference/errors/NG0506.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ class SimpleComponent {

In zoneless scenarios, stability might be delayed by an application code inside of an `effect` running in an infinite loop (potentially because signals used in effect functions keep changing) or a pending HTTP request.

Developers may also explicitly contribute to indicating the application's stability by using the experimental [`PendingTasks`](/api/core/ExperimentalPendingTasks) service. If you use the mentioned APIs in your application, make sure you invoke a function to mark the task as completed.
Developers may also explicitly contribute to indicating the application's stability by using the [`PendingTasks`](/api/core/PendingTasks) service. If you use the mentioned APIs in your application, make sure you invoke a function to mark the task as completed.
2 changes: 1 addition & 1 deletion goldens/public-api/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ export interface ExistingSansProvider {
}

// @public
export class ExperimentalPendingTasks {
export class PendingTasks {
add(): () => void;
// (undocumented)
static ɵprov: unknown;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/http/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
runInInjectionContext,
ɵConsole as Console,
ɵformatRuntimeError as formatRuntimeError,
ɵPendingTasks as PendingTasks,
ɵPendingTasksInternal as PendingTasks,
} from '@angular/core';
import {Observable} from 'rxjs';
import {finalize} from 'rxjs/operators';
Expand Down
1 change: 1 addition & 0 deletions packages/core/schematics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pkg_npm(
"//packages/core/schematics/migrations/after-render-phase:bundle",
"//packages/core/schematics/migrations/http-providers:bundle",
"//packages/core/schematics/migrations/invalid-two-way-bindings:bundle",
"//packages/core/schematics/migrations/pending-tasks:bundle",
"//packages/core/schematics/ng-generate/control-flow-migration:bundle",
"//packages/core/schematics/ng-generate/inject-migration:bundle",
"//packages/core/schematics/ng-generate/route-lazy-loading:bundle",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/schematics/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"version": "18.1.0",
"description": "Updates calls to afterRender with an explicit phase to the new API",
"factory": "./migrations/after-render-phase/bundle"
},
"migration-pending-tasks": {
"version": "19.0.0",
"description": "Updates ExperimentalPendingTasks to PendingTasks",
"factory": "./migrations/pending-tasks/bundle"
}
}
}
33 changes: 33 additions & 0 deletions packages/core/schematics/migrations/pending-tasks/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("//tools:defaults.bzl", "esbuild", "ts_library")

package(
default_visibility = [
"//packages/core/schematics:__pkg__",
"//packages/core/schematics/migrations/google3:__pkg__",
"//packages/core/schematics/test:__pkg__",
],
)

ts_library(
name = "pending-tasks",
srcs = glob(["**/*.ts"]),
tsconfig = "//packages/core/schematics:tsconfig.json",
deps = [
"//packages/core/schematics/utils",
"@npm//@angular-devkit/schematics",
"@npm//@types/node",
"@npm//typescript",
],
)

esbuild(
name = "bundle",
entry_point = ":index.ts",
external = [
"@angular-devkit/*",
"typescript",
],
format = "cjs",
platform = "node",
deps = [":pending-tasks"],
)
58 changes: 58 additions & 0 deletions packages/core/schematics/migrations/pending-tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Rule, SchematicsException, Tree, UpdateRecorder} from '@angular-devkit/schematics';
import {relative} from 'path';
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';
import {migrateFile} from './migration';

export default function (): Rule {
return async (tree: Tree) => {
const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree);
const basePath = process.cwd();
const allPaths = [...buildPaths, ...testPaths];

if (!allPaths.length) {
throw new SchematicsException(
'Could not find any tsconfig file. Cannot run the afterRender phase migration.',
);
}

for (const tsconfigPath of allPaths) {
runMigration(tree, tsconfigPath, basePath);
}
};
}

function runMigration(tree: Tree, tsconfigPath: string, basePath: string) {
const program = createMigrationProgram(tree, tsconfigPath, basePath);
const sourceFiles = program
.getSourceFiles()
.filter((sourceFile) => canMigrateFile(basePath, sourceFile, program));

for (const sourceFile of sourceFiles) {
let update: UpdateRecorder | null = null;

const rewriter = (startPos: number, width: number, text: string | null) => {
if (update === null) {
// Lazily initialize update, because most files will not require migration.
update = tree.beginUpdate(relative(basePath, sourceFile.fileName));
}
update.remove(startPos, width);
if (text !== null) {
update.insertLeft(startPos, text);
}
};
migrateFile(sourceFile, program.getTypeChecker(), rewriter);

if (update !== null) {
tree.commitUpdate(update);
}
}
}
67 changes: 67 additions & 0 deletions packages/core/schematics/migrations/pending-tasks/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import ts from 'typescript';
import {ChangeTracker} from '../../utils/change_tracker';
import {
getImportOfIdentifier,
getImportSpecifier,
getNamedImports,
} from '../../utils/typescript/imports';

const CORE = '@angular/core';
const EXPERIMENTAL_PENDING_TASKS = 'ExperimentalPendingTasks';

type RewriteFn = (startPos: number, width: number, text: string) => void;

export function migrateFile(
sourceFile: ts.SourceFile,
typeChecker: ts.TypeChecker,
rewriteFn: RewriteFn,
) {
const changeTracker = new ChangeTracker(ts.createPrinter());
// Check if there are any imports of the `AfterRenderPhase` enum.
const coreImports = getNamedImports(sourceFile, CORE);
if (!coreImports) {
return;
}
const importSpecifier = getImportSpecifier(sourceFile, CORE, EXPERIMENTAL_PENDING_TASKS);
if (!importSpecifier) {
return;
}
const nodeToReplace = importSpecifier.propertyName ?? importSpecifier.name;
if (!ts.isIdentifier(nodeToReplace)) {
return;
}

changeTracker.replaceNode(nodeToReplace, ts.factory.createIdentifier('PendingTasks'));

ts.forEachChild(sourceFile, function visit(node: ts.Node) {
// import handled above
if (ts.isImportDeclaration(node)) {
return;
}

if (
ts.isIdentifier(node) &&
node.text === EXPERIMENTAL_PENDING_TASKS &&
getImportOfIdentifier(typeChecker, node)?.name === EXPERIMENTAL_PENDING_TASKS
) {
changeTracker.replaceNode(node, ts.factory.createIdentifier('PendingTasks'));
}

ts.forEachChild(node, visit);
});

// Write the changes.
for (const changesInFile of changeTracker.recordChanges().values()) {
for (const change of changesInFile) {
rewriteFn(change.start, change.removeLength ?? 0, change.text);
}
}
}
2 changes: 2 additions & 0 deletions packages/core/schematics/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jasmine_node_test(
"//packages/core/schematics/migrations/http-providers:bundle",
"//packages/core/schematics/migrations/invalid-two-way-bindings",
"//packages/core/schematics/migrations/invalid-two-way-bindings:bundle",
"//packages/core/schematics/migrations/pending-tasks",
"//packages/core/schematics/migrations/pending-tasks:bundle",
"//packages/core/schematics/ng-generate/control-flow-migration",
"//packages/core/schematics/ng-generate/control-flow-migration:bundle",
"//packages/core/schematics/ng-generate/control-flow-migration:static_files",
Expand Down
101 changes: 101 additions & 0 deletions packages/core/schematics/test/pending_tasks_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {runfiles} from '@bazel/runfiles';
import shx from 'shelljs';

describe('experimental pending tasks migration', () => {
let runner: SchematicTestRunner;
let host: TempScopedNodeJsSyncHost;
let tree: UnitTestTree;
let tmpDirPath: string;

function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}

function runMigration() {
return runner.runSchematic('migration-pending-tasks', {}, tree);
}

beforeEach(() => {
runner = new SchematicTestRunner('test', runfiles.resolvePackageRelative('../migrations.json'));
host = new TempScopedNodeJsSyncHost();
tree = new UnitTestTree(new HostTree(host));

writeFile(
'/tsconfig.json',
JSON.stringify({
compilerOptions: {
lib: ['es2015'],
strictNullChecks: true,
},
}),
);

writeFile(
'/angular.json',
JSON.stringify({
version: 1,
projects: {t: {root: '', architect: {build: {options: {tsConfig: './tsconfig.json'}}}}},
}),
);

tmpDirPath = getSystemPath(host.root);

// Switch into the temporary directory path. This allows us to run
// the schematic against our custom unit test tree.
shx.cd(tmpDirPath);
});

it('should update ExperimentalPendingTasks', async () => {
writeFile(
'/index.ts',
`
import {ExperimentalPendingTasks, Directive} from '@angular/core';
@Directive({
selector: '[someDirective]'
})
export class SomeDirective {
x = inject(ExperimentalPendingTasks);
}`,
);

await runMigration();

const content = tree.readContent('/index.ts').replace(/\s+/g, ' ');
expect(content).toContain("import {PendingTasks, Directive} from '@angular/core';");
expect(content).toContain('x = inject(PendingTasks);');
});

it('should update import alias', async () => {
writeFile(
'/index.ts',
`
import {ExperimentalPendingTasks as Y, Directive} from '@angular/core';
@Directive({
selector: '[someDirective]'
})
export class SomeDirective {
x = inject(Y);
}`,
);

await runMigration();

const content = tree.readContent('/index.ts').replace(/\s+/g, ' ');
expect(content).toContain("import {PendingTasks as Y, Directive} from '@angular/core';");
expect(content).toContain('x = inject(Y);');
});
});
4 changes: 2 additions & 2 deletions packages/core/src/application/application_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {ComponentFactory, ComponentRef} from '../linker/component_factory';
import {ComponentFactoryResolver} from '../linker/component_factory_resolver';
import {NgModuleRef} from '../linker/ng_module_factory';
import {ViewRef} from '../linker/view_ref';
import {PendingTasks} from '../pending_tasks';
import {PendingTasksInternal} from '../pending_tasks';
import {RendererFactory2} from '../render/api';
import {AfterRenderManager} from '../render3/after_render/manager';
import {ComponentFactory as R3ComponentFactory} from '../render3/component_ref';
Expand Down Expand Up @@ -359,7 +359,7 @@ export class ApplicationRef {
/**
* Returns an Observable that indicates when the application is stable or unstable.
*/
public readonly isStable: Observable<boolean> = inject(PendingTasks).hasPendingTasks.pipe(
public readonly isStable: Observable<boolean> = inject(PendingTasksInternal).hasPendingTasks.pipe(
map((pending) => !pending),
);

Expand Down
Loading

0 comments on commit 0067ae2

Please sign in to comment.