fix(signals): deprecate calling rxMethod/signalMethod outside injection context#5079
Conversation
The rxMethod and signalMethod memory leak warning now only fires when the source injector is a root or platform injector. Previously, the warning fired any time a reactive input was passed outside an injection context, even when the source injector (e.g. a component injector) would properly handle cleanup. Uses duck-typed access to Angular's internal R3Injector.scopes property to detect root/platform injectors. Closes ngrx#4991 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
✅ Deploy Preview for ngrx-io ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Hey @david-shortman 👋 Thanks for raising these PRs. I'm glad to see your PRs again! I don't think we should show the warning only for root/platform injectors. In the following situation: @Injectable()
export class ParentService {
readonly parentMethod = rxMethod(...);
}
@Component({
template: `
@if (show()) { <child /> }
`,
providers: [ParentService],
})
export class ParentComponent {
readonly show = signal(false);
}
@Component({ /* ... */ })
export class ChildComponent implements OnInit {
readonly parentService = inject(ParentService);
ngOnInit() {
this.parentService.parentMethod(interval(2_000));
}
}The memory leak will happen when Another problem with this implementation is using the private |
|
I'll see if I can detect this situation. Additionally, I can add testing that would catch a breaking change from Angular. Glad to be looking at NgRx again. I was curious to look at older issues I had been involved with but with the fresh lens of researching and validating ideas with Claude Code. |
|
👋 Hey all. If we’re looking to make changes here, I’d actually propose going into the opposite direction: show a deprecation message instead of a warning. In that sense, rxMethod would align much better with the other Angular signal-based functions like effect, linkedSignal, and the resource APIs. |
You're referring to requiring supplying the injection context explicitly Although I like the stylishness of the context being implicitly loaded, and the slightly less boilerplate that comes with it, I would support either direction. So you're saying that, like the other Angular signal functions, unless you call in an injection context like the constructor, then you must explicitly supply the intended injection context? |
Exactly. My take here is that |
|
Hey @david-shortman, Do you want to update this PR with the suggested deprecation message? |
✅ Deploy Preview for ngrx-site-v21 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
910f727 to
08be3d6
Compare
…outside injection context Instead of warning only when the source injector is root (which uses private Angular APIs and misses cases like child→parent leaks), warn for all reactive calls outside an injection context without an explicit injector. This aligns with Angular's pattern for effect(), linkedSignal(), and resource(), and avoids reliance on internal R3Injector.scopes API. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
08be3d6 to
c0db4c3
Compare
Yes; knowing that's the direction that multiple maintainers are good with, I've updated the PR to deprecate calling outside injection context. |
markostanimirovic
left a comment
There was a problem hiding this comment.
thanks @david-shortman! 👑
rainerhahnekamp
left a comment
There was a problem hiding this comment.
Well done Mr. @david-shortman 👍
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When
rxMethodorsignalMethodis called with a reactive input outside an injection context without an explicit injector, a warning about a "potential memory leak" is displayed. This warning had some false positives in common usage patterns.Closes #4991
What is the new behavior?
The warning is replaced with a deprecation message. The goal is to eventually align with Angular's reactive primitives by requiring an injector when reactive methods are called outside an injection context.
Changes:
rxMethodandsignalMethod: warning updated to deprecation noticeinformtowarnwith deprecation languageDoes this PR introduce a breaking change?
This is a non-breaking change to a dev-mode warning message.