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

feat(ivy): support generation of flags for directive injection #23345

Closed
wants to merge 4 commits into from

Conversation

mhevery
Copy link
Contributor

@mhevery mhevery commented Apr 12, 2018

This change changes:

Compiler portion of #23342
Prep for #23330

feat(ivy): support injection even if no injector present

  • Remove default injection value from inject / directiveInject since
    it is not possible to set using annotations.
  • Module Injector is stored on LView instead of LInjector data
    structure because it can change only at LView level. (More efficient)
  • Add ngInjectableDef to IterableDiffers so that existing tests can
    pass as well as enable IterableDiffers to be injectable without
    Injector

@mhevery mhevery requested a review from kara April 12, 2018 19:30
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Looks good to me, except for the fdescribe

import {MockDirectory, setup} from '../aot/test_util';
import {compile, expectEmit} from './mock_compile';

fdescribe('compiler compliance: dependency injection', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove fdescribe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

* @param token The token associated with the error
* @returns The error that was created
*/
function createInjectionError(text: string, token: any) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to remove this? Looks like it's still used in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Working on two things in one PR, reverted

@mhevery mhevery force-pushed the injection_fix branch 3 times, most recently from 520e435 to 644e714 Compare April 12, 2018 19:41
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM

@mary-poppins
Copy link

You can preview 4d6700a at https://pr23345-4d6700a.ngbuilds.io/.

@mhevery mhevery force-pushed the injection_fix branch 2 times, most recently from 7b8bcef to 8fd7d95 Compare April 12, 2018 20:05
token.identifier != null ? outputCtx.importExpr(tokenRef) : o.literal(tokenRef);
args.push(o.importExpr(R3.inject).callFn([value]));
const directiveInjectArgs = [tokenValue];
let flags = InjectFlags.Default;
Copy link
Contributor

Choose a reason for hiding this comment

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

const flags = moveThatLogicFurtherDown(dependency); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

extracted

};

// The template should look like this (where IDENT is a wild card for an identifier):
const template = `
Copy link
Contributor

@vicb vicb Apr 12, 2018

Choose a reason for hiding this comment

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

rename template to factory or injectxxxyyy ? (comment, var name and expect)
(I'm also ok with removing this comment)
Use $r3$ instead of IDENT for consistency with other tests ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@mary-poppins
Copy link

You can preview 7b8bcef at https://pr23345-7b8bcef.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 8fd7d95 at https://pr23345-8fd7d95.ngbuilds.io/.

@mhevery mhevery added the target: major This PR is targeted for the next major release label Apr 12, 2018
@mary-poppins
Copy link

You can preview c75e17f at https://pr23345-c75e17f.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 49cb591 at https://pr23345-49cb591.ngbuilds.io/.

// TODO(misko) Fix injection
// constructor(todoStore: TodoStore) { this.todoStore = todoStore; }
constructor() { this.todoStore = new TodoStore(); }
constructor(todoStore: TodoStore) { this.todoStore = todoStore; }
Copy link
Contributor

Choose a reason for hiding this comment

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

constructor(public todoStore: TodoStore) {} should work now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@mary-poppins
Copy link

You can preview 493ab9b at https://pr23345-493ab9b.ngbuilds.io/.

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

A few small typos, otherwise looks good to me

}

class MyComponent {
constructor(public mySerivce: MyService) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to myService ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -397,6 +397,35 @@ describe('di', () => {
expect(log).toEqual(['DirB', 'DirB', 'DirA (dep: DirB - 2)']);
});

it('should crete instance even when no injector present', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

crete -> create

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@mhevery mhevery force-pushed the injection_fix branch 2 times, most recently from 019b4c1 to e2a3a2f Compare April 13, 2018 00:57
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM

@mary-poppins
Copy link

You can preview 091c9e1 at https://pr23345-091c9e1.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 019b4c1 at https://pr23345-019b4c1.ngbuilds.io/.

@mary-poppins
Copy link

You can preview e2a3a2f at https://pr23345-e2a3a2f.ngbuilds.io/.

@@ -107,6 +121,7 @@ export function defineInjectable<T>(opts: {
return {
providedIn: (opts.providedIn as InjectorType<any>| 'root' | null | undefined) || null,
Copy link
Contributor

Choose a reason for hiding this comment

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

no 'any' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

} else if (_currentInjector === null) {
const injectableDef: InjectableDef<T> = (token as any).ngInjectableDef;
if (injectableDef && injectableDef.providedIn == 'root') {
return injectableDef.value === undefined ? injectableDef.value = injectableDef.factory() :
Copy link
Contributor

Choose a reason for hiding this comment

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

do we want to be able to provide undefined by using a special value (ie {}) instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about it, and decided that it is not worth the complexity. We can always added later.

@@ -189,6 +190,11 @@ export interface LView {
* Queries active for this view - nodes from a view are reported to those queries
*/
queries: LQueries|null;

/**
* Injector to be used (if any).
Copy link
Contributor

Choose a reason for hiding this comment

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

useful comment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated comment.

@@ -135,6 +137,11 @@ export interface IterableDifferFactory {
*
*/
export class IterableDiffers {
static ngInjectableDef: InjectableDef<IterableDiffers> = defineInjectable({
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would compilation result lives in source code? Shouldn't it using @Injectable() decorator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Few reasons:
1.) we currently don't compile core with ivy so the decorator does not get lowered.
2.) There is a circular dependency between the compiler and core which makes it hard for the compiler to compile tho core. Rather than solving that, we have opted for hand coding it.

Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should probably be in the source code

@mhevery mhevery added action: merge The PR is ready for merge by the caretaker and removed action: merge The PR is ready for merge by the caretaker labels Apr 13, 2018
@mary-poppins
Copy link

You can preview 27674e3 at https://pr23345-27674e3.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 685546e at https://pr23345-685546e.ngbuilds.io/.

@IgorMinar IgorMinar added state: blocked and removed action: merge The PR is ready for merge by the caretaker labels Apr 13, 2018
@IgorMinar
Copy link
Contributor

this PR changes the API surface. @mhevery let's discuss tomorrow before merging this.

@mhevery mhevery added action: merge The PR is ready for merge by the caretaker and removed state: blocked labels Apr 13, 2018
@mhevery
Copy link
Contributor Author

mhevery commented Apr 13, 2018

Discussed with @IgorMinar and we agreed to merge.

@IgorMinar
Copy link
Contributor

we agreed because the api is wrong and needs to be corrected before the release.

This change changes:
- compiler uses `directiveInject` instead of `inject` for `Directive`s
- unifies the flags in `di` as well as `render3`
- changes the signature of `directiveInject` to match `inject` In prep for angular#23330
- compiler now generates flags for injection.

Compiler portion of angular#23342
Prep for angular#23330
- Remove default injection value from `inject` / `directiveInject` since
  it is not possible to set using annotations.
- Module `Injector` is stored on `LView` instead of `LInjector` data
  structure because it can change only at `LView` level. (More efficient)
- Add `ngInjectableDef` to `IterableDiffers` so that existing tests can
  pass as well as enable `IterableDiffers` to be injectable without
  `Injector`
@mary-poppins
Copy link

You can preview 7cf605d at https://pr23345-7cf605d.ngbuilds.io/.

@IgorMinar IgorMinar closed this in 6f213a7 Apr 13, 2018
IgorMinar pushed a commit that referenced this pull request Apr 13, 2018
- Remove default injection value from `inject` / `directiveInject` since
  it is not possible to set using annotations.
- Module `Injector` is stored on `LView` instead of `LInjector` data
  structure because it can change only at `LView` level. (More efficient)
- Add `ngInjectableDef` to `IterableDiffers` so that existing tests can
  pass as well as enable `IterableDiffers` to be injectable without
  `Injector`

PR Close #23345
IgorMinar pushed a commit that referenced this pull request Apr 13, 2018
@@ -376,8 +376,10 @@ export interface InjectDecorator {

export declare const enum InjectFlags {
Default = 0,
SkipSelf = 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change the order? It's a bitmask anyway o:

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants