Practicing Prototypical Code
I am focusing on practicing a very prototypical structure with my code (non library oriented) to begin learning to reuse large portions of my application infrastructures where applicable in future projects. With this in mind, I do not have experience with prototypical programming before now, and would like to ensure that I'm not misusing the strategy.
Code Summary
The following server side JavaScript code has the purpose of handling a large number of Socket.io events in a modulated, prototypical way. My goal is code that is readable, but also reusable. I feel as if I may be sacrificing the clarity of my code in order to utilize a prototype for the handlers, which eliminates the inclusion of duplicate code in each handler, most of which helps with debug logging.
Review
Requesting general improvements, best practices, code readability, etc.
Most importantly, I'm interested in feedback on the comparison between the prototypical and non prototypical event handling, and whether or not I'm using that strategy correctly.
Non Prototypical Handlers
control_center.js
/* Server Control Center */
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Control Center");
var socketHandler = require("custom_modules/socket_handler");
//APP INITIALIZE
debug.log("Application initialized...");
settings.js
module.exports = {
"debug" : true
}
debug.js
module.exports = function debug(name) {
this.settings = require('custom_modules/settings');
this.name = name;
this.log = function (message) {
if (this.settings.debug === true) {
console.log("[Debug][" + name + "]: " + message);
}
};
};
socket_handler.js
/* Socket Handler */
module.exports = function () {
//REQUIRE NPM MODULES
var socket = require('socket.io')(4546);
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Sale Handler");
var login = require("custom_modules/login");
var register = require("custom_modules/register");
var sale = require("custom_modules/sale");
socket.on("connection", function (client) {
//Handle Connection
debug.log("Client connected to socket!");
// Handle Custom Events
login.handle(client);
register.handle(client);
sale.handle(client);
});
};
register.js
/* Register Handler */
//DEFINE MODULE EXPORTS
module.exports.handle = function (client) {
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Register Handler");
//HANDLE EVENTS
debug.log("Initialized");
client.on("register", function (data) {
debug.log("Handling registration...");
});
};
login.js
/* Login Handler */
//DEFINE MODULE EXPORTS
module.exports.handle = function (client) {
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Sale Handler");
//HANDLE EVENTS
debug.log("Initialized");
client.on("login", function (data) {
debug.log("Handling login...");
});
};
sale.js
/* Sale Handler */
//DEFINE MODULE EXPORTS
module.exports.handle = function (client) {
//REQUIRE CUSTOM MODULES
var debug = new(require("custom_modules/debug"))("Sale Handler");
//HANDLE EVENTS
debug.log("Initialized");
client.on("sale", function (data) {
debug.log("Handling sale...");
debug.log("Streaming data to all clients: " + data);
socket.sockets.emit("saleUpdate", data);
});
};
This is the code in a modulated fashion, without the utilization of prototypes for the Socket.io handlers. (I'm using a prototype for the debugging object only).
Prototypical Handlers
control_center.js === above
settings.js === above
debug.js === above
socket_handler.js === above
handler.js
/* Handler */
module.exports = function (client, handlerName, handlerFunc) {
//DEFINE MODULE EXPORTS
this.handle = function (client) {
//REQUIRE CUSTOM MODULES
this.debug = new(require("custom_modules/debug"))(handlerName);
//HANDLE EVENTS
this.debug.log("Initialized");
handlerFunc();
};
};
login.js
/* Login Handler */
module.exports.handle = new(require("custom_modules/handler"))(client,
"Login Handler", function () {
client.on("login", function (data) {
debug.log("Handling login...");
});
});
register.js
/* Register Handler */
module.exports.handle = new(require("custom_modules/handler"))(client,
"Register Handler", function () {
client.on("register", function (data) {
debug.log("Handling registration...");
});
});
sale.js
/* Sale Handler */
module.exports.handle = new(require("custom_modules/handler"))(client,
"Sale Handler", function () {
client.on("sale", function (data) {
debug.log("Handling sale...");
debug.log("Streaming data to all clients: " + data);
socket.sockets.emit("saleUpdate", data);
});
});
This is the code in a modulated fashion, with the utilization of prototypes for the Socket.io handlers and debugging.