Skip to content

Commit 6218571

Browse files
arturovtthePunderWoman
authored andcommitted
refactor(core): drop injection context assertion in production (angular#61564)
In other parts of the code, calls to the `assertInInjectionContext` function are guarded with `ngDevMode`. This change aligns these parts of the code with other implementations that drop such assertions in production. PR Close angular#61564
1 parent 46af023 commit 6218571

7 files changed

Lines changed: 25 additions & 7 deletions

File tree

packages/common/http/src/resource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ function makeHttpResourceFn<TRaw>(responseType: ResponseType) {
230230
request: RawRequestType,
231231
options?: HttpResourceOptions<TResult, TRaw>,
232232
): HttpResourceRef<TResult> {
233-
options?.injector || assertInInjectionContext(httpResource);
233+
if (ngDevMode && !options?.injector) {
234+
assertInInjectionContext(httpResource);
235+
}
234236
const injector = options?.injector ?? inject(Injector);
235237
return new HttpResourceImpl(
236238
injector,

packages/core/rxjs-interop/src/rx_resource.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export function rxResource<T, R>(
4444
*/
4545
export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T | undefined>;
4646
export function rxResource<T, R>(opts: RxResourceOptions<T, R>): ResourceRef<T | undefined> {
47-
opts?.injector || assertInInjectionContext(rxResource);
47+
if (ngDevMode && !opts?.injector) {
48+
assertInInjectionContext(rxResource);
49+
}
4850
return resource<T, R>({
4951
...opts,
5052
loader: undefined,

packages/core/rxjs-interop/src/to_signal.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export function toSignal<T, U = undefined>(
131131
);
132132

133133
const requiresCleanup = !options?.manualCleanup;
134-
requiresCleanup && !options?.injector && assertInInjectionContext(toSignal);
134+
135+
if (ngDevMode && requiresCleanup && !options?.injector) {
136+
assertInInjectionContext(toSignal);
137+
}
138+
135139
const cleanupRef = requiresCleanup
136140
? (options?.injector?.get(DestroyRef) ?? inject(DestroyRef))
137141
: null;

packages/core/src/render3/after_render/hooks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ export function afterEveryRender(
216216
'callback inside the component constructor`.',
217217
);
218218

219-
!options?.injector && assertInInjectionContext(afterEveryRender);
219+
if (ngDevMode && !options?.injector) {
220+
assertInInjectionContext(afterEveryRender);
221+
}
222+
220223
const injector = options?.injector ?? inject(Injector);
221224

222225
if (typeof ngServerMode !== 'undefined' && ngServerMode) {

packages/core/src/render3/reactivity/after_render_effect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ export function afterRenderEffect<E = never, W = never, M = never>(
366366
'effect inside the component constructor`.',
367367
);
368368

369-
!options?.injector && assertInInjectionContext(afterRenderEffect);
369+
if (ngDevMode && !options?.injector) {
370+
assertInInjectionContext(afterRenderEffect);
371+
}
370372

371373
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
372374
return NOOP_AFTER_RENDER_REF;

packages/core/src/render3/reactivity/effect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ export function effect(
140140
'effect inside the component constructor.',
141141
);
142142

143-
!options?.injector && assertInInjectionContext(effect);
143+
if (ngDevMode && !options?.injector) {
144+
assertInInjectionContext(effect);
145+
}
144146

145147
if (ngDevMode && options?.allowSignalWrites !== undefined) {
146148
console.warn(

packages/core/src/resource/resource.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export function resource<T, R>(
5858
*/
5959
export function resource<T, R>(options: ResourceOptions<T, R>): ResourceRef<T | undefined>;
6060
export function resource<T, R>(options: ResourceOptions<T, R>): ResourceRef<T | undefined> {
61-
options?.injector || assertInInjectionContext(resource);
61+
if (ngDevMode && !options?.injector) {
62+
assertInInjectionContext(resource);
63+
}
64+
6265
const oldNameForParams = (
6366
options as ResourceOptions<T, R> & {request: ResourceOptions<T, R>['params']}
6467
).request;

0 commit comments

Comments
 (0)