-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
RFC: method
- rxMethod
without RxJS
#4581
Comments
First, great improvement ! |
Exactly, I don't have the best feeling about the name either. |
I may have missed something so what would be the difference/benefit over this? const UserStore = signalStore(
withState({selectedId: 0}),
withMethods(store => ({
setSelectedId: (selectedId: number) => patchState(store, {selectedId})
}))
)
class UserComponent {
userId = input.required<number>();
userStore = inject(UserStore);
constructor() {
this.userStore.setSelectedId(this.userId);
}
} EDIT: If I'm correct, the difference would be with |
Yeah, I might be wrong but your example would not be triggered again when the userId would change, right ? |
Okay I think I understand now! Is the goal triggering the method without calling the method? |
@Tiime-LcsGa, your example would throw a compilation error because you are passing a It is not a SignalStore feature. So not in the same group as |
I missed the fact that const UserStore = signalStore(
withState({selectedId: 0}),
withMethods(store => ({
setSelectedId: (selectedId: number) => patchState(store, {selectedId})
}))
)
class UserComponent {
userId = input.required<number>();
userStore = inject(UserStore);
constructor() {
effect(() => this.userStore.setSelectedId(this.userId()));
}
} What are the benefits of this utility function? |
Oh, I'd say that this will become the way to go.
https://blog.angular.dev/latest-updates-to-effect-in-angular-f2d2648defcd ...and since you asked what
|
@rainerhahnekamp thanks for all of your explanations! I get way better why you proposed this now! I think what "bothered" me here is while => Currently => With This is what I found hacky... |
signalMethod is a function factory to process side effects on Signals or static values. It is similar to rxMethod but does not support RxJS. signalMethod follows Angular’s pattern of RxJS-less utilities for Signals, like resource and rxResource. `signalMethod` expects a type and processor function: ```typescript const doubleLogger = signalMethod<number>(value => console.log(value * 2)) const value = signal(1); doubleLogger(value); // tracks value and executes initially and on every change ``` It also supports static values, e.g., `doubleLogger(1)`.
`signalMethod` is a factory function to process side effects on Signals or static values. It is similar to `rxMethod` but does not support RxJS. `signalMethod` follows Angular's pattern of RxJS-less utilities for Signals, like `resource` and `rxResource`. `signalMethod` expects a type and processor function: ```typescript const doubleLogger = signalMethod<number>(value => console.log(value * 2)) const value = signal(1); doubleLogger(value); // tracks value and executes initially and on every change ``` It also supports static values, e.g., `doubleLogger(1)`.
`signalMethod` is a factory function to process side effects on Signals or static values. It is similar to `rxMethod` but does not support RxJS. `signalMethod` follows Angular's pattern of RxJS-less utilities for Signals, like `resource` and `rxResource`. `signalMethod` expects a type and processor function: ```typescript const doubleLogger = signalMethod<number>(value => console.log(value * 2)) const value = signal(1); doubleLogger(value); // tracks value and executes initially and on every change ``` It also supports static values, e.g., `doubleLogger(1)`.
@LcsGa you can now check out the PR and especially the tests where you can see its usage. You'll see that there is no |
Okay I get it now! I don't know why but as I read the first message you wrote there, I thought it was only usable in |
I just read the implementation of |
Well, looks like I didn't describe it enough in the RFC:
With |
Okay I get everything now! 👌🏼 |
Which @ngrx/* package(s) are relevant/related to the feature request?
signals
Information
Problem
With Angular introducing
resource
andrxResource
for handling asynchronous tasks, SignalStore could benefit from a similar feature—a standalonerxMethod
that operates without a dependency to RxJS:method
.Proposed Solution
The
method
function, defined asfunction method<Type>(consumer: (value: T | Signal<T>) => void)
, would provide a straightforward way to process Signal changes independently of RxJS.Differences to native
effect()
It might be tempting to see
method
as a simpleeffect
:However,
method
offers three distinctive advantages overeffect
:Flexible Parameter: The parameter can also be a simple number, not just a Signal.
No Injection Context Required: Unlike an
effect
, which requires an injection context or anInjector
,method
can be used directly in an event listener without passing anInjector
.Explicit Tracking: Only the Signal of the parameter is tracked, while other Signals within the consumer function (like
anotherSignal()
in the example) are not tracked. For example:Discussion
However, the name
method
may not clearly convey its purpose, so a more descriptive alternative is worth considering. Possible options includesignalMethod
.Describe any alternatives/workarounds you're currently using
Devs could go with a normal
effect()
. This would limit the use case ofmethod
to only Signals as inputs.I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: