Skip to content

Commit

Permalink
Add remove functionality in eventingLibrary.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Dec 29, 2014
1 parent 9d06818 commit 84b2df5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion eventingLibrary/eventingLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,29 @@ var mixEvents = function (obj) {
};

obj.remove = function (eventType, callback) {

if (arguments.length === 0) {
throw new Error('Missing both arguments for remove');
}

if (typeof eventType !== 'string') {
throw new Error('Please pass in a string value for the event type to remove');
}

storage[eventType] = storage[eventType] || [];
if (arguments.length >= 2) {
var i = 0;
while (i < storage[eventType].length) {
if (storage[eventType][i] === callback) {
// remove and don't increment i
storage[eventType].splice(i, 1);
} else {
i += 1;
}
}
} else {
// remove all callback functions associated with the event type
delete storage[eventType];
}
};

return obj;
Expand Down

0 comments on commit 84b2df5

Please sign in to comment.