using System.Collections.ObjectModel; using System.Collections.Specialized; using ObservableComputations; namespace DeclarativeProperty; public static class DeclarativePropertyExtensions { public static IDeclarativeProperty Debounce(this IDeclarativeProperty from, TimeSpan interval, bool resetTimer = false) => new DebounceProperty(from, _ => interval) {ResetTimer = resetTimer}; public static IDeclarativeProperty Debounce(this IDeclarativeProperty from, Func interval, bool resetTimer = false) => new DebounceProperty(from, interval) {ResetTimer = resetTimer}; public static IDeclarativeProperty Throttle(this IDeclarativeProperty from, TimeSpan interval) => new ThrottleProperty(from, () => interval); public static IDeclarativeProperty Throttle(this IDeclarativeProperty from, Func interval) => new ThrottleProperty(from, interval); public static IDeclarativeProperty DistinctUntilChanged(this IDeclarativeProperty from) => new DistinctUntilChangedProperty(from); public static IDeclarativeProperty Map(this IDeclarativeProperty from, Func> mapper) => new MapProperty(mapper, from); public static IDeclarativeProperty Map(this IDeclarativeProperty from, Func mapper) => new MapProperty((next, _) => Task.FromResult(mapper(next)), from); public static async Task> MapAsync(this IDeclarativeProperty from, Func> mapper) => await MapProperty.CreateAsync(mapper, from); public static async Task> MapAsync(this IDeclarativeProperty from, Func mapper) => await MapProperty.CreateAsync((next, _) => Task.FromResult(mapper(next)), from); public static IDisposable Subscribe(this IDeclarativeProperty property, Action onChange) => property.Subscribe((value, token) => { onChange(value, token); return Task.CompletedTask; }); public static IDisposable Subscribe(this IDeclarativeProperty property, Action onChange) => property.Subscribe((value, _) => { onChange(value); return Task.CompletedTask; }); public static IDeclarativeProperty Extract( this IDeclarativeProperty> from, Func?, T?> extractor ) => new ExtractorProperty(from, extractor); public static IDeclarativeProperty Extract( this IDeclarativeProperty> from, Func?, T?> extractor ) => new ExtractorProperty(from, extractor); public static IDeclarativeProperty Extract( this IDeclarativeProperty> from, Func?, T?> extractor ) => new ExtractorProperty(from, extractor); public static IDeclarativeProperty Watch( this IDeclarativeProperty collection) where TCollection : IList, INotifyCollectionChanged => new CollectionRepeaterProperty(collection); public static IDeclarativeProperty Watch( this TCollection collection) where TCollection : IList, INotifyCollectionChanged => new CollectionRepeaterProperty(collection); public static IDeclarativeProperty?> Watch( this ObservableCollection collection) => new CollectionRepeaterProperty?, TItem>(collection); public static IDeclarativeProperty?> Watch( this ReadOnlyObservableCollection collection) => new CollectionRepeaterProperty?, TItem>(collection); public static IDeclarativeProperty CombineLatest( this IDeclarativeProperty prop1, IDeclarativeProperty prop2, Func> func, Action? setValueHook = null) => new CombineLatestProperty(prop1, prop2, func, setValueHook); public static IDeclarativeProperty Switch(this IDeclarativeProperty> from) => new SwitchProperty(from); }