コード例 #1
0
        public static CompositeDisposable TrackChangesOfType <T, T2>(this IReactiveList <T2> list, Action <T2> add,
                                                                     Action <T2> remove,
                                                                     Action <IEnumerable <T2> > reset, Func <T, bool> filter = null)
            where T2 : class
            where T : class, T2
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            var disposables = new CompositeDisposable();

            if (add != null)
            {
                var o = list.ItemsAdded.OfType <T>();
                if (filter != null)
                {
                    o = o.Where(filter);
                }
                disposables.Add(o.Subscribe(add));
            }
            if (remove != null)
            {
                var o = list.ItemsRemoved.OfType <T>();
                if (filter != null)
                {
                    o = o.Where(filter);
                }
                disposables.Add(o.Subscribe(remove));
            }
            if (reset != null)
            {
                disposables.Add(list.ShouldReset
                                .Subscribe(
                                    x =>
                                    reset(filter == null
                                ? list.OfType <T>().ToArray()
                                : list.OfType <T>().Where(filter).ToArray())));
            }
            return(disposables);
        }