public static IReactiveSet <T> Except <T>(this IReactiveSet <T> first, IReactiveSet <T> second)
        {
            if (first == null)
            {
                throw new ArgumentNullException(nameof(first));
            }
            if (second == null)
            {
                throw new ArgumentNullException(nameof(second));
            }

            var obs1 = first.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => !second.Contains(x))));
            var obs2 = second.AsObservable()
                       .Select(change => {
                if (change.ChangeReason == ReactiveSetChangeReason.Add)
                {
                    return(new ReactiveSetChange <T>(ReactiveSetChangeReason.Remove, change.Items));
                }
                else
                {
                    return(new ReactiveSetChange <T>(ReactiveSetChangeReason.Add, change.Items.Where(first.Contains)));
                }
            });

            return(obs1.Merge(obs2).ToReactiveSet(x => first.Contains(x) && !second.Contains(x)));
        }
        public static ICollectedReactiveSet <T> Collect <T>(this IReactiveSet <T> set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            return(new ReactiveSet <T>(set.AsObservable()));
        }
        public static IReactiveSet <T> ObserveOnDispatcher <T>(this IReactiveSet <T> set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            return(set.AsObservable().ObserveOnDispatcher().ToSet(set.Contains));
        }
        public static IObservable <T> ElementsAdded <T>(this IReactiveSet <T> set)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            return(set
                   .AsObservable()
                   .Where(x => x.ChangeReason == ReactiveSetChangeReason.Add)
                   .SelectMany(x => x.Items));
        }
Esempio n. 5
0
        public static ICollectedReactiveSet <TResult> Select <T, TResult>(this IReactiveSet <T> set, Func <T, TResult> filter)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            return(new SetSelectImplementation <T, TResult>(set.AsObservable(), filter));
        }
Esempio n. 6
0
        public static IObservable <TResult> Aggregate <T, TResult>(this IReactiveSet <T> set, TResult seed, Func <TResult, T, TResult> addFunc, Func <TResult, T, TResult> removeFunc)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            if (addFunc == null)
            {
                throw new ArgumentNullException(nameof(addFunc));
            }
            if (removeFunc == null)
            {
                throw new ArgumentNullException(nameof(removeFunc));
            }

            return(Observable.Create <TResult>(observer => {
                var current = seed;
                observer.OnNext(current);

                IDisposable sub = null;
                sub = set
                      .AsObservable()
                      .Subscribe(
                    x => {
                    try {
                        if (x.ChangeReason == ReactiveSetChangeReason.Add)
                        {
                            current = x.Items.Aggregate(current, addFunc);
                        }
                        else
                        {
                            current = x.Items.Aggregate(current, removeFunc);
                        }

                        observer.OnNext(current);
                    }
                    catch (Exception ex) {
                        observer.OnError(ex);
                        sub?.Dispose();
                        return;
                    }
                },
                    observer.OnError,
                    observer.OnCompleted
                    );

                return sub;
            })
                   .DistinctUntilChanged());
        }
Esempio n. 7
0
        public static IReactiveSet <T> Where <T>(this IReactiveSet <T> set, Func <T, bool> filter)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            return(set
                   .AsObservable()
                   .Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(filter)))
                   .ToReactiveSet(x => filter(x) && set.Contains(x)));
        }
        public static IReactiveSet <T> Intersection <T>(this IReactiveSet <T> first, IReactiveSet <T> second)
        {
            if (first == null)
            {
                throw new ArgumentNullException(nameof(first));
            }
            if (second == null)
            {
                throw new ArgumentNullException(nameof(second));
            }

            var obs1 = first.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => second.Contains(x))));
            var obs2 = second.AsObservable().Select(change => new ReactiveSetChange <T>(change.ChangeReason, change.Items.Where(x => first.Contains(x))));

            return(obs1.Merge(obs2).ToReactiveSet(x => first.Contains(x) && second.Contains(x)));
        }
Esempio n. 9
0
        public static IOrderedReactiveSet <T> OrderBy <T, TCompare>(this IReactiveSet <T> set, Func <T, TCompare> compareFunc)
        {
            int comparison(T first, T second)
            {
                int comp = Comparer <TCompare> .Default.Compare(compareFunc(first), compareFunc(second));

                if (comp != 0)
                {
                    return(comp);
                }

                comp = Comparer <T> .Default.Compare(first, second);

                if (comp != 0)
                {
                    return(comp);
                }

                return(1);
            }

            return(new OrderedReactiveSet <T>(set.AsObservable(), Comparer <T> .Create(comparison)));
        }
Esempio n. 10
0
 public static T[] ToArray <T>(this IReactiveSet <T> set)
 {
     return(set.AsObservable().FirstAsync().Select(x => x.Items.ToArray()).Wait());
 }
Esempio n. 11
0
        public static ICollectedReactiveSet <T> Buffer <T>(this IReactiveSet <T> set, TimeSpan time)
        {
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }

            var obs = Observable.Create <ReactiveSetChange <T> >(observer => {
                var isFirst = true;

                return(set.AsObservable()
                       .Where(x => {
                    if (isFirst)
                    {
                        isFirst = false;
                        observer.OnNext(x);
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                })
                       .Buffer(time)
                       .Subscribe(
                           x => {
                    var added = new HashSet <T>();
                    var removed = new HashSet <T>();

                    foreach (var change in x)
                    {
                        if (change.ChangeReason == ReactiveSetChangeReason.Add)
                        {
                            foreach (var item in change.Items)
                            {
                                if (removed.Contains(item))
                                {
                                    removed.Remove(item);
                                }
                                else
                                {
                                    added.Add(item);
                                }
                            }
                        }
                        else
                        {
                            foreach (var item in change.Items)
                            {
                                if (added.Contains(item))
                                {
                                    added.Remove(item);
                                }
                                else
                                {
                                    removed.Add(item);
                                }
                            }
                        }
                    }

                    if (added.Any())
                    {
                        observer.OnNext(new ReactiveSetChange <T>(ReactiveSetChangeReason.Add, added));
                    }

                    if (removed.Any())
                    {
                        observer.OnNext(new ReactiveSetChange <T>(ReactiveSetChangeReason.Remove, removed));
                    }
                },
                           observer.OnError,
                           observer.OnCompleted
                           ));
            });

            return(obs.ToReactiveSet());
        }
Esempio n. 12
0
 public static T FirstOrDefault <T>(this IReactiveSet <T> set, Func <T, bool> predicate)
 {
     return(set.AsObservable().Select(x => x.Items.FirstOrDefault(predicate)).FirstAsync().Wait());
 }
Esempio n. 13
0
 public static T FirstOrDefault <T>(this IReactiveSet <T> set)
 {
     return(set.AsObservable().Select(x => x.Items.FirstOrDefault()).FirstAsync().Wait());
 }