πŸ“¦
Cluster
A Cluster is just a wrapper, a group, of two or more ActiveJS fundamental constructs, Units, Systems, Actions, or even Clusters.
It creates a master Observable of the combined value of its members by merging all the provided Observable constructs. Whenever any of these wrapped constructs emits a value, Cluster emits a new combined-value.
It also provides direct access to the wrapped constructs through items property. The ideal use case would be when you need access to the combined value of some ActiveJS constructs as an Observable.
Basically,
  • It creates a master Observable of the combined -value of provided members.
  • Provides access to its members through the Cluster.
  • Provides static access to the combined-value.
See API reference for more details.

Initializing a Cluster:

1
// a ListUnit that saves user's liked things
2
const userLikesUnit = new ListUnit();
3
​
4
// a DictUnit that saves user's preferences as key:value pairs
5
const userPreferencesUnit = new DictUnit({initialValue: {humor: 'dark'}});
6
​
7
// create a Cluster to group the above Units together
8
const userDataCluster = new Cluster({
9
userLikesUnit, // using shorthand notation
10
userPreferencesUnit
11
});
Copied!

Accessing the value:

1
// subscribe for the combined-value
2
userDataCluster.subscribe(value => console.log(value))
3
​
4
// or access it directly
5
console.log(userDataCluster.value())
6
​
7
// both of these will log the following
8
{
9
userLikesUnit: [],
10
userPreferencesUnit: {humor: 'dark'}
11
}
Copied!

Accessing the members through Cluster:

Cluster members can be accessed through the items property.
1
console.log(userDataCluster.items.userPreferencesUnit.value());
2
// logs {humor: 'dark'}
3
​
4
console.log(userDataCluster.items.userLikesUnit.value());
5
// logs []
Copied!

Value propagation:

Whenever a member of the Cluster emits a value, the Cluster will emit a new combined value.
1
// set a preference to the userPreferencesUnit
2
userPreferencesUnit.set('favouriteMovie', 'Matrix');
3
​
4
// the Cluster will emit the following value
5
{
6
userLikesUnit: [],
7
userPreferencesUnit: {humor: 'dark', favouriteMovie: 'Matrix'}
8
}
9
​
10
​
11
​
12
// add some liked things to the userLikesUnit
13
userLikesUnit.push({id: 42, label: 'puppy'}, {id: 66, label: 'kitten'});
14
​
15
// the Cluster will emit the following value
16
{
17
userLikesUnit: [{id: 42, label: 'puppy'}, {id: 66, label: 'kitten'}],
18
userPreferencesUnit: {humor: 'dark', favouriteMovie: 'Matrix'}
19
}
20
​
Copied!
Similarly, we can group any number of Units, Systems, Actions or even Clusters together.

Notes

  • Try to refrain from using the static value accessor value() method when the Cluster contains immutable Units because when calling the value() method of a Cluster, it internally calls its members' value() method, and if members are Immutable Units, those Units would create a clone on every call. It's not a bad thing if used intentionally, but it can add extra overhead if used too often.

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