1 /**
  2  * Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global core, runtime*/
 26 
 27 /**
 28  * @constructor
 29  * @implements {core.EventSource}
 30  * @param {!Array.<!string>=} eventIds
 31  */
 32 core.EventNotifier = function EventNotifier(eventIds) {
 33     "use strict";
 34 
 35     var /**@type{!Object.<!string,!Array.<!Function>>}*/
 36         eventListener = {};
 37 
 38     /**
 39      * @param {!string} eventId
 40      * @param {*} args
 41      * @return {undefined}
 42      */
 43     this.emit = function (eventId, args) {
 44         var i, subscribers;
 45 
 46         runtime.assert(eventListener.hasOwnProperty(eventId),
 47             "unknown event fired \"" + eventId + "\"");
 48         subscribers = eventListener[eventId];
 49         // runtime.log("firing event \"" + eventId + "\" to " + subscribers.length + " subscribers.");
 50         for (i = 0; i < subscribers.length; i += 1) {
 51             subscribers[i](args);
 52         }
 53     };
 54 
 55     /**
 56      * @param {!string} eventId
 57      * @param {!Function} cb
 58      * @return {undefined}
 59      */
 60     this.subscribe = function (eventId, cb) {
 61         runtime.assert(eventListener.hasOwnProperty(eventId),
 62             "tried to subscribe to unknown event \"" + eventId + "\"");
 63         eventListener[eventId].push(cb);
 64     };
 65 
 66     /**
 67      * @param {!string} eventId
 68      * @param {!Function} cb
 69      * @return {undefined}
 70      */
 71     this.unsubscribe = function (eventId, cb) {
 72         var cbIndex;
 73         runtime.assert(eventListener.hasOwnProperty(eventId),
 74             "tried to unsubscribe from unknown event \"" + eventId + "\"");
 75 
 76         cbIndex = eventListener[eventId].indexOf(cb);
 77         runtime.assert(cbIndex !== -1, "tried to unsubscribe unknown callback from event \"" + eventId + "\"");
 78         if (cbIndex !== -1) {
 79             eventListener[eventId].splice(cbIndex, 1);
 80         }
 81     };
 82 
 83     /**
 84      * Register an event
 85      * @param {!string} eventId
 86      * @return {undefined}
 87      */
 88     function register(eventId) {
 89         runtime.assert(!eventListener.hasOwnProperty(eventId), "Duplicated event ids: \"" + eventId + "\" registered more than once.");
 90         eventListener[eventId] = [];
 91     }
 92     this.register = register;
 93 
 94     /**
 95      * @return {undefined}
 96      */
 97     function init() {
 98         if (eventIds) {
 99             eventIds.forEach(register);
100         }
101     }
102 
103     init();
104 };
105