W
W
Waldemar10102020-02-26 16:57:50
JavaScript
Waldemar1010, 2020-02-26 16:57:50

How to implement PubSub Event channel in pure javascript?

Good afternoon, can someone tell me how to implement the PubSub Event channel pattern in ES6?
I saw the implementation of this pattern only in Java, but I don’t understand how to rewrite it in js without interfaces, since they are not in the language.
java example

public interface IEventChannel
{
    void Publish(string topic, string data);
    void Subscribe(string topic, ISubscriber subscriber);
}

public interface IPublisher
{
    void Publish(string data);
}

public interface ISubscriber
{
    void Notify(string data);
}

public class EventChannel: IEventChannel
{
    private Dictionary<string, List<ISubscriber>> _topics = 
        new Dictionary<string, List<ISubscriber>>();
    
    public void Publish(string topic, string data)
    {
        if(!_topics.ContainsKey(topic)) return;
        foreach(var subscriber in _topics[topic])
            subscriber.Notify(data);
    }

    public void Subscribe(string topic, ISubscriber subscriber)
    {
        if(_topics.ContainsKey(topic))
            _topics[topic].Add(subscriber);
        else
            _topics.Add(topic, new List<ISubscriber>() { subscriber });
    }
}

public class Publisher: IPublisher
{
    private string _topic;
    private IEventChannel _channel;
    public Publisher(string topic, IEventChannel channel)
    {
           _topic = topic;
           _channel = channel;
    }

    public void Publish(string data)
    {
           _channel.Publish(_topic, data);
    }
}

public class Subscriber: ISubscriber
{
    private string _name;
    public Subscriber(string name)
    {
           _name = name;
    }

    public void Notify(string data)
    {
           Console.Write($"Subscriber '{_name}' notify: '{data}'")
    }
}

static class Program
{
    public void Main(string[] args)
    {
           var channel = new EventChannel();
           var publisherA = new Publisher("#topic.a", channel);
           var publisherB = new Publisher("#topic.b", channel);
           var subscriberA = new Subscriber("Reader 1");
           var subscriberB = new Subscriber("Reader 2");

           channel.Subscribe("#topic.a", subscriberA);
           channel.Subscribe("#topic.a", subscriberB);
           channel.Subscribe("#topic.b", subscriberB);

           // Console write: Subscriber 'Reader 1' notify: 'Text1'
           // Console write: Subscriber 'Reader 2' notify: 'Text1'
           publisherA.Publish("Text1");

           // Console write: Subscriber 'Reader 2' notify: 'Text2'
           publisherB.Publish("Text2");
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shvets, 2020-02-26
@Waldemar1010

because I do not fully understand how it works)) can you tell me?

well here
https://jsfiddle.net/melchiorio/qr5gs0te/

S
sinneren, 2020-02-26
@sinneren

What about the first link on google?
https://medium.com/@thebabscraig/javascript-design...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question