Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for @angular/core #16394

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/view/provider.ts
Expand Up @@ -355,6 +355,12 @@ export function resolveDep(
}
const tokenKey = depDef.tokenKey;

if (tokenKey === ChangeDetectorRefTokenKey) {
// directives on the same element as a component should be able to control the change detector
// of that component as well.
allowPrivateServices = !!(elDef && elDef.element !.componentView);
}

if (elDef && (depDef.flags & DepFlags.SkipSelf)) {
allowPrivateServices = false;
elDef = elDef.parent !;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/view/refs.ts
Expand Up @@ -90,7 +90,9 @@ class ComponentFactory_ extends ComponentFactory<any> {
const view = Services.createRootView(
injector, projectableNodes || [], rootSelectorOrNode, viewDef, ngModule, EMPTY_CONTEXT);
const component = asProviderData(view, componentNodeIndex).instance;
view.renderer.setAttribute(asElementData(view, 0).renderElement, 'ng-version', VERSION.full);
if (rootSelectorOrNode) {
view.renderer.setAttribute(asElementData(view, 0).renderElement, 'ng-version', VERSION.full);
}

return new ComponentRef_(view, new ViewRef_(view), component);
}
Expand Down Expand Up @@ -236,11 +238,11 @@ export class ViewRef_ implements EmbeddedViewRef<any>, InternalViewRef {
get destroyed(): boolean { return (this._view.state & ViewState.Destroyed) !== 0; }

markForCheck(): void { markParentViewsForCheck(this._view); }
detach(): void { this._view.state &= ~ViewState.ChecksEnabled; }
detach(): void { this._view.state &= ~ViewState.Attached; }
detectChanges(): void { Services.checkAndUpdateView(this._view); }
checkNoChanges(): void { Services.checkNoChangesView(this._view); }

reattach(): void { this._view.state |= ViewState.ChecksEnabled; }
reattach(): void { this._view.state |= ViewState.Attached; }
onDestroy(callback: Function) {
if (!this._view.disposables) {
this._view.disposables = [];
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/view/types.ts
Expand Up @@ -324,9 +324,12 @@ export interface ViewData {
*/
export const enum ViewState {
FirstCheck = 1 << 0,
ChecksEnabled = 1 << 1,
Errored = 1 << 2,
Destroyed = 1 << 3
Attached = 1 << 1,
ChecksEnabled = 1 << 2,
Errored = 1 << 3,
Destroyed = 1 << 4,

CatDetectChanges = Attached | ChecksEnabled,
}

export interface DisposableFn { (): void; }
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/view/view.ts
Expand Up @@ -211,7 +211,7 @@ function createView(
viewContainerParent: null, parentNodeDef,
context: null,
component: null, nodes,
state: ViewState.FirstCheck | ViewState.ChecksEnabled, root, renderer,
state: ViewState.FirstCheck | ViewState.CatDetectChanges, root, renderer,
oldValues: new Array(def.bindingCount), disposables
};
return view;
Expand Down Expand Up @@ -542,13 +542,13 @@ function callViewAction(view: ViewData, action: ViewAction) {
const viewState = view.state;
switch (action) {
case ViewAction.CheckNoChanges:
if ((viewState & ViewState.ChecksEnabled) &&
if ((viewState & ViewState.CatDetectChanges) === ViewState.CatDetectChanges &&
(viewState & (ViewState.Errored | ViewState.Destroyed)) === 0) {
checkNoChangesView(view);
}
break;
case ViewAction.CheckAndUpdate:
if ((viewState & ViewState.ChecksEnabled) &&
if ((viewState & ViewState.CatDetectChanges) === ViewState.CatDetectChanges &&
(viewState & (ViewState.Errored | ViewState.Destroyed)) === 0) {
checkAndUpdateView(view);
}
Expand Down
16 changes: 15 additions & 1 deletion packages/core/test/linker/change_detection_integration_spec.ts
Expand Up @@ -1175,6 +1175,21 @@ export function main() {
expect(renderLog.log).toEqual([]);
}));

it('Detached should disable OnPush', fakeAsync(() => {
const ctx = createCompFixture('<push-cmp [value]="value"></push-cmp>');
ctx.componentInstance.value = 0;
ctx.detectChanges();
renderLog.clear();

const cmp: CompWithRef = queryDirs(ctx.debugElement, PushComp)[0];
cmp.changeDetectorRef.detach();

ctx.componentInstance.value = 1;
ctx.detectChanges();

expect(renderLog.log).toEqual([]);
}));

it('Detached view can be checked locally', fakeAsync(() => {
const ctx = createCompFixture('<wrap-comp-with-ref></wrap-comp-with-ref>');
const cmp: CompWithRef = queryDirs(ctx.debugElement, CompWithRef)[0];
Expand Down Expand Up @@ -1225,7 +1240,6 @@ export function main() {

ctx.detectChanges();
expect(cmp.renderCount).toBe(count);

}));

});
Expand Down
20 changes: 19 additions & 1 deletion packages/core/test/linker/regression_integration_spec.ts
Expand Up @@ -6,9 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, ContentChild, Directive, InjectionToken, Injector, Input, Pipe, PipeTransform, Provider, QueryList, Renderer2, SimpleChanges, TemplateRef, ViewChildren, ViewContainerRef} from '@angular/core';
import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, ContentChild, Directive, InjectionToken, Injector, Input, NgModule, NgModuleRef, Pipe, PipeTransform, Provider, QueryList, Renderer2, SimpleChanges, TemplateRef, ViewChildren, ViewContainerRef} from '@angular/core';
import {TestBed, fakeAsync, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {expect} from '@angular/platform-browser/testing/src/matchers';

export function main() {
Expand Down Expand Up @@ -365,6 +366,23 @@ function declareTests({useJit}: {useJit: boolean}) {
expect(testDirs[1].tpl).toBeDefined();
expect(testDirs[2].tpl).toBeDefined();
});

it('should not add ng-version for dynamically created components', () => {
@Component({template: ''})
class App {
}

@NgModule({declarations: [App], entryComponents: [App]})
class MyModule {
}

const modRef = TestBed.configureTestingModule({imports: [MyModule]})
.get(NgModuleRef) as NgModuleRef<MyModule>;
const compRef =
modRef.componentFactoryResolver.resolveComponentFactory(App).create(Injector.NULL);

expect(getDOM().hasAttribute(compRef.location.nativeElement, 'ng-version')).toBe(false);
});
});
}

Expand Down
40 changes: 40 additions & 0 deletions packages/core/test/linker/view_injector_integration_spec.ts
Expand Up @@ -652,6 +652,46 @@ export function main() {
expect(compEl.nativeElement).toHaveText('1');
});

it('should inject ChangeDetectorRef of a same element component into a directive', () => {
TestBed.configureTestingModule(
{declarations: [PushComponentNeedsChangeDetectorRef, DirectiveNeedsChangeDetectorRef]});
const cf = createComponentFixture(
'<div componentNeedsChangeDetectorRef directiveNeedsChangeDetectorRef></div>');
cf.detectChanges();
const compEl = cf.debugElement.children[0];
const comp = compEl.injector.get(PushComponentNeedsChangeDetectorRef);
const dir = compEl.injector.get(DirectiveNeedsChangeDetectorRef);
comp.counter = 1;
cf.detectChanges();
expect(compEl.nativeElement).toHaveText('0');
dir.changeDetectorRef.markForCheck();
cf.detectChanges();
expect(compEl.nativeElement).toHaveText('1');
});

it(`should not inject ChangeDetectorRef of a parent element's component into a directive`, () => {
TestBed
.configureTestingModule({
declarations: [PushComponentNeedsChangeDetectorRef, DirectiveNeedsChangeDetectorRef]
})
.overrideComponent(
PushComponentNeedsChangeDetectorRef,
{set: {template: '<ng-content></ng-content>{{counter}}'}});
const cf = createComponentFixture(
'<div componentNeedsChangeDetectorRef><div directiveNeedsChangeDetectorRef></div></div>');
cf.detectChanges();
const compEl = cf.debugElement.children[0];
const comp = compEl.injector.get(PushComponentNeedsChangeDetectorRef);
const dirEl = compEl.children[0];
const dir = dirEl.injector.get(DirectiveNeedsChangeDetectorRef);
comp.counter = 1;
cf.detectChanges();
expect(compEl.nativeElement).toHaveText('0');
dir.changeDetectorRef.markForCheck();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is dir.changeDetectorRef in this case ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the changeDetectorRef that the directive received. The test verifies that it cannot change mark the component on the parent element as being dirty.

cf.detectChanges();
expect(compEl.nativeElement).toHaveText('0');
});

it('should inject ViewContainerRef', () => {
TestBed.configureTestingModule({declarations: [NeedsViewContainerRef]});
const el = createComponent('<div needsViewContainerRef></div>');
Expand Down