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
Conversation
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
packages/core/src/di/injector.ts
Outdated
/** | ||
* Injects a token from the currently active injector. | ||
* | ||
* This function must be used in the context of an `ngInjectableDef` factory function, and will |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
a25dc34
to
de32278
Compare
You can preview de32278 at https://pr22389-de32278.ngbuilds.io/. |
I spoke with Chuck and we don't think that reflection capabilities would be
needed if we used deps. Can you please sync up with him and come to an
agreement?
My preference is to keep this api private (for now) unless there is a
strong reason to make it public.
…On Thu, Feb 22, 2018, 4:34 PM Mary Poppins ***@***.***> wrote:
You can preview de32278
<de32278>
at https://pr22389-de32278.ngbuilds.io/.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#22389 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AANM6Ln2M2RttuDqam_rPXVaMtCTrkM3ks5tXgeCgaJpZM4SQKns>
.
|
de32278
to
50a9b9c
Compare
You can preview 50a9b9c at https://pr22389-50a9b9c.ngbuilds.io/. |
c0b1722
to
abc6001
Compare
All comments addressed, needs re-review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
You can preview abc6001 at https://pr22389-abc6001.ngbuilds.io/. |
6aac2aa
to
4d3931e
Compare
You can preview 6aac2aa at https://pr22389-6aac2aa.ngbuilds.io/. |
You can preview 4d3931e at https://pr22389-4d3931e.ngbuilds.io/. |
4d3931e
to
ef54180
Compare
You can preview ef54180 at https://pr22389-ef54180.ngbuilds.io/. |
After discussing with @mhevery, we're agreed that |
ef54180
to
c86fb35
Compare
You can preview c86fb35 at https://pr22389-c86fb35.ngbuilds.io/. |
packages/core/src/di/injector.ts
Outdated
* 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 @@ | |||
/** |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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