Skip to content

Commit

Permalink
better callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Nov 4, 2012
1 parent d374758 commit 4718ded
Show file tree
Hide file tree
Showing 34 changed files with 225 additions and 287 deletions.
56 changes: 0 additions & 56 deletions actions/actionsView.js
Expand Up @@ -36,62 +36,6 @@ action.run = function(api, connection, next){
outputExample: api.actions[i].outputExample,
});
}
if(connection.type == "socket"){
connection.response.actions.push({
name: "quit",
description: "I will send your intent to quit to the API. You will get a goodbye message and then be disconnected",
inputs: {"required" : [],"optional" : []},
outputExample: {"status": "Bye!"},
});
connection.response.actions.push({
name: "paramAdd",
description: "I am used to add a single saved param to your session. IE: `addParam key=value`",
inputs: {"required" : ['key=value'],"optional" : []},
outputExample: {"status": "OK"},
});
connection.response.actions.push({
name: "paramDelete",
description: "I am used to delte a previously saved param from your session. IE: `paramDelete key`",
inputs: {"required" : ['key'],"optional" : []},
outputExample: {"status": "OK"},
});
connection.response.actions.push({
name: "paramView",
description: "I am used to view a previously saved param from your session. IE: `paramView key`",
inputs: {"required" : ['key'],"optional" : []},
outputExample: {key: 'value'},
});
connection.response.actions.push({
name: "paramsView",
description: "I am used to view all previously saved params session. IE: `paramsView`",
inputs: {"required" : [],"optional" : []},
outputExample: {key1: 'value1', key2: 'value2'},
});
connection.response.actions.push({
name: "paramsDelete",
description: "I am used to delete all previously saved params session. IE: `paramsDelete`",
inputs: {"required" : [],"optional" : []},
outputExample: {"status": "OK"},
});
connection.response.actions.push({
name: "roomChange",
description: "I am used to change the room the connection is in and listening to resonses from",
inputs: {"required" : ['room'],"optional" : []},
outputExample: {"status": "OK"},
});
connection.response.actions.push({
name: "roomView",
description: "I am used to view which room I am in",
inputs: {"required" : [],"optional" : []},
outputExample: {"status": "OK"},
});
connection.response.actions.push({
name: "say",
description: "I am used to send a message to all other clients in my room",
inputs: {"required" : ["say hello world"],"optional" : []},
outputExample: {"status": "OK"},
});
}
connection.response.actions.sort(function compare(a,b) {
if (a.name < b.name)
return -1;
Expand Down
6 changes: 3 additions & 3 deletions actions/chat.js
Expand Up @@ -42,7 +42,7 @@ action.run = function(api, connection, next){
if(connection.params.message != null){
api.chatRoom.socketRoomBroadcast(api, connection, connection.params.message);
}else{
connection.error = "message is required to use the say method";
connection.error = new Error("message is required to use the say method");
}
next(connection, true);
}else if(connection.params.method == "messages"){
Expand All @@ -51,11 +51,11 @@ action.run = function(api, connection, next){
next(connection, true);
});
}else{
connection.error = connection.params.method + " is not a known chat method";
connection.error = new Error(connection.params.method + " is not a known chat method");
next(connection, true);
}
}else{
connection.error = "this action is only for web clients; use your proticol's native methods";
connection.error = new Error("this action is only for web clients; use your proticol's native methods");
next(connection, true);
}
};
Expand Down
26 changes: 11 additions & 15 deletions api.js
Expand Up @@ -8,7 +8,7 @@ var createActionHero = function(){
var actionHero = new Object;
actionHero.running = false;

actionHero.start = function(params, callback){
actionHero.start = function(params, next){

if (params == null){params = {};}
actionHero.startngParams = params;
Expand Down Expand Up @@ -146,12 +146,12 @@ var createActionHero = function(){
startTaskProcessing: function(next){
api.tasks.startTaskProcessing(api, next);
},
_complete: function(next){
_complete: function(){
api.log("server ID: " + api.id);
api.log(successMessage, ["green", "bold"]);
actionHero.running = true;
if(callback != null){
callback(api);
if(next != null){
next(null, api);
// next();
}else{
// next();
Expand Down Expand Up @@ -186,7 +186,7 @@ var createActionHero = function(){
closed = -1;
actionHero.running = false;
actionHero.api.log("The actionHero has been stopped", "bold");
next(true);
next(null, actionHero.api);
}
}

Expand Down Expand Up @@ -219,14 +219,10 @@ var createActionHero = function(){
if(actionHero.api.redis.enable){
clearTimeout(actionHero.api.redis.pingTimer);
clearTimeout(actionHero.api.redis.lostPeerTimer);
actionHero.api.redis.client.llen("actionHero:peers", function(err, length){
actionHero.api.redis.client.lrange("actionHero:peers", 0, length, function(err, peers){
actionHero.api.redis.client.lrem("actionHero:peers", 1, actionHero.api.id, function(err, count){
if(count != 1){ actionHero.api.log("Error removing myself from the peers list", "red"); }
actionHero.api.redis.client.hdel("actionHero:peerPings", actionHero.api.id, function(){
cont();
});
});
actionHero.api.redis.client.lrem("actionHero:peers", 1, actionHero.api.id, function(err, count){
if(count != 1){ actionHero.api.log("Error removing myself from the peers list", "red"); }
actionHero.api.redis.client.hdel("actionHero:peerPings", actionHero.api.id, function(){
cont();
});
});
}else{
Expand All @@ -242,12 +238,12 @@ var createActionHero = function(){
if(actionHero.running == true){
actionHero.stop(function(){
actionHero.start(actionHero.startngParams, function(){
if(typeof next == "function"){ next(true, actionHero.api); }
if(typeof next == "function"){ next(null, actionHero.api); }
});
});
}else{
actionHero.start(actionHero.startngParams, function(){
if(typeof next == "function"){ next(true, actionHero.api); }
if(typeof next == "function"){ next(null, actionHero.api); }
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion config.js
Expand Up @@ -9,7 +9,7 @@ var configData = {};
/////////////////////////

configData.general = {
"apiVersion": "3.0.14",
"apiVersion": "3.1.0",
"serverName": "actionHero API",
// The welcome message seen by TCP and webSocket clients upon connection
"welcomeMessage" : "Hello! Welcome to the actionHero api",
Expand Down
2 changes: 1 addition & 1 deletion examples/servers/actionHero_cluster_peer_1.js
Expand Up @@ -27,6 +27,6 @@ params.initFunction = function(api, next){
}

// start the server!
actionHero.start(params, function(api){
actionHero.start(params, function(err, api){
api.log("Boot Sucessful!");
});
2 changes: 1 addition & 1 deletion examples/servers/actionHero_cluster_peer_2.js
Expand Up @@ -27,6 +27,6 @@ params.initFunction = function(api, next){
}

// start the server!
actionHero.start(params, function(api){
actionHero.start(params, function(err, api){
api.log("Boot Sucessful!");
});
2 changes: 1 addition & 1 deletion examples/servers/actionHero_cluster_peer_3.js
Expand Up @@ -28,6 +28,6 @@ params.initFunction = function(api, next){
}

// start the server!
actionHero.start(params, function(api){
actionHero.start(params, function(err, api){
api.log("Boot Sucessful!");
});
2 changes: 1 addition & 1 deletion examples/servers/actionHero_single_node.js
Expand Up @@ -18,6 +18,6 @@ params.initFunction = function(api, next){
}

// start the server!
actionHero.start(params, function(api){
actionHero.start(params, function(err, api){
api.log("Boot Sucessful!");
});
Expand Up @@ -19,7 +19,7 @@ params.initFunction = function(api, next){

// start the server!
var timer = 5000;
actionHero.start(params, function(api){
actionHero.start(params, function(err, api){

api.log(" >> Boot Sucessful!");
setTimeout(function(){
Expand Down
12 changes: 6 additions & 6 deletions helpers/_specHelper.js
Expand Up @@ -146,13 +146,13 @@ specHelper.startServer = function(serverID, next){
if(err.code == "ECONNREFUSED"){
specHelper.actionHeroes[serverID] = new baseActionHero;
if(serverID == 0){
specHelper.actionHeroes[serverID].start({configChanges: specHelper.params[serverID], initFunction: specHelper.initFunction}, function(api){
specHelper.actionHeroes[serverID].start({configChanges: specHelper.params[serverID], initFunction: specHelper.initFunction}, function(err, api){
specHelper.apis[serverID] = api;
conn.destroy();
next(specHelper.apis[serverID]);
});
}else{
specHelper.actionHeroes[serverID].start({configChanges: specHelper.params[serverID], initFunction: specHelper.initFunction}, function(api){
specHelper.actionHeroes[serverID].start({configChanges: specHelper.params[serverID], initFunction: specHelper.initFunction}, function(err, api){
specHelper.apis[serverID] = api;
conn.destroy();
next(specHelper.apis[serverID]);
Expand All @@ -168,8 +168,8 @@ specHelper.startServer = function(serverID, next){
specHelper.stopServer = function(serverID, next){
if(serverID == null){serverID = 0};
if(specHelper.actionHeroes[serverID] != null){
specHelper.actionHeroes[serverID].stop(function(resp){
next(resp);
specHelper.actionHeroes[serverID].stop(function(err, api){
next(err, api);
});
}else{
next(false);
Expand All @@ -178,8 +178,8 @@ specHelper.stopServer = function(serverID, next){

specHelper.restartServer = function(serverID, next){
if(serverID == null){serverID = 0};
specHelper.actionHeroes[serverID].restart(function(resp, api){
next(resp, api);
specHelper.actionHeroes[serverID].restart(function(err, api){
next(err, api);
});
};

Expand Down
3 changes: 1 addition & 2 deletions helpers/utils.js
Expand Up @@ -143,10 +143,9 @@ utils.parseCookies = function(req){
utils.setupConnection = function(api, connection, type, remotePort, remoteIP){
if(connection == null){ connection = {}; }
connection.type = type;
connection.error = false;
connection.error = null;
connection.params = {};
connection.response = {};
connection.errror = false;
connection.remotePort = remotePort;
connection.remoteIP = remoteIP;
if(connection.room == null){ connection.room = api.configData.general.defaultChatRoom; }
Expand Down
6 changes: 3 additions & 3 deletions initializers/initActions.js
Expand Up @@ -109,12 +109,12 @@ var initActions = function(api, next)
connection.params.offset = parseFloat(connection.params.offset);
}

if (connection.error === false){
if (connection.error === null){
if(connection.type == "web"){ api.utils.processRoute(api, connection); }
connection.action = connection.params["action"];
if(api.actions[connection.action] != undefined){
api.utils.requiredParamChecker(api, connection, api.actions[connection.action].inputs.required);
if(connection.error == false){
if(connection.error === null){
process.nextTick(function() {
if(api.domain != null){
var actionDomain = api.domain.create();
Expand Down Expand Up @@ -143,7 +143,7 @@ var initActions = function(api, next)
}
}else{
if(connection.action == "" || connection.action == null){ connection.action = "{no action}"; }
connection.error = connection.action + " is not a known action.";
connection.error = new Error(connection.action + " is not a known action.");
process.nextTick(function(){
connection.respondingTo = messageID;
next(connection, true);
Expand Down

0 comments on commit 4718ded

Please sign in to comment.