Exemplo n.º 1
0
        public void TestReactiveCollection()
        {
            var _nums = new ReactiveCollection <int>();
            IReadOnlyReactiveCollection <int> nums = _nums;

            nums.ObserveAdd().Subscribe(Add);
            nums.ObserveAdd().Subscribe(Add);
            _nums.Add(42);

            Assert.AreEqual(0, _index);
            Assert.AreEqual(42 * 2, _added);
        }
 /// <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)));
 }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
 public static IObservable <T> ToObservableAndAdded <T>(this IReadOnlyReactiveCollection <T> collection)
 {
     return(collection.ToObservable()
            .Merge(collection
                   .ObserveAdd()
                   .Select(added => added.Value)));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Connect this collection to another so that it reacts to additions and removals from the connected collection.
 /// </summary>
 /// <typeparam name="TTarget">The type of the connected collection.</typeparam>
 /// <param name="self">The collection to connect to and watch.</param>
 /// <param name="add">Called when an element is added to the connected collection.</param>
 /// <param name="remove">Called when an element is removed from the connected collection.</param>
 public static void ObserveChanges <TTarget>(
     this IReadOnlyReactiveCollection <TTarget> self,
     Action <CollectionAddEvent <TTarget> > add,
     Action <CollectionRemoveEvent <TTarget> > remove)
 {
     self.ObserveAdd().Subscribe(add);
     self.ObserveRemove().Subscribe(remove);
 }
Exemplo n.º 6
0
 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 <CollectionAddRemoveEvent <T> > ObserveCurrentAddRemove <T>(this IReadOnlyReactiveCollection <T> This) =>
 This.Select((x, i) => new CollectionAddRemoveEvent <T>(i, x, true))
 .ToObservable()
 .Concat(This
         .ObserveAdd()
         .Select(x => new CollectionAddRemoveEvent <T>(x.Index, x.Value, true))
         .Merge(This
                .ObserveRemove()
                .Select(x => new CollectionAddRemoveEvent <T>(x.Index, x.Value, false))));
 /// <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()
                ));
 }
Exemplo n.º 9
0
        public SortedReactiveCollection(IReadOnlyReactiveCollection <T> source, System.Func <T, T, int> comparer, IObservable <Unit> observableToReact)
        {
            this.comparer = comparer;
            source.ObserveAdd().Subscribe(@event => { AddSorted(@event.Value); });
            source.ObserveRemove().Subscribe(@event => { Remove(@event.Value); });

            foreach (var el in source)
            {
                AddSorted(el);
            }
        }
Exemplo n.º 10
0
        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();
        }
Exemplo n.º 11
0
        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);
                    }
                }
            });
        }
Exemplo n.º 12
0
        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));
        }
Exemplo n.º 13
0
 public ReactiveCollectionView(IReadOnlyReactiveCollection <T> collection, IFactory <TView> factory, Action <TView, T> initializer)
     : base(collection, factory, initializer)
 {
     collection.ObserveAdd().Subscribe(addEvent => Add(addEvent.Index, addEvent.Value)).AddTo(Disposable);
     collection.ObserveRemove().Subscribe(removeEvent => Remove(removeEvent.Index)).AddTo(Disposable);
 }