Refactored plugins injection

Passes a dependency map, makes it easier to add dependencies later
Esse commit está contido em:
Laurent Eschenauer
2013-05-18 22:36:51 +02:00
commit 7fcf87379e
4 arquivos alterados com 16 adições e 8 exclusões
+9 -1
Ver Arquivo
@@ -72,6 +72,14 @@ var pushNavData = function() {
}; };
var navTimer = setInterval(pushNavData, 100); var navTimer = setInterval(pushNavData, 100);
// Prepare dependency map for plugins
var deps = {
server: server
, app: app
, io: io
, client: client
};
// Load the plugins // Load the plugins
var dir = path.join(__dirname, 'plugins'); var dir = path.join(__dirname, 'plugins');
@@ -79,7 +87,7 @@ config.plugins.forEach(function (plugin) {
console.log("Loading " + plugin + " plugin."); console.log("Loading " + plugin + " plugin.");
// Load the backend code // Load the backend code
require(path.join(dir, plugin))(plugin, app, io, client); require(path.join(dir, plugin))(plugin, deps);
// Add the public assets to a static route // Add the public assets to a static route
if (fs.existsSync(assets = path.join(dir, plugin, 'public'))) { if (fs.existsSync(assets = path.join(dir, plugin, 'public'))) {
+1 -1
Ver Arquivo
@@ -1,4 +1,4 @@
function hud(name, app, io, client) { function hud(name, deps) {
// Nothing special on the backend for now // Nothing special on the backend for now
}; };
+4 -4
Ver Arquivo
@@ -1,14 +1,14 @@
function pilot(iname, app, io, client) { function pilot(name, deps) {
io.sockets.on('connection', function (socket) { deps.io.sockets.on('connection', function (socket) {
socket.on('/pilot/move', function (cmd) { socket.on('/pilot/move', function (cmd) {
var _name; var _name;
console.log("move", cmd); console.log("move", cmd);
return typeof client[_name = cmd.action] === "function" ? client[_name](cmd.speed) : void 0; return typeof deps.client[_name = cmd.action] === "function" ? deps.client[_name](cmd.speed) : void 0;
}); });
socket.on('/pilot/drone', function (cmd) { socket.on('/pilot/drone', function (cmd) {
var _name; var _name;
console.log("drone", cmd); console.log("drone", cmd);
return typeof client[_name = cmd.action] === "function" ? client[_name]() : void 0; return typeof deps.client[_name = cmd.action] === "function" ? deps.client[_name]() : void 0;
}); });
}); });
}; };
+2 -2
Ver Arquivo
@@ -1,11 +1,11 @@
var arDrone = require('ar-drone'); var arDrone = require('ar-drone');
function video(name, app, io, client) { function video(name, deps) {
var latestImage; var latestImage;
// Add a new route to fetch camera image // Add a new route to fetch camera image
app.get('/camera/:id', function(req, res) { deps.app.get('/camera/:id', function(req, res) {
if (!latestImage) { if (!latestImage) {
res.writeHead(301, {"Location": "/plugin/" + name + "/images/nofeed.jpg"}); res.writeHead(301, {"Location": "/plugin/" + name + "/images/nofeed.jpg"});
res.end(); res.end();