Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26fc49d

Browse files
authored
Update README.md
1 parent 6c60dbc commit 26fc49d

File tree

1 file changed

+78
-15
lines changed

1 file changed

+78
-15
lines changed

libs/cdk/zone-less/docs/README.md

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,62 @@ A demo application is available on [GitHub](https://github.com/BioPhoton/rx-angu
55

66
# Motivation
77

8-
# Available Approaches
8+
By default, Angular or better say zone.js patches most of the asynchronouse API's of the Browser.
9+
This adds additional overhead on those API's and even more important, this leads to un-nessecary renderings of the Angular component tree, also known as overredering.
910

10-
- `ngZone#runOutsideZone`
11-
- RxAngular CDK/Zone-Less
11+
Until now developers where only able to either disable zone.js completely or wrapping another logic around the patched APIs to avoid change detection calls.
12+
This of course requires to inject another service and also makes your code more cluttery and slow.
1213

13-
**The Benefits of RxAngular**
14+
The zone-less package helps out here.
15+
It provides a general method to access the unpatched version of an API and also ships a set of commonly used APIs of the Browser as well as RxJS.
16+
All those APIs are fully independent of zone.js and NgZone (which is normally used to run things outside Angular).
1417

15-
-
1618

17-
## `ngZone#runOutsideZone`
19+
# The Benefits
1820

19-
**Event Bubbling of smae event on multiple elements**
21+
- ✅ Fastest option possible
22+
- ✅ Drop-in replacement for navive APIs
23+
- ✅ Drop-in replacement for RxJS functions
24+
- ✅ No need for `ngZone#runOutsideZone`
2025

21-
```html
22-
<div (click)="doStuff()">
23-
<button (click)="doStuff()">click</button>
24-
</div>
25-
```
26+
# RxAngular CDK/Zone-Less
27+
28+
The way how zone.js works is, it patches all relevant async APIs of the Browser. This happens early on as zone.js is treated as polyfill.
29+
30+
![rx-angular-cdk-zone-less_API-patching_michael-hladky](https://user-images.githubusercontent.com/10064416/129472845-e27c5a52-f99d-4f5f-b205-4e947e188d25.png)
2631

27-
# RxAngular Zone-Less
32+
All original logic of the patched API's is stored under a symbol in `window`. You can check that by logging for example the following value: `console.log(window.__zone_symbol__setTimeout)`.
33+
This will print the original unpatched API to the Brwosers console.
34+
35+
Internally the symbols and in turn the unpatched API's is what the zone-less package is using.
36+
The essential method used to get hold of the unpatched APIs is called `getZoneUnPatchedApi`. It takes a target i.e. `window` and a name of the method we want to retreive the unpatched version of.
37+
38+
39+
## Usage
2840

2941
**utils**
3042

3143
- getZoneUnPatchedApi
3244

45+
```typescript
46+
@Component({
47+
// ...
48+
})
49+
export class AppComponent {
50+
51+
doStuff() {
52+
getZoneUnPatchedApi('setTimeout')(
53+
() => console.log('tada!'),
54+
300);
55+
};
56+
57+
doOtherStuff() {
58+
getZoneUnPatchedApi(elem, 'addEventListener')('click', () => console.log('tada!') );
59+
};
60+
61+
}
62+
```
63+
3364
**browser**
3465

3566
- requestAnimationFrame
@@ -47,11 +78,43 @@ creation
4778
- interval
4879
- timer
4980

50-
operators
51-
5281
scheduler
5382

5483
- async
5584
- asap
5685
- queue
5786
- animationFrame
87+
88+
89+
# Available Approaches
90+
91+
- `ngZone#runOutsideZone`
92+
- `zone-configuration`
93+
94+
## `ngZone#runOutsideAngular`
95+
96+
It is possible to inject an instance of NgZone into components and services etc to tell Angular to run a specific peice of coe outside of Angular.
97+
98+
```typescript
99+
@Component({
100+
// ...
101+
})
102+
export class AppComponent {
103+
104+
constructor(private ngZone: NgZone) {
105+
}
106+
107+
doStuff() {
108+
109+
this.ngZone.runOutsideAngular(() => {
110+
setTimeout(() => {
111+
console.log('tada!');
112+
}, 300)
113+
});
114+
115+
}
116+
117+
}
118+
```
119+
120+
The downside here is we need to inject `NgZone` and rely on dependency injection wich is not only more code but also very slow.

0 commit comments

Comments
 (0)