Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

購読解除は奥が深い

$
0
0

ReactivePropertyを使いたい人のための、ReactiveExtensions入門(その3)yone64.wordpress.com

よねさんのこの記事を見てもやもやしてたけど、妙案が思いつかないで今にいたる。ということで、こういう拡張メソッドでもReactivePropertyにいれようか悩む。どうだろうか

publicclass MainWindowViewModel
{
    public ReactiveProperty<PersonViewModel> Person { get; private set; }

    public MainWindowViewModel()
    {
        this.Person = new ReactiveProperty<PersonViewModel>();

        this.Person
            .CleanSubscribe(x => x?.Name.Subscribe(Console.WriteLine));
    }
}

publicclass PersonViewModel
{
    public ReactiveProperty<string> Name { get; private set; }

    public PersonViewModel()
    {
        this.Name = new ReactiveProperty<string>();
    }
}

publicstaticclass Ex
{
    publicstatic IDisposable CleanSubscribe<T>(this IObservable<T> self, Func<T, IDisposable> subscribe)
    {
        return self
            .Select(subscribe)
            .Scan(Tuple.Create(default(IDisposable), default(IDisposable)), (a, b) => Tuple.Create(a.Item2, b))
            .Subscribe(x => x.Item1?.Dispose());
    }
}

メソッド名もいいのが思いつかない。


Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>