Last active
March 5, 2025 10:53
-
-
Save Argon42/66aa8707c13c2c33246aacbb506eb0da to your computer and use it in GitHub Desktop.
UniRx aggregate RxProperty from RxCollection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class UniRxExtensions | |
{ | |
public static IObservable<TResult> AggregateRxProperties<TItem, TPropertyValue, TResult>( | |
this IReadOnlyReactiveCollection<TItem> collection, | |
Func<TItem, IReadOnlyReactiveProperty<TPropertyValue>> propertySelector, | |
Func<IEnumerable<TPropertyValue>, TResult> resultSelector) | |
{ | |
var disposable = new CompositeDisposable(); | |
var dictionary = new Dictionary<TItem, IDisposable>(); | |
var subject = new Subject<TResult>().AddTo(disposable); | |
subject.Subscribe(_ => { }, () => | |
{ | |
ResetWithoutNotification(); | |
disposable.Dispose(); | |
}); | |
collection.ObserveAdd().Subscribe(@event => AddElement(@event.Value)).AddTo(disposable); | |
collection.ObserveRemove().Subscribe(@event => RemoveElement(@event.Value)).AddTo(disposable); | |
collection.ObserveReset().Subscribe(_ => Reset()).AddTo(disposable); | |
collection.ObserveReplace().Subscribe(Replace).AddTo(disposable); | |
subject.OnNext(GetResult()); | |
return subject; | |
void AddElement(TItem item) | |
{ | |
IDisposable dispose = propertySelector(item).Subscribe(_ => subject.OnNext(GetResult())); | |
dictionary[item] = dispose; | |
} | |
TResult GetResult() => resultSelector(collection.Select(item => propertySelector(item).Value)); | |
void RemoveElement(TItem item) | |
{ | |
dictionary[item].Dispose(); | |
dictionary.Remove(item); | |
subject.OnNext(GetResult()); | |
} | |
void Replace(CollectionReplaceEvent<TItem> @event) | |
{ | |
dictionary[@event.OldValue].Dispose(); | |
dictionary.Remove(@event.OldValue); | |
IDisposable dispose = propertySelector(@event.NewValue).Subscribe(_ => subject.OnNext(GetResult())); | |
dictionary[@event.NewValue] = dispose; | |
} | |
void Reset() | |
{ | |
ResetWithoutNotification(); | |
subject.OnNext(GetResult()); | |
} | |
void ResetWithoutNotification() | |
{ | |
dictionary.Values.ForEach(disposable1 => disposable1.Dispose()); | |
dictionary.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of how I use this