-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Description
🚀 feature request
Relevant Package
@angular/core
@angular/forms
Description
Currently Angular uses Zone.js to make DOM change detection on browsers events, async XHR etc.
There are quite few problems:
- Zero progress on pushing it to browsers as a standard. On the other hand Proxy is already supported by most browsers.
- There is no way to monkey patch async/await after they are fully supported on all browsers. Currently Zone.js is working just because async/await is converted to generators and promises. In the feature this is going to be a problem.
- ChangeDetectionStrategy, Zone.runOutsideAngular are needed just because angular is not smart enough.
Describe the solution you'd like
Mobx and Vue showed that observable based reactive change detection is a powerful and smart thing to do. Mobx 5 introduced Proxy based observables which overcomes problems with observing dynamically added properties. The only problem is that it is not supported on IE11 as it's not polyfillable. I propose that Angular should also use Proxy and observer pattern to do change detection.
Syntax:
import { Component, observable, watch, computed } from '@angular/core';
@Component({
selector: 'app',
template: `<div (click)="changeTitle()">{{title}}</div>`
})
export class AppComponent {
@observable title = 'hi';
@computed get reversedTitle() {
return this.title.split('').reverse().join('');
}
@action changeTitle() {
this.title = 'bye';
}
@watch() titleChange() {
console.log('new title', this.title);
}
}
Benefits:
- No zone.js is needed.
- No change detection strategy OnPush is needed, by default all components can work as OnPush.
- Useful and powerful helpers like watch and computed.
- RxJs may not be needed at all then working with reactive forms as observables will already allow to observe changes easily.
Implementation:
This is already working when using mobx with angular. But since most 3th party libraries depends on zone.js there is no way of removing it. Built in observables and reactions would be a huge win.
Migration
Since angular checks templates during AOT compilation we could write a tool which automatically adds observable decorators properties automatically where needed.