Last active
August 29, 2015 14:04
-
-
Save wthit56/51286ebe0cb310fc98b4 to your computer and use it in GitHub Desktop.
CreateCallback.js, a callback creator/manager for client or server-side
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
var CreateCallback = (function() { | |
var pool = []; | |
function CreateCallback() { | |
if(pool.length){ | |
var callback = pool.pop(); | |
} | |
else { | |
function callback() { | |
if(callback.action){ | |
Array.prototype.push.call(arguments, callback); | |
callback.action.apply(this, arguments); | |
} | |
} | |
callback.action = null; | |
callback.dispose = _dispose.bind(callback); | |
callback.dispose.disposed = false; | |
callback.setAction = _setAction; | |
} | |
return callback; | |
} | |
CreateCallback.pool = pool; | |
CreateCallback.maxPoolSize = Infinity; | |
function _setAction(action){ | |
this.action = action; | |
return this; | |
} | |
function _dispose(){ | |
if (!this.dispose.disposed) { | |
if (pool.length < CreateCallback.maxPoolSize) { | |
pool.push(this); | |
} | |
this.action = null; | |
if (this.onDispose) { this.onDispose(this); } | |
this.dispose.disposed = true; | |
} | |
return this; | |
} | |
return CreateCallback; | |
})(); | |
if (undefined !== module) { module.exports = CreateCallback; } |
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
CreateCallback.maxPoolSize = 10; // pooled callbacks will never exceed this number (defaults to Infinity) | |
var callback = CreateCallback(); // creates a new callback function, or takes one from the pool if available | |
// setup | |
callback.var1 = 1; | |
callback.filepath = filepath; | |
callback.request = request; | |
callback.response = response; | |
// callback.action = action; // does the same thing as setAction | |
callback.onDispose = function() { // called after disposal and pooling | |
// cleanup | |
delete callback.var1; | |
delete callback.request; | |
delete callback.response; | |
}; | |
// setAction(action) == action will be called with added 'callback' argument after existing arguments | |
fs.exists(filepath, callback.setAction(exists)); | |
function exists(exists, callback) { | |
// 'exists' comes from the fs.exists callback trigger; any existing arguments are kept intact | |
// 'callback' is the original callback function, to be read from, reused, or disposed of | |
callback.var1 // == 1; properties set in setup persist | |
response.write("hello world!"); // including objects | |
response.end(); | |
if (!exists) { | |
callback.response.write("404") | |
callback.response.end(); | |
// .dispose() resets and pools the callback if applicable | |
// this is not required, but will help protect against Garbage Collection | |
callback.dispose(); | |
CreateCallback.pool.length // == 1 | |
} | |
else { | |
// callback.dispose can be used directly as an event listener | |
fs.createReadStream(callback.filepath).on("end", callback.dispose).pipe(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment