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

fix(core): export inject() from @angular/core #22389

Closed
wants to merge 1 commit into from

Conversation

alxhub
Copy link
Member

@alxhub alxhub commented Feb 22, 2018

inject() supports the ngInjectableDef-based configuration of the injector
(otherwise known as tree-shakeable services). It was missing from the
public API of @angular/core, this PR adds it.

The test added here is correct in theory, but may pass accidentally due
to the decorator side-effect replacing the inject() call at runtime. An
upcoming compiler PR will strip reified decorators from output entirely.

Fixes #22388.

CC @jelbourn

@mary-poppins
Copy link

You can preview a25dc34 at https://pr22389-a25dc34.ngbuilds.io/.

@@ -447,6 +447,9 @@ export interface HostDecorator {
/** @stable */
export declare const HostListener: HostListenerDecorator;

/** @experimental */
export declare function inject<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: undefined, flags?: InjectFlags): T;
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this be exported as ɵinject instead? I don't think that this needs to be a public api since only generated code needs access to it as far as I can tell.

Can you please clarify?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is public API. InjectionToken depends on it being so. A user would write:

const TOKEN = new InjectionToken<Service>('tree-shakeable token!', {
  scope: Module,
  factory: () => new Service(inject(Dep))
});

inject() is how they interact with the injection system.

Copy link
Contributor

Choose a reason for hiding this comment

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

so we don't support providing dependencies via deps just like we do any any other provider?

Copy link
Member Author

@alxhub alxhub Feb 23, 2018

Choose a reason for hiding this comment

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

Not with InjectionToken, because this isn't actually a provider definition. We can't support a provider here because that would require reflecting dependencies, which would bring in ReflectionCapabilities even in AOT mode.

/**
* Injects a token from the currently active injector.
*
* This function must be used in the context of an `ngInjectableDef` factory function, and will
Copy link
Contributor

Choose a reason for hiding this comment

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

ngInjectableDef is not a public api so this comment won't make sense to a application developer reading this comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

True, I'll reword it for now.

@@ -447,6 +447,9 @@ export interface HostDecorator {
/** @stable */
export declare const HostListener: HostListenerDecorator;

/** @experimental */
export declare function inject<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: undefined, flags?: InjectFlags): T;
Copy link
Contributor

Choose a reason for hiding this comment

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

so we don't support providing dependencies via deps just like we do any any other provider?

@mary-poppins
Copy link

You can preview de32278 at https://pr22389-de32278.ngbuilds.io/.

@IgorMinar
Copy link
Contributor

IgorMinar commented Feb 23, 2018 via email

@mary-poppins
Copy link

You can preview 50a9b9c at https://pr22389-50a9b9c.ngbuilds.io/.

@alxhub alxhub force-pushed the export-inject branch 2 times, most recently from c0b1722 to abc6001 Compare February 23, 2018 17:22
@alxhub alxhub added action: merge The PR is ready for merge by the caretaker target: major This PR is targeted for the next major release labels Feb 23, 2018
@ngbot
Copy link

ngbot bot commented Feb 23, 2018

I see that you just added the PR action: merge label, but the following checks are still failing:
    failure missing required status "continuous-integration/travis-ci/pr"
    failure missing required status "code-review/pullapprove"
    pending status "google3" is pending
    pending status "ci/circleci: build" is pending
    pending status "ci/circleci: lint" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@alxhub alxhub dismissed IgorMinar’s stale review February 23, 2018 17:22

All comments addressed, needs re-review.

IgorMinar
IgorMinar previously approved these changes Feb 23, 2018
Copy link
Contributor

@IgorMinar IgorMinar left a comment

Choose a reason for hiding this comment

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

thanks

@mary-poppins
Copy link

You can preview abc6001 at https://pr22389-abc6001.ngbuilds.io/.

@alxhub alxhub force-pushed the export-inject branch 2 times, most recently from 6aac2aa to 4d3931e Compare February 26, 2018 16:54
@mary-poppins
Copy link

You can preview 6aac2aa at https://pr22389-6aac2aa.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 4d3931e at https://pr22389-4d3931e.ngbuilds.io/.

@mary-poppins
Copy link

You can preview ef54180 at https://pr22389-ef54180.ngbuilds.io/.

@alxhub
Copy link
Member Author

alxhub commented Feb 26, 2018

After discussing with @mhevery, we're agreed that inject() should be exported publicly. It's intended to replace deps: [...] as the way of specify dependencies to factory functions, and has the major advantage that its call sites are monomorphic.

@mary-poppins
Copy link

You can preview c86fb35 at https://pr22389-c86fb35.ngbuilds.io/.

@alexeagle alexeagle added the area: core Issues related to the framework runtime label Feb 26, 2018
* Injects a token from the currently active injector.
*
* This function must be used in the context of a factory function such as one defined for an
* `InjectionToken`, and will throw an error if not called from such a context.
Copy link
Contributor

Choose a reason for hiding this comment

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

most people will not use InjectionToken to interact with this. Maybe expand this to say "one defined via @Injectable factory or InjectionToken" and link both the the respective api pages.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for documentation with example

Copy link
Member Author

Choose a reason for hiding this comment

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

@Injectable does not yet have factory (you could use inject in useFactory but that's not a pattern that we've decided to teach yet).

I've added an example that shows how to use inject within an InjectionToken function.

I'll communicate this public API change to the docs team working on tree-shakeable providers as well, and they can document inject in their new material.

@@ -0,0 +1,41 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

since you are proposing to make inject a public api, you should also make use of it in this test. currently all the testing is done indirectly by relying on the generating code containing "inject" call.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added it to the token test.

This was referenced Mar 15, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
inject() supports the ngInjectableDef-based configuration of the injector
(otherwise known as tree-shakeable services). It was missing from the
exported API of @angular/core, this PR adds it.

The test added here is correct in theory, but may pass accidentally due
to the decorator side-effect replacing the inject() call at runtime. An
upcoming compiler PR will strip reified decorators from the output
entirely.

Fixes angular#22388

PR Close angular#22389
@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 area: core Issues related to the framework runtime 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.

inject() not exported from @angular/core
6 participants