/**
 * Created by goksuadanali on 3/19/16.
 */
'use strict';

angular.module('androidNewTestAppApp').factory('messenger', function() {

    var messageMap = {};

    var broadcast = function(message) {
        if (messageMap[message]) {
            messageMap[message].forEach(function(cb) {
                cb();
            });
        }
    };

    var register = function(message, callback) {
        if (messageMap[message]) {
            messageMap[message].push(callback);
        } else {
            messageMap[message] = [callback];
        }
    };

    var unregister = function(message) {
        if(messageMap[message]) {
            var deleteIndex = messageMap[message].indexOf(message);
            if (deleteIndex > -1) {
                messageMap[message].splice(deleteIndex, 1);
            }
        }

    };

    return {
        broadcastMessage: broadcast,
        registerMessage: register,
        unregisterMessage: unregister
    };
});