diff --git a/src/client/spark/core/Utility/ObjectSnapshot.cs b/src/client/spark/core/Utility/ObjectSnapshot.cs index 2f0a2b39f..4e8418417 100644 --- a/src/client/spark/core/Utility/ObjectSnapshot.cs +++ b/src/client/spark/core/Utility/ObjectSnapshot.cs @@ -1,5 +1,7 @@ using bts.api.core; using mbts; +using mbts.ui.shell; +using mbts.utils; using System; using System.Collections.Generic; using System.ComponentModel; @@ -8,11 +10,25 @@ using System.Reactive.Linq; using System.Reflection; namespace bts.utils { - static class ObjectSnapshot { - public static IObservable> property_table(T instance, params string[] prop_names) where T : INotifyPropertyChanged => - property_table(instance, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + interface IPropertyChangedDetector where T : IRaiseNotifyPropertyChanged { + IDisposable detect_changes(T target); + } - public static IObservable> property_table(T instance, params PropertyInfo[] props) where T : INotifyPropertyChanged => + static class ObjectSnapshot { + public static IEnumerable> property_delegates_of_type() where TSource : class where TValue : class { + var tval = typeof(TValue); + return typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(p => tval.IsAssignableFrom(p.PropertyType)) + .Select(_prop_getter); + } + + public static IObservable> property_table(T instance, params string[] prop_names) where T : INotifyPropertyChanged => + _property_table(instance, Array.ConvertAll(prop_names, PropertyInfoStore.type.GetProperty)); + + public static IObservable> property_table(INotifyPropertyChanged instance, params string[] prop_names) => + _property_table(instance, Array.ConvertAll(prop_names, instance.GetType().GetProperty)); + + static IObservable> _property_table(INotifyPropertyChanged instance, params PropertyInfo[] props) => Observable.Create>(o => { var cache = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance))); var table = TableUpdate<(string name, object current, object original), string>.mk_empty(t => t.name).with_added(cache.Values.Select(pn => (pn.prop.Name, pn.current, pn.original))); @@ -29,11 +45,20 @@ namespace bts.utils { } }); + public static IPropertyChangedDetector build_detector(params string[] props) where T : IRaiseNotifyPropertyChanged => + new DiscretePropertyChangedDetector(props); + + public static IPropertyChangedDetector build_detector_except(params string[] except_props) where T : IRaiseNotifyPropertyChanged { + // convert to a hashset for quicker lookups + var exclude = new HashSet(except_props); + return new DiscretePropertyChangedDetector(PropertyInfoStore.all_props.Where(p => !exclude.Contains(p.Name)).Select(p => p.Name).ToArray()); + } + public static IObservable dirty_monitor(T instance, params string[] prop_names) where T : INotifyPropertyChanged => - dirty_monitor(instance, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + dirty_monitor(instance, Array.ConvertAll(prop_names, PropertyInfoStore.type.GetProperty)); public static IObservable dirty_monitor(T instance, out IDisposable snapshot, params string[] prop_names) where T : INotifyPropertyChanged => - dirty_monitor(instance, out snapshot, Array.ConvertAll(prop_names, Impl.type.GetProperty)); + dirty_monitor(instance, out snapshot, Array.ConvertAll(prop_names, PropertyInfoStore.type.GetProperty)); public static IObservable dirty_monitor(T instance, out IDisposable snapshot, params PropertyInfo[] props) where T : INotifyPropertyChanged { var prop_map = props.ToDictionary(p => p.Name, p => new PropNode(p, p.GetValue(instance))); @@ -68,7 +93,7 @@ namespace bts.utils { }); public static IDisposable create(T instance) => - create(instance, Impl.mutable_props); + create(instance, PropertyInfoStore.mutable_props); public static IDisposable create(T instance, PropertyInfo[] props) { var snapshot = Array.ConvertAll(props, p => p.GetValue(instance)); @@ -78,13 +103,63 @@ namespace bts.utils { }); } - static class Impl { + static Func _prop_getter(PropertyInfo prop) => + (Func)prop.GetGetMethod().CreateDelegate(typeof(Func)); + + static class PropertyInfoStore { public static readonly Type type = typeof(T); - public static readonly PropertyInfo[] mutable_props = - type.GetProperties(BindingFlags.Public | BindingFlags.Instance) - .Where(p => p.GetSetMethod() != null) - .ToArray(); + public static readonly PropertyInfo[] + all_props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance), + mutable_props; + + static PropertyInfoStore() => + mutable_props = all_props.Where(p => p.GetGetMethod() != null).ToArray(); + } + + // DelegateStore maintains a cache of property getter/setter delegates, + // which perform much better than the corresponding PropertyInfo.GetValue/SetValue. + static class DelegateStore { + public static readonly Type type = typeof(T); + + static readonly IReadOnlyDictionary get, Action set)> _property_delegates; + + public static Func getter(string prop_name) => + _property_delegates.TryGetValue(prop_name, out var dels) ? dels.get : null; + + public static Action setter(string prop_name) => + _property_delegates.TryGetValue(prop_name, out var dels) ? dels.set : null; + + static DelegateStore() { + var prop_dels = new Dictionary get, Action set)>(); + foreach (var p in PropertyInfoStore.all_props) { + Func wrapped_getter = null; + Action wrapped_setter = null; + var getter = p.GetGetMethod(); + var setter = p.GetSetMethod(); + if (getter == null && setter == null) + continue; + var del_types = (typeof(Func<,>).MakeGenericType(typeof(T), p.PropertyType), typeof(Action<,>).MakeGenericType(typeof(T), p.PropertyType)); + var twrapper = typeof(Wrapper<>).MakeGenericType(typeof(T), p.PropertyType); + if (getter != null) { + wrapped_getter = (Func) + twrapper.GetMethod(nameof(Wrapper.getter), BindingFlags.Static | BindingFlags.Public) + .Invoke(null, new[] { getter.CreateDelegate(del_types.Item1) }); + } + if (setter != null) { + wrapped_setter = (Action) + twrapper.GetMethod(nameof(Wrapper.setter), BindingFlags.Static | BindingFlags.Public) + .Invoke(null, new[] { setter.CreateDelegate(del_types.Item2) }); + } + prop_dels[p.Name] = (wrapped_getter, wrapped_setter); + } + _property_delegates = prop_dels; + } + + static class Wrapper { + public static Func getter(Func getter) => src => getter(src); + public static Action setter(Action setter) => (src, val) => setter(src, (TValue)val); + } } class PropNode { @@ -100,5 +175,30 @@ namespace bts.utils { public object original { get; set; } public object current { get; set; } } + + class DiscretePropertyChangedDetector : IPropertyChangedDetector where T : IRaiseNotifyPropertyChanged { + static readonly Converter get)> _realize_prop = + (prop_name) => (prop_name, DelegateStore.getter(prop_name)); + + readonly (string name, Func get)[] _target_props; + + public DiscretePropertyChangedDetector() + : this(Array.ConvertAll(PropertyInfoStore.all_props, p => p.Name)) { } + + public DiscretePropertyChangedDetector(params string[] props) => + _target_props = Array.ConvertAll(props, _realize_prop); + + public IDisposable detect_changes(T target) { + var get_target_val = new Converter<(string name, Func get), object>(tup => tup.get(target)); + var orig_state = Array.ConvertAll(_target_props, get_target_val); + return Disposable.Create(() => { + var curr_state = Array.ConvertAll(_target_props, get_target_val); + for (int i = 0; i < curr_state.Length; i++) { + if (!Equals(curr_state[i], orig_state[i])) + target.raise_notify_property_changed(_target_props[i].name); + } + }); + } + } } } \ No newline at end of file