🤜Action
Actions are meant to represent unique reactive events. Action is an RxJS Subject like construct. Actions don't replay the last value when subscribed to, unlike Units, Systems and Clusters. Actions help trigger customized operations in a reactive fashion.
You can call an Action a simplified form of a Unit as they share the same base API, although configured in a slightly different way.
It has a
dispatch
method, just like a Unit.It extends the RxJS Observable class, hence it's an Observable.
By default, It doesn't replay when subscribed to, unlike a Unit.
It doesn't require an initial value, yet provides direct access to the last emitted value, just like a Unit.
It can only cache one value at a time, unlike a Unit.
Unlike Units, it doesn't perform any checks on
dispatch
.
See API reference for more details.
Initializing an Action:
Action is an Observable but not a Subject.
Accessing the value:
Dispatching a value:
The above example shows the basic usage of an Action, it's similar to an RxJS Subject with slight differences.
Creating a stream:
The real-life usage would most probably be to create streams using the Action.
The advantage of using the createStream
method is that we get a Stream instance.
So we can stop and re-start the multiplication stream.
Action vs Units vs BehaviorSubject
Action | Unit | BehaviorSubject | |
Replays value on subscription (by default) | ❌ | ✅ | ✅ |
Can be configured to replay value on subscription | ✅ | ✅ | ❌ |
Allows to listen to only future values (useful when configured to replay on subscription) | ✅ | ✅ | ❌ |
Accepts an initial-value | ✅ | ✅ | ✅ |
Can cache more than one value | ❌ | ✅ | ❌ |
Validates dispatched values | ❌ | ✅ | ❌ |
Can re-emit/replay last value manually | ✅ | ✅ | ❌ |
Can be part of a Cluster | ✅ | ✅ | ❌ |
Configuration Options
The configuration options can be passed at the time of instantiation. All the configuration options are optional. Some options can also be set globally. See Configuration for more details.
Last updated