Skip to content

[Feature]: Add TransformVirtual Extensions for IObservableList and IObservableCache聽#1174

Description

@ktxiaok

Describe the functionality desired 馃悶

New extension methods TransformVirtual<TSource, TResult>(Func<TSource, TResult> transformFactory) for IObservableList<TSource> and IObservableCache<TSource, TKey> that create lightweight views of type IObservableList<TResult>/IObservableCache<TResult, TKey> without intermediate storage collection.

There's an implementation for IObservableList I created and used in my project:

public static class CollectionExtensions
{
    extension<T>(IReadOnlyList<T> source)
    {
        public IReadOnlyList<TResult> TransformVirtual<TResult>(Func<T, TResult> transformFactory)
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(transformFactory);

            return new ReadOnlyListTransformVirtual<T, TResult>(source, transformFactory);
        }
    }

    private sealed class ReadOnlyListTransformVirtual<TSource, TResult> : IReadOnlyList<TResult>
    {
        private readonly IReadOnlyList<TSource> _source;
        private readonly Func<TSource, TResult> _transformFactory;

        public ReadOnlyListTransformVirtual(IReadOnlyList<TSource> source, Func<TSource, TResult> transformFactory)
        {
            _source = source;
            _transformFactory = transformFactory;
        }

        public TResult this[int index] => _transformFactory(_source[index]);

        public int Count => _source.Count;

        public IEnumerator<TResult> GetEnumerator() => _source.Select(x => _transformFactory(x)).GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
}

public static class ObservableListExtensions
{
    extension<T>(IObservableList<T> source) where T : notnull
    {
        public IObservableList<TResult> TransformVirtual<TResult>(Func<T, TResult> transformFactory) where TResult : notnull
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(transformFactory);

            return new ObservableListTransformVirtual<T, TResult>(source, transformFactory);
        }
    }

    private sealed class ObservableListTransformVirtual<TSource, TResult> : IObservableList<TResult>
        where TSource : notnull
        where TResult : notnull
    {
        private readonly IObservableList<TSource> _source;
        private readonly Func<TSource, TResult> _transformFactory;

        public ObservableListTransformVirtual(IObservableList<TSource> source, Func<TSource, TResult> transformFactory)
        {
            _source = source;
            _transformFactory = transformFactory;
        }

        public int Count => _source.Count;

        public IObservable<int> CountChanged => _source.CountChanged;

        public IReadOnlyList<TResult> Items => _source.Items.TransformVirtual(_transformFactory);

        public void Dispose()
        {
        }

        public IObservable<IChangeSet<TResult>> Connect(Func<TResult, bool>? predicate = null)
        {
            var ret = _source.Connect().Transform(_transformFactory);
            if (predicate is not null)
            {
                ret = ret.Filter(predicate);
            }
            return ret;
        }

        public IObservable<IChangeSet<TResult>> Preview(Func<TResult, bool>? predicate = null)
        {
            var ret = _source.Preview().Transform(_transformFactory);
            if (predicate is not null)
            {
                ret = ret.Filter(predicate);
            }
            return ret;
        }
    }
}

Sincerely thank all of you for creating DynamicData!

The steps the functionality will provide

From the end-user perspective:

  1. Call the new extension method TransformVirtual on any IObservableList/IObservableCache to create another stateless IObservableList/IObservableCache as a projected view without intermediate storage collection in a fluent way.
  2. Transformation execution is on-demand and should be pure (no side effects). The transformFactory will run if items are accessed via indexer or enumeration. There is no precomputation or materialization, unless a subscription of a downstream observer via Connect or Preview occurs.
  3. Disposal of the returned IObservableList/IObservableCache is not required and does nothing because it's just a stateless wrapper.
  4. Fully compatible with consumer scenarios that require direct index access, count property and enumeration support.

Considerations

Currently, in order to transform the elements of an IObservableList/IObservableCache, the Connect method needs to be called first and then use the Transform operator of observable change set. After that, the type of the current operating object becomes observable change set and the direct accessors of collection count and items are lost. In some cases, SourceList/SourceCaches are internally used and some transformed IObservableList/IObservableCaches should be exposed as public API. To revert to IObservableList/IObservableCache type, the method AsObservableList/AsObservableCache can be used, which creates intermediate storage collection (e.g. SourceList is created for IObservableList). If we just want to do a simple transformation, such as a type cast (even the implicit cast because covariance is not supported by DynamicData), this method is inefficient and unnecessary. To do this in a better way, we can just create a stateless wrapper of IObservableList/IObservableCache without inherent subscription and collection allocation.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions