Skip to content

Commit 211a44a

Browse files
committed
Add zone context questions
1 parent 557b639 commit 211a44a

File tree

1 file changed

+96
-10
lines changed

1 file changed

+96
-10
lines changed

README.md

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@
252252
|244| [What is NoopZone?](#what-is-noopzone)|
253253
|245| [How do you create displayBlock components?](#how-do-you-create-displayblock-components)|
254254
|246| [What are the possible data change scenarios for change detection?](#what-are-the-possible-data-change-scenarios-for-change-detection)|
255-
|247| [?](#)|
256-
|248| [?](#)|
257-
|249| [?](#)|
258-
|250| [?](#)|
255+
|247| [What is a zone context?](#what-is-a-zone-context)|
256+
|248| [What are the lifecycle hooks of a zone?](#what-are-the-lifecycle-hooks-of-a-zone)|
257+
|249| [Which are the methods of NgZone used to control change detection?](#which-are-the-methods-of-ngzone-used-to-control-change-detection)|
258+
|250| [How do you change the settings of zonejs?](#how-do-you-change-the-settings-of-zonejs)|
259259
|251| [?](#)|
260260

261261

@@ -3560,6 +3560,9 @@
35603560
**[⬆ Back to Top](#table-of-contents)**
35613561
35623562
243. ### What is NgZone?
3563+
Angular provides a service called NgZone which creates a zone named `angular` to automatically trigger change detection when the following conditions are satisfied.
3564+
1. When a sync or async function is executed.
3565+
2. When there is no microTask scheduled.
35633566
35643567
**[⬆ Back to Top](#table-of-contents)**
35653568
@@ -3644,20 +3647,103 @@
36443647
36453648
**[⬆ Back to Top](#table-of-contents)**
36463649
3647-
247. ### ?
3648-
3650+
247. ### What is a zone context?
3651+
Execution Context is an abstract concept that holds information about the environment within the current code being executed. A zone provides an execution context that persists across asynchronous operations is called as zone context. For example, the zone context will be same in both outside and inside setTimeout callback function,
3652+
```js
3653+
zone.run(() => {
3654+
// outside zone
3655+
expect(zoneThis).toBe(zone);
3656+
setTimeout(function() {
3657+
// the same outside zone exist here
3658+
expect(zoneThis).toBe(zone);
3659+
});
3660+
});
3661+
```
3662+
The current zone is retrieved through `Zone.current`.
36493663
**[⬆ Back to Top](#table-of-contents)**
36503664
3651-
248. ### ?
3665+
248. ### What are the lifecycle hooks of a zone?
3666+
There are four lifecycle hooks for asynchronous operations from zone.js.
3667+
1. **onScheduleTask:** This hook triggers when a new asynchronous task is scheduled. For example, when you call setTimeout()
3668+
```js
3669+
onScheduleTask: function(delegate, curr, target, task) {
3670+
console.log('new task is scheduled:', task.type, task.source);
3671+
return delegate.scheduleTask(target, task);
3672+
}
3673+
```
3674+
2. **onInvokeTask:** This hook triggers when an asynchronous task is about to execute. For example, when the callback of setTimeout() is about to execute.
3675+
```js
3676+
onInvokeTask: function(delegate, curr, target, task, applyThis, applyArgs) {
3677+
console.log('task will be invoked:', task.type, task.source);
3678+
return delegate.invokeTask(target, task, applyThis, applyArgs);
3679+
}
3680+
```
3681+
3. **onHasTask:** This hook triggers when the status of one kind of task inside a zone changes from stable(no tasks in the zone) to unstable(a new task is scheduled in the zone) or from unstable to stable.
3682+
```js
3683+
onHasTask: function(delegate, curr, target, hasTaskState) {
3684+
console.log('task state changed in the zone:', hasTaskState);
3685+
return delegate.hasTask(target, hasTaskState);
3686+
}
3687+
```
3688+
4. **onInvoke:** This hook triggers when a synchronous function is going to execute in the zone.
3689+
```js
3690+
onInvoke: function(delegate, curr, target, callback, applyThis, applyArgs) {
3691+
console.log('the callback will be invoked:', callback);
3692+
return delegate.invoke(target, callback, applyThis, applyArgs);
3693+
}
3694+
```
36523695
36533696
**[⬆ Back to Top](#table-of-contents)**
36543697
3655-
249. ### ?
3656-
3698+
249. ### What are the methods of NgZone used to control change detection?
3699+
NgZone service provides a `run()` method that allows you to execute a function inside the angular zone. This function is used to execute third party APIs which are not handled by Zone and trigger change detection automatically at the correct time.
3700+
```js
3701+
export class AppComponent implements OnInit {
3702+
constructor(private ngZone: NgZone) {}
3703+
ngOnInit() {
3704+
// use ngZone.run() to make the asynchronous operation in the angular zone
3705+
this.ngZone.run(() => {
3706+
someNewAsyncAPI(() => {
3707+
// update the data of the component
3708+
});
3709+
});
3710+
}
3711+
}
3712+
```
3713+
Whereas `runOutsideAngular()` method is used when you don't want to trigger change detection.
3714+
```js
3715+
export class AppComponent implements OnInit {
3716+
constructor(private ngZone: NgZone) {}
3717+
ngOnInit() {
3718+
// Use this method when you know no data will be updated
3719+
this.ngZone.runOutsideAngular(() => {
3720+
setTimeout(() => {
3721+
// update component data and don't trigger change detection
3722+
});
3723+
});
3724+
}
3725+
}
3726+
```
36573727
**[⬆ Back to Top](#table-of-contents)**
36583728
3659-
250. ### ?
3729+
250. ### How do you change the settings of zonejs?
3730+
You can change the settings of zone by configuring them in a separate file and import it just after zonejs import.
3731+
For example, you can disable the requestAnimationFrame() monkey patch to prevent change detection for no data update as one setting and prevent DOM events(a mousemove or scroll event) to trigger change detection. Let's say the new file named zone-flags.js,
3732+
```js
3733+
// disable patching requestAnimationFrame
3734+
(window as any).__Zone_disable_requestAnimationFrame = true;
36603735
3736+
// disable patching specified eventNames
3737+
(window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove'];
3738+
```
3739+
The above configuration file can be imported in a polyfill.ts file as below,
3740+
```js
3741+
/***************************************************************************************************
3742+
* Zone JS is required by default for Angular.
3743+
*/
3744+
import `./zone-flags`;
3745+
import 'zone.js/dist/zone'; // Included with Angular CLI.
3746+
```
36613747
**[⬆ Back to Top](#table-of-contents)**
36623748
36633749
251. ### ?

0 commit comments

Comments
 (0)