ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.01k stars 1.97k forks source link

allow component inputs to be readonly within signal store #4323

Closed ducin closed 2 months ago

ducin commented 4 months ago

Which @ngrx/* package(s) are relevant/related to the feature request?

signals

Information

The issue is inspired by this tweet. In short:

It bothers me, that ngrx signal store is signal-based first and foremost, and that the only 2 solutions are:

(in other words we could say that ngrx signal store is incapable of composing component inputs as for now)

I don't think the above would be the most fundamental usecase, though I think combining component Inputs and signal store would be quite common in the long run.

THE IDEA

What I'm thinking of is a way for signal store to include a computed which would be accepted from outside, e.g. apart from withComputed, there'd be withInputs (just made up the name).

This doesn't seem trivial from the API surface area, i.e.:

I'd be happy to provide a PR but, definitely, design work needs to be done first.

EXAMPLE SCENARIO

Let's say our scenario is:


last note

if anything is anyhow unclear I'm happy to explain further.

Again, as the component input() signal already exists and can be used, it feels wrong to me to either use effect/signalWrites or rxMethod.

Describe any alternatives/workarounds you're currently using

No response

I would be willing to submit a PR to fix this issue

markostanimirovic commented 4 months ago

Some time ago, @alex-okrushko shared an example of how to implement the withInputBindings feature:

with-input-bindings

ducin commented 4 months ago

Okay, so that was through an external function. @markostanimirovic I think it'd make sense to include that at least in the docs - I could make a PR to signal store docs. WDYT?

gabrielguerrero commented 4 months ago

Hey @ducin @markostanimirovic, in my tweet, my case was calling the backend, so if I wanted to do that with this solution, it would require a withHook and onInit that listens to the stored signals to trigger the call; I still think for my case, the rxMethod that you pass the signal is easier. But I really like this solution for doing computed of signal inputs, I think might be a good candidate for a core feature even or something similar because it is a very common case as well. At least, like you mentioned, I think something in docs explaining how to pass input signals to the store will be great because everyone will face that case at some point. I could add that feature to @ngrx-traits/signals not sure if you have seen that package but Im trying to build a set of common util store features package, if @alex-okrushko doesnt mind :).

markostanimirovic commented 2 months ago

I'm going to convert this issue into a discussion because we don't plan to add it to the core package, at least for now.

@gabrielguerrero Sure, it would be great to have a feature like this in a community plugin!