One more thing: when error is throw the entire action is stopped (can't emit the action again) unless using catchError
in the pipeline:
saveData() {
throw new Error('bad');
...
}
...
readonly action1$ = this.ui.action1$.pipe(
exhaustMap(() =>
this.api.saveData().pipe(
switchMap(() => ...
....
feat: PoC RxSignals
Often setters are enough. In such cases, we can use state.set({prop}).
If we want to apply behaviour we need to get a stream. This often leads to bloated code with Subjects.
We could use a Proxy object and a little TS to reduce the boilerplate here:
A config object could even get rid of the mapping in the template:
If we use the actions only in the class with
RxEffects
orRxState
we won't cause any memory leaks.A config object could even get rid of the mapping in the template:
As we have no subscriber after the component is destroyed we don't need to complete the Subjects.
However if we would pass it to another service which has a longer life time we could create a memory leak:
Here, as the global service lives longer than the component we have subscribers on the subject after the component is destroyed. For such situations, a hook is needed to get the component destroyed event.
Update: Due to typing issues, I had to refactor to an architecture where we have a wrapper scope for the action create function. Incredible BIG THANKS to @ddprrt for the support here.
The current solution provides a service and a factory function.
The service is hooked into Angular's life-cycles and so it cleans up on destruction. The factory function returns a pair of functions, create and destroy that can be used manually.
Service:
Service Usage:
Component Usage:
The factory function is maybe irrelevant for Angular but still worth looking at it:
Usage:
Update:
I removed the function because we can also do
new RxActionsFactory<any>().create()
. Additionally, docus are now present as well as tests.Missin features:
Missing tests:
The only flaw we could maybe see here is the wrapper e.g. new RxActionsFactory().create().prop
For now I have no clue how we could get rid of it and expose the setter and observables directly e.g. new RxActionsFactory().prop. One problem is typescript, the other Proxies, classes and typescript :). Maybe problems to solve in a later step...
Related issues: #423, #1013