Skip to content

Commit

Permalink
fix(core): WIP - support Attribute DI decorator in deps section o…
Browse files Browse the repository at this point in the history
…f a token

Adding some random content to satisfy the 100 chars min length
  • Loading branch information
sonukapoor committed May 14, 2020
1 parent 4f3ac1d commit 014b123
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/src/core_render3_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
SWITCH_COMPILE_INJECTABLE__POST_R3__ as ɵSWITCH_COMPILE_INJECTABLE__POST_R3__,
} from './di/injectable';
export {INJECTOR_IMPL__POST_R3__ as ɵINJECTOR_IMPL__POST_R3__} from './di/injector';
export {CREATE_ATTRIBUTE_DECORATOR__POST_R3__ as ɵCREATE_ATTRIBUTE_DECORATOR__POST_R3__} from './di/metadata';
export {
NG_INJ_DEF as ɵNG_INJ_DEF,
NG_PROV_DEF as ɵNG_PROV_DEF,
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/di/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {makeParamDecorator} from '../util/decorators';

import {ɵɵinjectAttribute} from '../render3/instructions/di';


/**
Expand Down Expand Up @@ -277,11 +277,25 @@ export interface Attribute {
attributeName?: string;
}

function CREATE_ATTRIBUTE_DECORATOR__PRE_R3__(): AttributeDecorator {
return makeParamDecorator(
'Attribute',
(attributeName?: string) => ({attributeName}));
}

export function CREATE_ATTRIBUTE_DECORATOR__POST_R3__(): AttributeDecorator {
return makeParamDecorator(
'Attribute',
(attributeName?: string) =>
({attributeName, __NG_ELEMENT_ID__: () => ɵɵinjectAttribute(attributeName!)}));
}

const CREATE_ATTRIBUTE_DECORATOR_IMPL = CREATE_ATTRIBUTE_DECORATOR__PRE_R3__;

/**
* Attribute decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const Attribute: AttributeDecorator =
makeParamDecorator('Attribute', (attributeName?: string) => ({attributeName}));
*/
export const Attribute: AttributeDecorator = CREATE_ATTRIBUTE_DECORATOR_IMPL();
32 changes: 32 additions & 0 deletions packages/core/test/acceptance/di_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,38 @@ describe('di', () => {
});
});

describe('attribute tokens', () => {
it('should be able to provide an attribute token', () => {
const TOKEN = new InjectionToken<string>('Some token');
function factory(token: string): string {
return token + ' with factory';
}
@Component({
selector: 'my-comp',
template: '...',
providers: [{
provide: TOKEN,
deps: [[new Attribute('token')]],
useFactory: factory,
}]
})
class MyComp {
constructor(@Inject(TOKEN) readonly token: string) {}
}

@Component({template: `<my-comp token='token'></my-comp>`})
class WrapperComp {
@ViewChild(MyComp) myComp!: MyComp;
}

TestBed.configureTestingModule({declarations: [MyComp, WrapperComp]});

const fixture = TestBed.createComponent(WrapperComp);
fixture.detectChanges();
expect(fixture.componentInstance.myComp.token).toBe('token with factory');
});
});

it('should not cause cyclic dependency if same token is requested in deps with @SkipSelf', () => {
@Component({
selector: 'my-comp',
Expand Down

0 comments on commit 014b123

Please sign in to comment.