I have a concept Roomin my program. Each room has a quantity User, and each user has Observableone that indicates what level of silence he wants in the room. When the user selects a new value, I just push it on the Observable, calling OnNextin the internal instance BehaviorSubjectinside the class User.
The main logic of the program is that you need to be able to say "what level of silence" in the room. For example, if at least one user in a room needs silence, then the whole room needs silence.
This is a simplification of my classes:
First, I use an enumeration to represent possible sound levels. Now there are only two values:
enum SoundLevelRequirement
{
None,
Silence
}
Observable, , .
class User
{
private readonly BehaviorSubject<SoundLevelRequirement> soundLevel;
public User(string name)
{
Name = name;
soundLevel = new BehaviorSubject<SoundLevelRequirement>(SoundLevelRequirement.None);
}
public string Name { get; }
public IObservable<SoundLevelRequirement> SoundLevelChanged => soundLevel.DistinctUntilChanged();
public void SetSoundLevel(SoundLevelRequirement level)
{
soundLevel.OnNext(level);
}
}
, :
class Room
{
private readonly BehaviorSubject<SoundLevelRequirement> soundLevel;
private readonly ObservableCollection<User> users;
public Room(string name)
{
Name = name;
Users = new ObservableCollection<User>();
soundLevel = new BehaviorSubject<SoundLevelRequirement>(SoundLevelRequirement.None);
}
public ObservableCollection<User> Users { get; set; }
public string Name { get; }
public IObservable<SoundLevelRequirement> SoundLevel => soundLevel.DistinctUntilChanged();
}
Observable Rx Observable - . , ( User ), . , , CombineLatest, , :
Users.Select(u => u.SoundLevelChanged).CombineLatest().Select(latest => latest.Max());
, , - , , " ", . , , .
, . , 5 "" - , Silence, reset , , None.
, ObservableCollection , -, , , , , , .