Skip to content

Commit

Permalink
Merge pull request jxcore#10 from cleancoderocker/master
Browse files Browse the repository at this point in the history
Added type check of callback parameter.
  • Loading branch information
obastemur committed May 26, 2014
2 parents c2624bb + 756a3d9 commit b24c658
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 7 additions & 4 deletions backend/jx_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ exports.store = {
};

exports.addEvent = function (event, cb) {
if (!events[event]) {
events[event] = [];
if(typeof cb === "function") {
if (!events[event]) {
events[event] = [];
}
events[event].push(cb);
} else {
throw new TypeError("Callback is not a function.");
}

events[event].push(cb);
};

exports.emitEvent = function (event, param1, param2, param3) {
Expand Down
6 changes: 5 additions & 1 deletion backend/jxm.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ exports.setEngine = function (app) {
* @param listener {function} - Callback function which will be invoked upon emit of the event.
*/
exports.on = function (event, listener) {
helpers.addEvent(event, listener);
if(typeof listener === "function") {
helpers.addEvent(event, listener);
} else {
throw new TypeError("Listener is not a function.");
}
};


Expand Down

0 comments on commit b24c658

Please sign in to comment.