Replies: 2 comments
-
Angular standalone APIs have been added
Pre-release version of @mini-rx/signal-store on npmhttps://www.npmjs.com/package/@mini-rx/signal-store @mini-rx/signal-store is not ready yet for production. The API might change at anytime. Signal Store Pull RequestAngular Tetris with Signal StoreSignal Store Demohttps://signal-store-demo.mini-rx.io Demo code: https://github.com/spierala/mini-rx-store/tree/signal-store-demos/apps/signal-store-angular-demo |
Beta Was this translation helpful? Give feedback.
0 replies
-
Current status: Pull Request is open: #195
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Angular 16 has been released with support for Signals: https://blog.angular.io/angular-v16-is-here-4d7a28ec680d
In this RFC, we want to present some ideas around Signal-based state management with MiniRx.
If you are new to MiniRx, feel free to take a look at the docs: https://mini-rx.io/
MiniRx in short
MiniRx Store provides Reactive State Management, powered by RxJS. It is a highly flexible solution and scales with your state management needs:
MiniRx Signal Store
MiniRx Signal Store is a Signal-based variant of the original RxJS-based MiniRx Store.
Playground
There is a playground repo which helped to form the ideas for the MiniRx Signal Store: https://github.com/spierala/mini-rx-signal-store-playground
The playground contains a prototype of the Signal Store library and the MiniRx Demo app using the new Signal Store.
Source Code:
Goals
Key principles
Package name suggestion
@mini-rx/signal-store
Why Signals?
Signals in Angular have some advantages, compared to RxJS:
async
pipecomputed
instead of RxJScombineLatest
Why a dedicated Signal Store library?
The original MiniRx Store has an Angular Integration already...:
mini-rx-store-ng
.It is a thin wrapper around the framework-agnostic MiniRx Store providing
StoreModule
, injectableStore
,Actions
and more.So why not extend the existing Angular integration?
mini-rx-store-ng
with Signals would need to export dedicated classes forStore
,FeatureStore
,ComponentStore
. It could easily happen to import the wrong classes in your project (e.g. import accidentally frommini-rx-store
).effect
would be confusing together with Angulareffect
APIs for SignalsMain differences of the Signal Store
In general, the Signal Store will have a very similar API as the original MiniRx Store.
You can visit the docs to get an overview of the current MiniRx Store API: https://mini-rx.io/docs/intro#whats-included
Let's have a look at the main differences:
select
method returns a Signalstate
property returns a SignalcreateSelector
) use Angularcomputed
internallyrxEffect
naming to prevent confusion with Angular Signaleffect
APIupdate
instead ofsetState
(inspired by the Angular Signals API)update
, which is a very generic name and could be conflicting with other class methodsupdateState
would be very specific. It would be also more clear thatupdateState
is a little bit different from Signalupdate
.configureStore
will be removedStandalone APIs
The Signal Store should also come with modern Angular standalone APIs.
The future of the original MiniRx Store
The original MiniRx Store will still be maintained and stays a perfect state management library if you want to go all-in with RxJS (...and it is framework agnostic!).
The goal of MiniRx is to provide very similar features for both the Signal Store and the MiniRx Store.
Probably, Signal Store and MiniRx Store will depend on a shared library which will provide common code.
All libraries will reside in the same Nx mono-repo. E.g. the extensions, models and several util functions could be easily shared.
Signal Store in Action
Let's have a look at some code examples from the playground.
Store (Redux)
Actions, reducer, memoized selectors
MiniRx recommends to use ts-action for creating actions and reducers.
Memoized selectors
Memoized selectors in the Signal Store use Angular
computed
internally.The memoization is based purely on Signals!
This has some advantages:
createSelector
returns a function which takes a Signal and returns a Signal.See the
createSelector
implementation here: https://github.com/spierala/mini-rx-signal-store-playground/blob/main/projects/signal-store/src/lib/signal-selector.ts#L107-L120Register the Store
Yes, we need standalone APIs! Now! :)
Use the Store in the component
The Store can be injected in any component, service or directive.
Select state
We can select state with the
select
method.The
select
method accepts different types of selector functions:createSelector
returns a "Signal selector function"The Store also exposes a
state
property which holds the state as Signal.This allows to create your own computed Signals if you want.
Create and register effects
Use effects to trigger side effects like API calls and handle race conditions with RxJS flattening operators (e.g.
switchMap
).The effects API is pretty much unchanged. Just
createEffect
has been renamed tocreateRxEffect
.Register via the Effects Module
Feature Store
Feature Store offers a simplified API to update and read feature state directly. It uses Redux under the hood.
Update state
You can use
update
to update state.The
update
method works pretty much as the originalsetState
method (docs).Additionally, the
update
method also accepts a Signal (or Observable) to update state whenever the Signal/Observable has a new value.Select state
Signal State is available via the
state
property. Like that, you can directly create your own computed Signals.Alternatively, you can also use the
select
method to select state with state selector functions: e.g.count: Signal<number> = this.select(state => state.count)
FYI You can also use memoized selectors in the Feature Store. See an example here: https://github.com/spierala/mini-rx-signal-store-playground/blob/main/projects/signal-store-demo/src/app/modules/todos/state/todos-store.service.ts#L56-L59
Use the Feature Store in the component
Feature Store Effects
The
effect
method has been renamed torxEffect
.rxEffect
returns a function which accepts a Signal / Observable / Raw value to trigger side effects.Component Store
With Component Store you can manage local state which is independent of the global state object.
Component Store has the same API as Feature Store. So it will also have
update
,select
,rxEffect
,state
.Beta Was this translation helpful? Give feedback.
All reactions