4 * Simple API to store and invoke multiple callbacks per identifier.
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
11 define(['Dictionary'], function(Dictionary
) {
15 function CallbackList() {
16 this._dictionary
= new Dictionary();
18 CallbackList
.prototype = {
20 * Adds a callback for given identifier.
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
26 add: function(identifier
, callback
) {
27 if (typeof callback
!== 'function') {
28 throw new TypeError("Expected a valid callback as second argument for identifier '" + identifier
+ "'.");
32 if (!this._dictionary
.has(identifier
)) {
33 this._dictionary
.set(identifier
, []);
36 this._dictionary
.get(identifier
).push(callback
);
42 * Removes all callbacks registered for given identifier
44 * @param {string} identifier arbitrary string to group and identify callbacks
46 remove: function(identifier
) {
47 this._dictionary
.remove(identifier
);
51 * Invokes callback function on each registered callback.
53 * @param {string} identifier arbitrary string to group and identify callbacks
54 * @param {function(function)} callback function called with the individual callback as parameter
56 forEach: function(identifier
, callback
) {
57 var callbacks
= this._dictionary
.get(identifier
);
58 if (callbacks
!== null) {
59 callbacks
.forEach(callback
);