mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
added Exception handling and added applyCommands
This commit is contained in:
parent
7977220404
commit
b82a027f4f
2 changed files with 59 additions and 16 deletions
|
|
@ -1,33 +1,39 @@
|
||||||
define([
|
define([
|
||||||
"Game/Core/Protocol/Parser"
|
"Game/Core/Protocol/Parser",
|
||||||
|
"Lib/Utilities/Exception"
|
||||||
],
|
],
|
||||||
|
|
||||||
function (Parser) {
|
function (Parser, Exception) {
|
||||||
|
|
||||||
var Helper = {}
|
var Helper = {}
|
||||||
|
|
||||||
Helper.encodeCommand = function (command, options) {
|
Helper.encodeCommand = function (command, options) {
|
||||||
return Parser.encode(Helper.assemble(command, options));
|
var message = {};
|
||||||
|
message[command] = options || null;
|
||||||
|
return Parser.encode(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
Helper.assemble = function (command, options) {
|
Helper.applyCommand = function(options, target) {
|
||||||
var commands = {};
|
|
||||||
commands[command] = options || null;
|
|
||||||
return commands;
|
|
||||||
}
|
|
||||||
|
|
||||||
Helper.runCommands = function (message, callback) {
|
var message;
|
||||||
var commands;
|
if (typeof options == "string") {
|
||||||
if (typeof message == "string") {
|
message = Parser.decode(options);
|
||||||
commands = Parser.decode(message);
|
|
||||||
} else {
|
} else {
|
||||||
commands = message;
|
message = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(var command in commands) {
|
// The for loop is only here to get the key, it is not designed to get multiple commands
|
||||||
callback(command, commands[command]);
|
for(var command in message) {
|
||||||
|
var methodName = "on" + command.toUpperCaseFirstChar();
|
||||||
|
var options = message[command];
|
||||||
|
|
||||||
|
if (!target[methodName]) {
|
||||||
|
throw new Exception("Helper.applyCommand:", target, "has no method", methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
target[methodName].call(target, options);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return Helper;
|
return Helper;
|
||||||
|
|
||||||
|
|
|
||||||
37
app/Lib/Utilities/Exception.js
Normal file
37
app/Lib/Utilities/Exception.js
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
define([
|
||||||
|
],
|
||||||
|
|
||||||
|
function() {
|
||||||
|
|
||||||
|
function Exception(/* arguments */) {
|
||||||
|
var message = [];
|
||||||
|
for (var i = 0; i < arguments.length; i++) {
|
||||||
|
var arg = arguments[i];
|
||||||
|
if (typeof arg == "object") {
|
||||||
|
var name = this.getTypeOfObject(arg);
|
||||||
|
message.push(name);
|
||||||
|
} else {
|
||||||
|
message.push(arg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.message = message.join(" ");
|
||||||
|
|
||||||
|
Error.call(this, this.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception.prototype = Object.create(Error.prototype);
|
||||||
|
|
||||||
|
Exception.prototype.toString = function() {
|
||||||
|
return this.message;
|
||||||
|
};
|
||||||
|
|
||||||
|
Exception.prototype.getTypeOfObject = function (obj) {
|
||||||
|
var funcNameRegex = /function (.{1,})\(/;
|
||||||
|
var results = (funcNameRegex).exec((obj).constructor.toString());
|
||||||
|
return (results && results.length > 1) ? results[1] : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return Exception;
|
||||||
|
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue