allowing plugin system to detect object method path by dot notation

v1.18.x
Julian Lam 12 years ago
parent 8ef2761f53
commit fa2fe5c941

@ -136,9 +136,16 @@ var fs = require('fs'),
var _self = this; var _self = this;
if (data.hook && data.method) { if (data.hook && data.method) {
// Assign default priority of 10 if none is passed-in
data.id = id; data.id = id;
if (!data.priority) data.priority = 10; if (!data.priority) data.priority = 10;
data.method = data.method.split('.').reduce(function(memo, prop) {
if (memo[prop]) {
return memo[prop];
} else {
// Couldn't find method by path, assuming property with periods in it (evil!)
_self.libraries[data.id][data.method];
}
}, _self.libraries[data.id]);
_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || []; _self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || [];
_self.loadedHooks[data.hook].push(data); _self.loadedHooks[data.hook].push(data);
@ -157,10 +164,15 @@ var fs = require('fs'),
switch (hookType) { switch (hookType) {
case 'filter': case 'filter':
async.reduce(hookList, args, function(value, hookObj, next) { async.reduce(hookList, args, function(value, hookObj, next) {
if (hookObj.callbacked) { // If a callback is present (asynchronous method) if (hookObj.method) {
_self.libraries[hookObj.id][hookObj.method](value, next); if (hookObj.callbacked) { // If a callback is present (asynchronous method)
} else { // Synchronous method hookObj.method.call(_self.libraries[hookObj.id], value, next);
value = _self.libraries[hookObj.id][hookObj.method](value); } else { // Synchronous method
value = hookObj.method.call(_self.libraries[hookObj.id], value);
next(null, value);
}
} else {
if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
next(null, value); next(null, value);
} }
}, function(err, value) { }, function(err, value) {
@ -175,13 +187,8 @@ var fs = require('fs'),
break; break;
case 'action': case 'action':
async.each(hookList, function(hookObj) { async.each(hookList, function(hookObj) {
if ( if (hookObj.method) hookObj.method.apply(_self.libraries[hookObj.id], args);
_self.libraries[hookObj.id] && else {
_self.libraries[hookObj.id][hookObj.method] &&
typeof _self.libraries[hookObj.id][hookObj.method] === 'function'
) {
_self.libraries[hookObj.id][hookObj.method].apply(_self.libraries[hookObj.id], args);
} else {
if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
} }
}); });

Loading…
Cancel
Save