dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Messenger extensions to bridge functionality to IObservable<T>

Open Sergio0694 opened this issue 3 years ago • 0 comments

Overview

This issue tracks adding extension points for Reactive extensions to the messenger types. This can be done without needing any external dependencies, as IObservable<T> and IObserver<T> are built-in down to .NET Standard 2.0 as well. We really just need easy to use and simple APIs to get an IObservable<T> for a messenger, then people can build on top of this on their own using Reactive extensions. We mostly just need that initial support to create an observable to then do stuff with.

API breakdown

namespace CommunityToolkit.Mvvm.Messaging;

public static class IMessengerExtensions
{
    public static IObservable<TMessage> CreateObservable<TMessage>(this IMessenger messenger)
        where TMessage : class;

    public static IObservable<TMessage> CreateObservable<TMessage, TToken>(this IMessenger messenger, TToken token)
        where TMessage : class
        where TToken : IEquatable<TToken>;
}

Usage example

var token =
    messenger
    .CreateObservable<MyMessage>()
    .Where(...)
    .Whatever(...)
    .ObserveOnFooBar(...)
    .Subscribe(_ => Console.WriteLine("Hello world!"));

Breaking change?

No

Alternatives

People would basically have to implement this on their own every time. Doable, but annoying.

Sergio0694 avatar Aug 27 '22 23:08 Sergio0694