Changed var Foo = function() to function Foo(), added missing JSDoc
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLab / WCF / CallbackList.js
1 "use strict";
2
3 /**
4 * Simple API to store and invoke multiple callbacks per identifier.
5 *
6 * @author Alexander Ebert
7 * @copyright 2001-2015 WoltLab GmbH
8 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 * @module WoltLab/WCF/CallbackList
10 */
11 define(['Dictionary'], function(Dictionary) {
12 /**
13 * @constructor
14 */
15 function CallbackList() {
16 this._dictionary = new Dictionary();
17 };
18 CallbackList.prototype = {
19 /**
20 * Adds a callback for given identifier.
21 *
22 * @param {string} identifier arbitrary string to group and identify callbacks
23 * @param {function} callback callback function
24 * @return {boolean} false if callback is not a function
25 */
26 add: function(identifier, callback) {
27 if (typeof callback !== 'function') {
28 throw new TypeError("Expected a valid callback as second argument for identifier '" + identifier + "'.");
29 return false;
30 }
31
32 if (!this._dictionary.has(identifier)) {
33 this._dictionary.set(identifier, []);
34 }
35
36 this._dictionary.get(identifier).push(callback);
37
38 return true;
39 },
40
41 /**
42 * Removes all callbacks registered for given identifier
43 *
44 * @param {string} identifier arbitrary string to group and identify callbacks
45 */
46 remove: function(identifier) {
47 this._dictionary.remove(identifier);
48 },
49
50 /**
51 * Invokes callback function on each registered callback.
52 *
53 * @param {string} identifier arbitrary string to group and identify callbacks
54 * @param {function(function)} callback function called with the individual callback as parameter
55 */
56 forEach: function(identifier, callback) {
57 var callbacks = this._dictionary.get(identifier);
58 if (callbacks !== null) {
59 callbacks.forEach(callback);
60 }
61 }
62 };
63
64 return CallbackList;
65 });