Giter Site home page Giter Site logo

lanicon / easy.messagehub Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nimaara/easy.messagehub

0.0 1.0 0.0 1.77 MB

No need for .NET Events! A thread-safe, high performance & easy to use cross platform implementation of the Event Aggregator Pattern.

License: MIT License

C# 99.37% Batchfile 0.63%

easy.messagehub's Introduction

NuGet Build status

Easy.MessageHub

An implementation of the Event Aggregator Pattern.

Supports .Net Core (.Net 4.5 & netstandard1.0) running on:

  • .Net Core
  • .Net Framework 4.5 and above
  • Mono & Xamarin
  • UWP
  • Windows 8.0
  • Windows Phone 8.1
  • Windows Phone Seilverlight 8.0
If you enjoy what I build then please Buy Me A Coffee :-)

Usage example:

Start by creating an instance of the hub:

IMessageHub hub = new MessageHub();

You can now use the hub to subscribe to any publication of a given type:

Guid token = hub.Subscribe<Person>(p => Console.WriteLine($"Id is: {p.Id}"));
// or    
Action<string> action = message => Console.WriteLine($"Message is: {message}");
Guid anotherToken = hub.Subscribe(action);

You can then use the token to do:

hub.IsSubscribed(token); // returns true
hub.Unsubscribe(token);
hub.IsSubscribed(token); // returns false

Or you can clear all subscriptions by:

hub.ClearSubscriptions();

Publication is as easy as:

hub.Publish(new Person { Id = "Foo" });
hub.Publish("An important message");

Error handling:

The hub catches any exception thrown at the time of publication and exposes them via:

hub.RegisterGlobalErrorHandler((token, e) => Console.WriteLine($"Error Publishing, Token: {token} | Exception: {e}"));

Global handler:

The hub allows the registration of a single handler which will receive every message published by the hub. This can be useful in scenarios where every message published should be logged or audited.

hub.RegisterGlobalHandler((type, eventObject) => Console.WriteLine($"Type: {type} - Event: {eventObject}"));

Event throttling:

The hub allows each subscriber to throttle the rate at which it receives the events:

hub.Subscribe<string>(msg => Console.WriteLine($"Message is: {msg}"), TimeSpan.FromSeconds(1));

In the above example, if the subscriber receives more than one message within 1 second of another, it drops them.

Inheritance support:

The hub supports inheritance by allowing to subscribe to a base class or an interface and receiving all the publications of types that inherit or implement the subscribed type. For example, given:

public class Order {}
public class NewOrder : Order{}
public class BigOrder : Order{}

A subscriber registering against Ordrer will also receive events of type NewOrder and BigOrder.

More details HERE

easy.messagehub's People

Contributors

nimaara avatar

Watchers

 avatar

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.