JSFiddle - React, Tailwind, and code Playground

by nsshrinivasan

JavaScript

var publisher = {
    subscribers: {
        any: [] // event type: subscribers
    },
    subscribe: function (fn, type) {
        type = type || 'any';
        if (typeof this.subscribers[type] === "undefined") {
            this.subscribers[type] = [];
        }
        this.subscribers[type].push(fn);
    },
    unsubscribe: function (fn, type) {
        this.visitSubscribers('unsubscribe', fn, type);
    },
    publish: function (publication, type) {
        this.visitSubscribers('publish', publication, type);
    },
    visitSubscribers: function (action, arg, type) {
        var pubtype = type || 'any',
            subscribers = this.subscribers[pubtype],
            i,
            max = subscribers.length;

        for (i = 0; i < max; i += 1) {
            if (action === 'publish') {
                subscribers[i](arg);
            } else {
                if (subscribers[i] === arg) {
                    subscribers.splice(i, 1);
                }
            }
        }
    }
};

function makePublisher(o) {
    var i;
    for (i in publisher) {
        if (publisher.hasOwnProperty(i) && typeof publisher[i] === "function") {
            o[i] = publisher[i];
        }
    }
    o.subscribers = {any: []};
}

var community = {
    loginCallback: function () {
        this.publish("Standard login");
    }
};

makePublisher(community);

var echoLogin = {
    printCommunity: function(community){
        console.log('printing community');
    },
    printCommunityLogin: function(loginCallback){
        console.log('printing community login' + loginCallback);
    }
    
}

community.subscribe(echoLogin.printCommunity);
community.subscribe(echoLogin.printCommunityLogin, loginCallback);

community.loginCallback();