Skip to content

Instantly share code, notes, and snippets.

@naderchehab
Last active August 29, 2015 13:56
Show Gist options
  • Save naderchehab/9324498 to your computer and use it in GitHub Desktop.
Save naderchehab/9324498 to your computer and use it in GitHub Desktop.
// object type function prototype
var HelloWorld = function () {
// private function
function sayHelloWorld(workParam) {
return "Hello World " + workParam.userName;
};
// function that matches the unit of work defined work function
this.getHelloWorld = function (workParam) {
return {
helloWorld: sayHelloWorld(workParam)
};
};
};
// replicate node.js module loading system
module.exports = HelloWorld;
// load nPool module
var nPool = require('npool');
// load files defining object types
nPool.loadFile(1, __dirname + '/helloWorld.js');
// create thread pool with two threads
nPool.createThreadPool(2);
// work complete callback from thread pool
var helloWorldCallbackFunction = function (callbackObject, workId, exceptionObject) {
console.log("----------------------------------------");
console.log("Callback Function: Hello World\n");
console.log("Callback Context:");
console.log(this);
console.log("");
console.log("WorkId: " + workId + "\n");
if(exceptionObject == null) {
console.log("Callback Object:");
console.log(callbackObject);
console.log("");
}
else {
console.log(exceptionObject);
}
};
// object type to be used to demonstrate context param within unit of work
function ContextA() {
this.contextAProperty = "[Context A] Property";
this.contextAFunction = function() { console.log("[Context A] Function"); }
}
var ContextAObject = new ContextA();
// queue some other work on a special condition
var unitOfWork = {};
unitOfWork.workId = 99;
unitOfWork.fileKey = 1;
unitOfWork.workFunction = "getHelloWorld";
unitOfWork.workParam = {
userName: "nPool"
};
unitOfWork.callbackFunction = helloWorldCallbackFunction;
unitOfWork.callbackContext = ContextAObject;
// queue the unit of work
nPool.queueWork(unitOfWork);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment