GithubHelp home page GithubHelp logo

weingartner / reactivecompositecollections Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 2.0 1.63 MB

A .Net library for composing reactive collections.

License: MIT License

C# 99.42% PowerShell 0.58%
reactive reactiveui linq monad dotnet

reactivecompositecollections's Introduction

ReactiveCompositeCollections NuGet

A .Net library for composing reactive collections providing monadic types to support LINQ.

Motivation

We often have heirachical components that maintain collections of things. Imagine a collection of 3D objects that all produce lines. The 3D objects could be arranged in nested components. What we want is to be able to collect all the lines in the heirarchy as a flat reactive object

CompositeSourceList<ICompositeSourceList<Line>> nestedLines = new CompositeSourceList<ICompositeSourceList<Line>>();

Widget wa = new Widget();
Widget wb = new Widget();

nestedLines.Add(wa.Lines);
nestedLines.Add(wa.Lines);

ICompositeList<Line> allLines = 
               from lines in nestedLines
               from line in lines
               select line;
               
// Subscribe the the stream of flattened lines               
allLines.Items.Subscribe((ImmutableList<Line> lines)=>RenderLines(lines));

// or create an INPC object with a property Items
using(var s = allLines.Subscribe()){
   RenderLines(s.Items);
}

Using ICompositeCollection with WPF.

To use the ICompositeCollectin you need to convert it to a ReadOnlyObservableCollection via the following method.

    [Fact]
    public void ShouldWorkWithRangeOperators()
    {
        var source0 = new CompositeSourceList<int>();
        var source1 = new CompositeSourceList<int>();

        var target = source0.Concat(source1);

        using (var observableCollection = target.CreateObservableCollection(EqualityComparer<int>.Default))
        {

            observableCollection.Count.Should().Be(0);

            source0.AddRange(new [] {0,1,2});

            observableCollection.Should().Equal(0, 1,2);

            source0.Add(1);
            source1.Add(6);

            observableCollection.Should().Equal(0, 1, 2, 1, 6);

            source1.AddRange(new [] {3,4,5});

            observableCollection.Should().Equal(0, 1, 2, 1, 6, 3, 4, 5);

            source0.InsertRangeAt(1, new [] {6,7});

            observableCollection.Should().Equal(0, 6, 7, 1, 2, 1, 6, 3, 4, 5);

            source1.InsertRangeAt(1, new [] {6,7});
            observableCollection.Should().Equal(0, 6, 7, 1, 2, 1, 6, 6, 7, 3, 4, 5);

            source0.Source.Should().Equal(0, 6, 7, 1, 2,1);
            source0.Replace(1,99);
            observableCollection.Should().Equal(0, 6, 7, 99, 2, 1, 6, 6, 7, 3, 4, 5);


        }
    }

Note that the ReadOnlyObservableCollection is IDisposable so you should get rid of it when you don't need it anymore.

reactivecompositecollections's People

Contributors

bradphelan avatar florianauinger avatar johannesegger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bradphelan gkarka

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.