fix(upgrade): detect async downgrade component changes (#14039)
This commit effectively reverts 7e0f02f
but for `upgrade/static`
as it was an invalid fix for #6385, that created a more significant
bug, which was that changes were not always being detected.
Angular 1 digests should be run inside the ngZone to ensure
that async changes are detected.
We don't know how to fix #6385 without breaking change detection
at this stage. That issue is triggered by async operations, such as
`setTimeout`, being triggered inside scope watcher functions.
One could argue that watcher functions should be pure and not do
work such as triggering async operations. It is possible that the
original use case could be supported by moving the debounce
logic into the watch listener function, which is only called if the
watched value actually changes.
See #13812
PR Close #14039
This commit is contained in:

committed by
Miško Hevery

parent
777ba46837
commit
117fa79c7c
@ -170,12 +170,13 @@ export class UpgradeModule {
|
||||
const injector = this.injector;
|
||||
// Cannot use arrow function below because we need the context
|
||||
const newWhenStable = function(callback: Function) {
|
||||
originalWhenStable.call(this, function() {
|
||||
originalWhenStable.call(testabilityDelegate, function() {
|
||||
const ng2Testability: Testability = injector.get(Testability);
|
||||
if (ng2Testability.isStable()) {
|
||||
callback.apply(this, arguments);
|
||||
callback();
|
||||
} else {
|
||||
ng2Testability.whenStable(newWhenStable.bind(this, callback));
|
||||
ng2Testability.whenStable(
|
||||
newWhenStable.bind(testabilityDelegate, callback));
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -201,9 +202,14 @@ export class UpgradeModule {
|
||||
angular.element(element).data(controllerKey(INJECTOR_KEY), this.injector);
|
||||
|
||||
// Wire up the ng1 rootScope to run a digest cycle whenever the zone settles
|
||||
const $rootScope = $injector.get('$rootScope');
|
||||
this.ngZone.onMicrotaskEmpty.subscribe(
|
||||
() => this.ngZone.runOutsideAngular(() => $rootScope.$evalAsync()));
|
||||
// We need to do this in the next tick so that we don't prevent the bootup
|
||||
// stabilizing
|
||||
setTimeout(() => {
|
||||
const $rootScope = $injector.get('$rootScope');
|
||||
const subscription =
|
||||
this.ngZone.onMicrotaskEmpty.subscribe(() => $rootScope.$digest());
|
||||
$rootScope.$on('$destroy', () => { subscription.unsubscribe(); });
|
||||
}, 0);
|
||||
}
|
||||
]);
|
||||
|
||||
|
Reference in New Issue
Block a user