Skip to content

A simple, lightweight event messenger that uses a ConditionalWeakTable internally.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt
Notifications You must be signed in to change notification settings

immense/SimpleMessenger

Repository files navigation

SimpleMessenger

A simple, lightweight event messenger that uses a ConditionalWeakTable internally.

NuGet: https://www.nuget.org/packages/Immense.SimpleMessenger

Usage

// Program.cs
using Immense.SimpleMessenger;

// ...

services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);
// MyService.cs

public class MyService : IMyService, IDisposable
{
    private readonly IMessenger _messenger;
    private readonly IDisposable _messengerRegistration;

    public MyService(IMessenger messenger)
    {
        _messenger = messenger;

        // Registers a handler under the default channel.
        // The handler will be removed automatically if
        // this MyService instance is garbage-collected.
        // Or it can be removed manually by disposing of the return
        // value of the Register method.
        _messengerRegistration = messenger.Register<SomeMessageType>(this, MyHandler);
    }

    public void Dispose()
    {
        _messengerRegistration.Dispose();
    }

    private async Task MyHandler(object subscriber, SomeMessageType message)
    {
        // Handle the message.
    }
}

About

A simple, lightweight event messenger that uses a ConditionalWeakTable internally.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages