Created
January 30, 2014 20:20
-
-
Save ziyan-junaideen/8717925 to your computer and use it in GitHub Desktop.
Making callback functions safe - I mean not fail catastrophically because not providing a function or array of functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class window.SafeCallback | |
@arrayCallback: ( callback ) -> | |
if typeof callback == 'function' | |
console.log "SafeCallback: expected array, found function..." | |
return [ callback ] | |
if callback instanceof Array | |
console.log "SafeCallback: filtering functions from array..." | |
result = [] | |
for item in callback | |
result.push item if typeof item == 'function' | |
return result | |
console.log "SafeCallback: error with callback..." | |
return () -> | |
@callback: ( callback ) -> | |
return callback if typeof callback == 'function' | |
return () -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.6.3 | |
(function() { | |
window.SafeCallback = (function() { | |
function SafeCallback() {} | |
SafeCallback.arrayCallback = function(callback) { | |
var item, result, _i, _len; | |
if (typeof callback === 'function') { | |
console.log("SafeCallback: expected array, found function..."); | |
return [callback]; | |
} | |
if (callback instanceof Array) { | |
console.log("SafeCallback: filtering functions from array..."); | |
result = []; | |
for (_i = 0, _len = callback.length; _i < _len; _i++) { | |
item = callback[_i]; | |
if (typeof item === 'function') { | |
result.push(item); | |
} | |
} | |
return result; | |
} | |
console.log("SafeCallback: error with callback..."); | |
return function() {}; | |
}; | |
SafeCallback.callback = function(callback) { | |
if (typeof callback === 'function') { | |
return callback; | |
} | |
return function() {}; | |
}; | |
return SafeCallback; | |
})(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment