Selection
​Selection is a simple construct that operates on a nested property in the non-primitive Units' value, i.e.: ListUnit, GenericUnit, and DictUnit.
Selection offers the ability to access and observe a nested property at a certain path in a Unit's value.

Make a Selection

1
// create a Unit
2
const preferencesUnit = new DictUnit({initialValue:
3
{color: 'blue', music: 'EDM', game: 'GTA5'}
4
})
5
​
6
// make a selection
7
// the 'select' method takes path as it's params
8
const musicPreference = preferencesUnit.select('music')
Copied!

Static Value Access

After creating a Selection object, we can access the value at the selected path anytime, without worrying if the value still exists or not.
1
// access the value at the selected path
2
console.log(musicPreference.value())
3
// logs 'EDM' immediately
Copied!

Reactive Value Access

To observe the value over time we can make an Observable at the selected path, and subscribe to it.
1
// create an Observable
2
const musicPreference$ = musicPreference.asObservable()
3
​
4
// subsribe for reactive access
5
musicPreference$.subscribe(value => console.log(value))
6
// logs 'EDM' immediately, and
7
// will log whenever the value at the selected path changes
Copied!
Last modified 1yr ago