using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using DynamicData; namespace Wabbajack { public static class DynamicDataExt { public static IObservable CollectionCount(this IObservable> source) { int count = 0; return source .Select(changeSet => { count += changeSet.Adds; count -= changeSet.Removes; return count; }) .StartWith(0); } public static IObservable CollectionCount(this IObservable> source) { int count = 0; return source .Select(changeSet => { count += changeSet.Adds; count -= changeSet.Removes; return count; }) .StartWith(0); } public static IObservable> TransformAndCache( this IObservable> obs, Func onAdded, Action, TCache> onUpdated) { var cache = new ChangeAwareCache(); return obs .Select(changeSet => { foreach (var change in changeSet) { switch (change.Reason) { case ChangeReason.Add: case ChangeReason.Update: case ChangeReason.Refresh: var lookup = cache.Lookup(change.Key); TCache val; if (lookup.HasValue) { val = lookup.Value; } else { val = onAdded(change.Key, change.Current); cache.Add(val, change.Key); } onUpdated(change, val); break; case ChangeReason.Remove: cache.Remove(change.Key); break; case ChangeReason.Moved: break; default: throw new NotImplementedException(); } } return cache.CaptureChanges(); }) .Where(cs => cs.Count > 0); } } }