fix(ivy): markForCheck() should not schedule change detection (#28048)
Previously, we had the logic to schedule a change detection tick inside markViewDirty(). This is fine when used in markDirty(), the user-facing API, because it should always schedule change detection. However, this doesn't work when used in markForCheck() because historically markForCheck() does not trigger change detection. To be backwards compatible, this commit moves the scheduling logic out of markViewDirty() and into markDirty(), so markForCheck no longer triggers a tick. PR Close #28048
This commit is contained in:
parent
feebe03523
commit
ad6569c744
|
@ -2376,17 +2376,24 @@ function wrapListenerWithPreventDefault(listenerFn: (e?: any) => any): EventList
|
|||
};
|
||||
}
|
||||
|
||||
/** Marks current view and all ancestors dirty */
|
||||
export function markViewDirty(lView: LView): void {
|
||||
/**
|
||||
* Marks current view and all ancestors dirty.
|
||||
*
|
||||
* Returns the root view because it is found as a byproduct of marking the view tree
|
||||
* dirty, and can be used by methods that consume markViewDirty() to easily schedule
|
||||
* change detection. Otherwise, such methods would need to traverse up the view tree
|
||||
* an additional time to get the root view and schedule a tick on it.
|
||||
*
|
||||
* @param lView The starting LView to mark dirty
|
||||
* @returns the root LView
|
||||
*/
|
||||
export function markViewDirty(lView: LView): LView {
|
||||
while (lView && !(lView[FLAGS] & LViewFlags.IsRoot)) {
|
||||
lView[FLAGS] |= LViewFlags.Dirty;
|
||||
lView = lView[PARENT] !;
|
||||
}
|
||||
lView[FLAGS] |= LViewFlags.Dirty;
|
||||
ngDevMode && assertDefined(lView[CONTEXT], 'rootContext should be defined');
|
||||
|
||||
const rootContext = lView[CONTEXT] as RootContext;
|
||||
scheduleTick(rootContext, RootContextFlags.DetectChanges);
|
||||
return lView;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2575,7 +2582,10 @@ function updateViewQuery<T>(viewQuery: ComponentQuery<{}>| null, view: LView, co
|
|||
*/
|
||||
export function markDirty<T>(component: T) {
|
||||
ngDevMode && assertDefined(component, 'component');
|
||||
markViewDirty(getComponentViewByInstance(component));
|
||||
const rootView = markViewDirty(getComponentViewByInstance(component));
|
||||
|
||||
ngDevMode && assertDefined(rootView[CONTEXT], 'rootContext should be defined');
|
||||
scheduleTick(rootView[CONTEXT] as RootContext, RootContextFlags.DetectChanges);
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
|
|
|
@ -946,48 +946,50 @@ describe('change detection', () => {
|
|||
});
|
||||
}
|
||||
|
||||
it('should schedule check on OnPush components', () => {
|
||||
const parent = renderComponent(OnPushParent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
it('should ensure OnPush components are checked', () => {
|
||||
const fixture = new ComponentFixture(OnPushParent);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
comp.value = 'two';
|
||||
tick(parent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
comp.cdr.markForCheck();
|
||||
requestAnimationFrame.flush();
|
||||
expect(getRenderedText(parent)).toEqual('one - two');
|
||||
|
||||
// Change detection should not have run yet, since markForCheck
|
||||
// does not itself schedule change detection.
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - two');
|
||||
});
|
||||
|
||||
it('should only run change detection once with multiple calls to markForCheck', () => {
|
||||
renderComponent(OnPushParent);
|
||||
it('should never schedule change detection on its own', () => {
|
||||
const fixture = new ComponentFixture(OnPushParent);
|
||||
expect(comp.doCheckCount).toEqual(1);
|
||||
|
||||
comp.cdr.markForCheck();
|
||||
comp.cdr.markForCheck();
|
||||
comp.cdr.markForCheck();
|
||||
comp.cdr.markForCheck();
|
||||
comp.cdr.markForCheck();
|
||||
requestAnimationFrame.flush();
|
||||
|
||||
expect(comp.doCheckCount).toEqual(2);
|
||||
expect(comp.doCheckCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('should schedule check on ancestor OnPush components', () => {
|
||||
const parent = renderComponent(OnPushParent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
it('should ensure ancestor OnPush components are checked', () => {
|
||||
const fixture = new ComponentFixture(OnPushParent);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
parent.value = 'two';
|
||||
tick(parent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
fixture.component.value = 'two';
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
comp.cdr.markForCheck();
|
||||
requestAnimationFrame.flush();
|
||||
expect(getRenderedText(parent)).toEqual('two - one');
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('two - one');
|
||||
|
||||
});
|
||||
|
||||
it('should schedule check on OnPush components in embedded views', () => {
|
||||
it('should ensure OnPush components in embedded views are checked', () => {
|
||||
class EmbeddedViewParent {
|
||||
value = 'one';
|
||||
showing = true;
|
||||
|
@ -1029,24 +1031,27 @@ describe('change detection', () => {
|
|||
});
|
||||
}
|
||||
|
||||
const parent = renderComponent(EmbeddedViewParent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
const fixture = new ComponentFixture(EmbeddedViewParent);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
comp.value = 'two';
|
||||
tick(parent);
|
||||
expect(getRenderedText(parent)).toEqual('one - one');
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
comp.cdr.markForCheck();
|
||||
requestAnimationFrame.flush();
|
||||
expect(getRenderedText(parent)).toEqual('one - two');
|
||||
// markForCheck should not trigger change detection on its own.
|
||||
expect(fixture.hostElement.textContent).toEqual('one - one');
|
||||
|
||||
parent.value = 'two';
|
||||
tick(parent);
|
||||
expect(getRenderedText(parent)).toEqual('one - two');
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - two');
|
||||
|
||||
fixture.component.value = 'two';
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('one - two');
|
||||
|
||||
comp.cdr.markForCheck();
|
||||
requestAnimationFrame.flush();
|
||||
expect(getRenderedText(parent)).toEqual('two - two');
|
||||
tick(fixture.component);
|
||||
expect(fixture.hostElement.textContent).toEqual('two - two');
|
||||
});
|
||||
|
||||
// TODO(kara): add test for dynamic views once bug fix is in
|
||||
|
|
|
@ -451,8 +451,7 @@ describe('Integration', () => {
|
|||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should work when an outlet is in an ngIf',
|
||||
it('should work when an outlet is in an ngIf',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
|
@ -468,8 +467,7 @@ describe('Integration', () => {
|
|||
expect(location.path()).toEqual('/child/simple');
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should work when an outlet is added/removed', fakeAsync(() => {
|
||||
it('should work when an outlet is added/removed', fakeAsync(() => {
|
||||
@Component({
|
||||
selector: 'someRoot',
|
||||
template: `[<div *ngIf="cond"><router-outlet></router-outlet></div>]`
|
||||
|
@ -633,18 +631,15 @@ describe('Integration', () => {
|
|||
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should navigate back and forward',
|
||||
it('should navigate back and forward',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig([{
|
||||
path: 'team/:id',
|
||||
component: TeamCmp,
|
||||
children: [
|
||||
{path: 'simple', component: SimpleCmp},
|
||||
{path: 'user/:name', component: UserCmp}
|
||||
]
|
||||
children:
|
||||
[{path: 'simple', component: SimpleCmp}, {path: 'user/:name', component: UserCmp}]
|
||||
}]);
|
||||
|
||||
let event: NavigationStart;
|
||||
|
@ -1006,9 +1001,7 @@ describe('Integration', () => {
|
|||
]);
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should handle failed navigations gracefully',
|
||||
fakeAsync(inject([Router], (router: Router) => {
|
||||
it('should handle failed navigations gracefully', fakeAsync(inject([Router], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig([{path: 'user/:name', component: UserCmp}]);
|
||||
|
@ -1883,14 +1876,11 @@ describe('Integration', () => {
|
|||
});
|
||||
|
||||
describe('redirects', () => {
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should work',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
it('should work', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig([
|
||||
{path: 'old/team/:id', redirectTo: 'team/:id'},
|
||||
{path: 'team/:id', component: TeamCmp}
|
||||
{path: 'old/team/:id', redirectTo: 'team/:id'}, {path: 'team/:id', component: TeamCmp}
|
||||
]);
|
||||
|
||||
router.navigateByUrl('old/team/22');
|
||||
|
@ -1899,15 +1889,13 @@ describe('Integration', () => {
|
|||
expect(location.path()).toEqual('/team/22');
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should update Navigation object after redirects are applied',
|
||||
it('should update Navigation object after redirects are applied',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
let initialUrl, afterRedirectUrl;
|
||||
|
||||
router.resetConfig([
|
||||
{path: 'old/team/:id', redirectTo: 'team/:id'},
|
||||
{path: 'team/:id', component: TeamCmp}
|
||||
{path: 'old/team/:id', redirectTo: 'team/:id'}, {path: 'team/:id', component: TeamCmp}
|
||||
]);
|
||||
|
||||
router.events.subscribe(e => {
|
||||
|
@ -2031,9 +2019,7 @@ describe('Integration', () => {
|
|||
});
|
||||
});
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('works',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig(
|
||||
|
@ -2054,9 +2040,7 @@ describe('Integration', () => {
|
|||
|
||||
beforeEach(() => { TestBed.configureTestingModule({providers: [AlwaysTrue]}); });
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('works',
|
||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
router.resetConfig(
|
||||
|
@ -3355,8 +3339,7 @@ describe('Integration', () => {
|
|||
});
|
||||
|
||||
describe('route events', () => {
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should fire matching (Child)ActivationStart/End events',
|
||||
it('should fire matching (Child)ActivationStart/End events',
|
||||
fakeAsync(inject([Router], (router: Router) => {
|
||||
const fixture = createRoot(router, RootCmp);
|
||||
|
||||
|
@ -4528,8 +4511,7 @@ describe('Integration', () => {
|
|||
expect(simpleCmp1).not.toBe(simpleCmp2);
|
||||
})));
|
||||
|
||||
fixmeIvy('FW-768: markViewDirty instruction is scheduling a tick')
|
||||
.it('should not mount the component of the previously reused route when the outlet was not instantiated at the time of route activation',
|
||||
it('should not mount the component of the previously reused route when the outlet was not instantiated at the time of route activation',
|
||||
fakeAsync(() => {
|
||||
@Component({
|
||||
selector: 'root-cmp',
|
||||
|
|
Loading…
Reference in New Issue