Skip to content

MessageChannel - a pub-sub message-passing pattern #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

pdeschain
Copy link
Contributor

@pdeschain pdeschain commented Feb 3, 2022

Description (*)

MessasgeChannel is a pub-sub mechanism that is heavily inspired by https://github.com/Cysharp/MessagePipe (but is much simpler in implementation an can do drastically less than the big library).

Also adding DIScope extensions - this messaging system is particularly useful with DI and the extension provided allows to easily bind all the necessary interfaces to the DI Scope for easy retrieval of either publisher or subscriber interfaces to the message channel.

When we use DI to pass the Pub or Sub references to classes - we facilitate less strong coupling in our code.

Usage would look like this:

struct Msg
{
//some message
}

//...

scope.BindMessageChannel<Msg>();

//and after the scope is resolved we can fetch IPublisher or ISubbscriber like this:

    public class PubUserExample
    {
        private IPublisher<Msg> _msgPublisher;
        
        [Inject]
        public PubUserExample(IPublisher<Msg> msgPublisher)
        {
            _msgPublisher = msgPublisher;
        }
    }

    public class SubUserExample : IDisposable
    {
        private IDisposable _subscription;
        
        [Inject]
        public SubUserExample(ISubscriber<Msg> msgSubscriber)
        {
            _subscription = msgSubscriber.Subscribe(Callback);
        }

        private void Callback(Msg msg)
        {
            
        }
        
        public void Dispose()
        {
            _subscription?.Dispose();
        }
    }
 

Related Pull Requests

Issue Number(s) (*)

Fixes issue(s):

Manual testing scenarios

  1. ...
  2. ...

Questions or comments

  • Buffered pub-sub channel could be added - it would remember the last message passed and would replay it to any new subscriber.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@pdeschain pdeschain added the 1-Needs Review PR needs attention from the assignee and reviewers label Feb 3, 2022
@pdeschain pdeschain force-pushed the pdeschain/message-channel-pub-sub branch from 4f4a159 to ada19b1 Compare February 14, 2022 15:10
@pdeschain pdeschain force-pushed the pdeschain/dependency-injection-container branch from e39a997 to 9520990 Compare February 14, 2022 16:03
also adding DIScope extensions - this messaging system is particularly useful with DI and the extension provided allows to easily bind all the necessary interfaces to the DI Scope for easy retrieval of either publisher or subscriber interfaces to the message channel
@pdeschain pdeschain force-pushed the pdeschain/message-channel-pub-sub branch from d76106f to b489113 Compare February 14, 2022 16:04
@pdeschain pdeschain mentioned this pull request Feb 14, 2022
4 tasks
@pdeschain
Copy link
Contributor Author

closing this PR - the content in here was merged with Lobby PR

@pdeschain pdeschain closed this Feb 24, 2022
@pdeschain pdeschain deleted the pdeschain/message-channel-pub-sub branch March 23, 2022 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1-Needs Review PR needs attention from the assignee and reviewers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants