feat(signal-slice): pass initial state streams to source functions #486
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implementation of proposal discussed here: #485
This PR is non-breaking, it adds onto the
state
object passed to sources but it does not change existing functionalityThis adds the ability to use streams of the signal slices own state within sources (e.g. if there is an initial state property of
myVal
then amyVal$
stream will be available on the state object passed to a source), which allows asignalSlice
to react to its own state changing without needing to use external subjects, or without using anactionSource
which will lead to a more imperative style of code.Here is an example of plain RxJS handling a pagination scenario in a way that is cleanly declarative (but perhaps not the most practical):
My goal was to allow this style of code to be followed reasonably faithfully using only features of
signalSlice
. With the change in this PR, we are able to write code like this:Note specifically that no external subjects are required here. Our action sources update the state values, and our pre-defined sources can react to those changes via the
pageNumber$
anditemFilter$
streams that have been added.The discussion linked also discussed potentially creating automatic setter action sources for the initial state, so that you could update them without needing to manually define an
actionSource
as I have done above. This PR does not include that change, if we end up deciding on doing that I will open a separate PR for that.