🀜
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:

1
// create an Action, that accepts number as its value
2
const multiplyAction = new Action<number>();
Copied!
Action is an Observable but not a Subject.
1
multiplyAction instanceof Subject // false
2
multiplyAction instanceof Observable // true
Copied!

Accessing the value:

1
// access the value
2
console.log(multiplyAction.value()) // logs undefined
3
​
4
// dynamically access the value
5
multiplyAction.subscribe(value => {
6
console.log(value) // will log future values
7
// do something with the value here
8
});
Copied!

Dispatching a value:

1
// dispatch a value
2
multiplyAction.dispatch(3);
3
​
4
// replay the value
5
multiplyAction.replay(); // multiplyAction emits 3 again
Copied!
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.
1
// create a NumUnit to apply the multiplyAction on it
2
const numUnit = new NumUnit({initialValue: 10})
3
​
4
​
5
// create a stream using the earlier Action and above Unit
6
const subscription = multiplyAction.pipe(
7
tap(multiplier => {
8
// multiply the numUnit's value and update it
9
numUnit.dispatch(currValue => currValue * multiplier)
10
})
11
).subscribe();
12
​
13
​
14
// or simply use the built-in createStream method
15
const stream = multiplyAction.createStream(action$ => {
16
return action$.pipe(
17
tap(multiplier => {
18
// multiply the numUnit's value and update it
19
numUnit.dispatch(currValue => currValue * multiplier)
20
})
21
)
22
})
Copied!
The advantage of using the createStream method is that we get a Stream instance. So we can stop and re-start the multiplication stream.
1
// stop the multiplication stream
2
stream.unsubscribe();
3
​
4
// re-start the multiplication stream
5
stream.resubscribe();
Copied!

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 modified 1yr ago