public WhereManyReactiveCollection(IReadOnlyReactiveCollection <T> source, Func <T, bool> filter, Func <T, IObservable <Unit> > filterChanged, IObservable <Unit> generalFilterChanged = null) { this.filter = filter; this.filterChanged = filterChanged; onAdd = source.ObserveAdd().Subscribe(@event => { OnAdd(@event.Value); }); onRemove = source.ObserveRemove().Subscribe(@event => { OnRemove(@event.Value); }); onReset = source.ObserveReset().Subscribe(_ => ClearItems()); onReset = source.ObserveReplace().Subscribe(@event => { OnRemove(@event.OldValue); OnAdd(@event.NewValue); }); if (generalFilterChanged != null) { generalFilterChangedBinding = generalFilterChanged.Subscribe(_ => { foreach (var element in source) { UpdateElement(element); } }); } foreach (var el in source) { OnAdd(el); } }
/// <summary> /// Iterates through the collection on subscription, and then pumps add and replace events. /// </summary> /// <param name="collection"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IObservable <T> Observe <T>(this IReadOnlyReactiveCollection <T> collection) { return(Observable.Merge( collection.ToObservable(), collection.ObserveAdd().Select(e => e.Value), collection.ObserveReplace().Select(e => e.NewValue))); }
public static IObservable <Unit> AnyCollectionChangeAsObservable <T>(this IReadOnlyReactiveCollection <T> reactiveCollection) { return(Observable.Merge( reactiveCollection.ObserveReset().AsUnitObservable(), reactiveCollection.ObserveAdd().AsUnitObservable(), reactiveCollection.ObserveMove().AsUnitObservable(), reactiveCollection.ObserveRemove().AsUnitObservable(), reactiveCollection.ObserveReplace().AsUnitObservable())); }
public static IObservable <T> ToObservableAddedAndReplaced <T>(this IReadOnlyReactiveCollection <T> collection) { return(collection.ToObservable() .Merge(collection .ObserveAdd() .Select(added => added.Value), collection.ObserveReplace() .Select(replaced => replaced.NewValue))); }
/// <summary> /// Iterates through the collection on subscription, and pumps add and replace events with the index and the new /// document always in the new value field. /// </summary> /// <param name="collection"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IObservable <CollectionReplaceEvent <T> > ObserveChanges <T>( this IReadOnlyReactiveCollection <T> collection) { return(Observable.Merge( Observable.Range(0, collection.Count) .Select(i => new CollectionReplaceEvent <T>(i, default(T), collection[i])), collection.ObserveAdd().Select(e => new CollectionReplaceEvent <T>(e.Index, default(T), e.Value)), collection.ObserveReplace() )); }
public CollectionBinder(IReadOnlyReactiveCollection <T> source, BaseListItem <T> prefab, Transform parent) { this.source = source; this.prefab = prefab; this.parent = parent; prefab.gameObject.SetActive(false); onAdd = source.ObserveAdd().Subscribe(@event => UpdateCollection()); onMove = source.ObserveMove().Subscribe(@event => UpdateCollection()); onReset = source.ObserveReset().Subscribe(@event => UpdateCollection()); onRemove = source.ObserveRemove().Subscribe(@event => UpdateCollection()); onReplace = source.ObserveReplace().Subscribe(@event => UpdateCollection()); UpdateCollection(); }
public WhereReactiveCollection(IReadOnlyReactiveCollection <T> source, Func <T, bool> filter, IObservable <Unit> filterChanged) { this.filter = filter; onAdd = source.ObserveAdd().Subscribe(@event => { if (filter(@event.Value)) { Add(@event.Value); } }); onRemove = source.ObserveRemove().Subscribe(@event => { Remove(@event.Value); }); onReset = source.ObserveReset().Subscribe(_ => ClearItems()); onReset = source.ObserveReplace().Subscribe(@event => { Remove(@event.OldValue); if (filter(@event.NewValue)) { Add(@event.NewValue); } }); filterChanged.Subscribe(_ => { foreach (var element in source) { if (filter(element)) { if (!Contains(element)) { Add(element); } } else { Remove(element); } } }); }
public static IDisposable BindChildPrefabsTo <T>(this GameObject input, IReadOnlyReactiveCollection <T> list, GameObject prefab, Func <GameObject, Transform, GameObject> instantiator, Action <T, GameObject> onChildCreated = null, Action <T, GameObject> onChildRemoving = null) { var disposable = new CompositeDisposable(); void onElementAdded(CollectionAddEvent <T> data) { var newChild = instantiator(prefab, input.transform); onChildCreated?.Invoke(data.Value, newChild); } void onElementUpdated(CollectionReplaceEvent <T> data) { var existingChild = input.transform.GetChild(data.Index); onChildCreated?.Invoke(data.NewValue, existingChild.gameObject); } void onElementRemoved(CollectionRemoveEvent <T> data) { var existingChild = input.transform.GetChild(data.Index); onChildRemoving?.Invoke(data.Value, existingChild.gameObject); GameObject.Destroy(existingChild); } list.ObserveAdd().Subscribe(onElementAdded).AddTo(disposable); list.ObserveReplace().Subscribe(onElementUpdated).AddTo(disposable); list.ObserveRemove().Subscribe(onElementRemoved).AddTo(disposable); input.transform.DeleteAllChildren(); foreach (var element in list) { var newChild = instantiator(prefab, input.transform); onChildCreated?.Invoke(element, newChild); } return(disposable.AddTo(input)); }