Skip to content

Commit 4642919

Browse files
feat(component): add RenderScheduler to the public API (#3516)
1 parent d97e75a commit 4642919

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

modules/component/src/core/render-scheduler.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,71 @@
11
import { ChangeDetectorRef, inject, Injectable } from '@angular/core';
22
import { TickScheduler } from './tick-scheduler';
33

4+
/**
5+
* Provides rendering functionality regardless of whether `zone.js` is present
6+
* or not. It must be provided at the component/directive level.
7+
*
8+
* @usageNotes
9+
*
10+
* ### Rerender zone-less app on route changes
11+
*
12+
* ```ts
13+
* @Component({
14+
* selector: 'app-root',
15+
* template: '<router-outlet>',
16+
* // 👇 `RenderScheduler` is provided at the component level
17+
* providers: [RenderScheduler],
18+
* changeDetection: ChangeDetectionStrategy.OnPush,
19+
* })
20+
* export class AppComponent implements OnInit {
21+
* constructor(
22+
* private readonly router: Router,
23+
* private readonly renderScheduler: RenderScheduler
24+
* ) {}
25+
*
26+
* ngOnInit(): void {
27+
* this.router.events
28+
* .pipe(filter((e) => e instanceof NavigationEnd))
29+
* .subscribe(() => this.renderScheduler.schedule());
30+
* }
31+
* }
32+
* ```
33+
*
34+
* ### Rerender component on interval
35+
*
36+
* ```ts
37+
* @Component({
38+
* selector: 'app-interval',
39+
* template: '{{ elapsedTime }}ms',
40+
* // 👇 `RenderScheduler` is provided at the component level
41+
* providers: [RenderScheduler],
42+
* changeDetection: ChangeDetectionStrategy.OnPush,
43+
* })
44+
* export class IntervalComponent implements OnInit {
45+
* elapsedTime = 0;
46+
*
47+
* constructor(private readonly renderScheduler: RenderScheduler) {}
48+
*
49+
* ngOnInit(): void {
50+
* setInterval(() => {
51+
* this.elapsedTime += 1000;
52+
* this.renderScheduler.schedule();
53+
* }, 1000);
54+
* }
55+
* }
56+
* ```
57+
*/
458
@Injectable()
559
export class RenderScheduler {
660
constructor(
761
private readonly cdRef: ChangeDetectorRef,
862
private readonly tickScheduler: TickScheduler
963
) {}
1064

65+
/**
66+
* Marks component and its ancestors as dirty.
67+
* It also schedules a new change detection cycle in zone-less mode.
68+
*/
1169
schedule(): void {
1270
this.cdRef.markForCheck();
1371
this.tickScheduler.schedule();

modules/component/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { RenderScheduler } from './core/render-scheduler';
12
export { LetDirective } from './let/let.directive';
23
export { LetModule } from './let/let.module';
34
export { PushPipe } from './push/push.pipe';

0 commit comments

Comments
 (0)