plugins fireHook refactor

(drunk)
v1.18.x
barisusakli 10 years ago
parent e93b2b6aa1
commit 3f12d363f7

@ -388,120 +388,77 @@ var fs = require('fs'),
return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0); return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0);
}; };
Plugins.fireHook = function(hook) { Plugins.fireHook = function(hook, params, callback) {
var callback = typeof arguments[arguments.length-1] === 'function' ? arguments[arguments.length-1] : null, callback = typeof callback === 'function' ? callback : function() {};
args = arguments.length ? Array.prototype.slice.call(arguments, 1) : [];
if (callback) {
args.pop();
}
var hookList = Plugins.loadedHooks[hook]; var hookList = Plugins.loadedHooks[hook];
if (Array.isArray(hookList)) { if (!Array.isArray(hookList) || !hookList.length) {
// if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); return callback(null, params);
var hookType = hook.split(':')[0]; }
switch (hookType) {
case 'filter':
async.reduce(hookList, args, function(value, hookObj, next) {
if (hookObj.method) {
if (!hookObj.hasOwnProperty('callbacked') || hookObj.callbacked === true) {
// omg, after 6 months I finally realised what this does...
// It adds the callback to the arguments passed-in, since the callback
// is defined in *this* file (the async cb), and not the hooks themselves.
value = hookObj.method.apply(Plugins, value.concat(function() {
next(arguments[0], Array.prototype.slice.call(arguments, 1));
}));
/*
Backwards compatibility block for v0.5.0
Remove this once NodeBB enters v0.5.0-1
*/
if (value !== undefined && value !== callback) {
winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook);
next(null, [value]);
}
} else {
winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook);
value = hookObj.method.apply(Plugins, value);
next(null, [value]);
}
/* End backwards compatibility block */
} else {
if (global.env === 'development') {
winston.info('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
}
next(null, [value]);
}
}, function(err, values) {
if (err) {
if (global.env === 'development') {
winston.info('[plugins] Problem executing hook: ' + hook + ' err: ' + JSON.stringify(err));
}
}
if (callback) { // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
callback.apply(Plugins, [err].concat(values)); var hookType = hook.split(':')[0];
} switch (hookType) {
}); case 'filter':
break; fireFilterHook(hook, hookList, params, callback);
case 'action': break;
var deprecationWarn = []; case 'action':
async.each(hookList, function(hookObj, next) { fireActionHook(hook, hookList, params, callback);
/* break;
Backwards compatibility block for v0.5.0 case 'static':
Remove this once NodeBB enters v0.6.0-1 fireStaticHook(hook, hookList, params, callback);
*/ break;
if (hook === 'action:app.load') { default:
deprecationWarn.push(hookObj.id); winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook);
} break;
/* End backwards compatibility block */ }
};
if (hookObj.method) { function fireFilterHook(hook, hookList, params, callback) {
hookObj.method.apply(Plugins, args); async.reduce(hookList, params, function(params, hookObj, next) {
} else { if (typeof hookObj.method !== 'function') {
if (global.env === 'development') { if (global.env === 'development') {
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
} }
} return next(null, params);
}
next(); hookObj.method(params, next);
}, function() {
/* }, function(err, values) {
Backwards compatibility block for v0.5.0 if (err) {
Remove this once NodeBB enters v0.6.0-1 winston.error('[plugins] Problem executing hook: ' + hook + ' err: ' + err.stack);
*/
if (deprecationWarn.length) {
winston.warn('[plugins] The `action:app.load` hook is deprecated in favour of `static:app.load`, please notify the developers of the following plugins:');
for(var x=0,numDeprec=deprecationWarn.length;x<numDeprec;x++) {
process.stdout.write(' * ' + deprecationWarn[x] + '\n');
}
}
/* End backwards compatibility block */
});
break;
case 'static':
async.each(hookList, function(hookObj, next) {
if (hookObj.method) {
hookObj.method.apply(Plugins, args.concat(next));
}
}, function(err) {
callback(err);
});
break;
default:
// Do nothing...
break;
} }
} else {
// Otherwise, this hook contains no methods callback(err, values);
if (callback) { });
callback.apply(this, [null].concat(args)); }
function fireActionHook(hook, hookList, params, callback) {
async.each(hookList, function(hookObj, next) {
if (typeof hookObj.method !== 'function') {
if (global.env === 'development') {
winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
}
return next();
} }
return args[0]; hookObj.method(params);
} next();
}; }, callback);
}
function fireStaticHook(hook, hookList, params,callback) {
async.each(hookList, function(hookObj, next) {
if (typeof hookObj.method === 'function') {
hookObj.method(params, next);
} else {
next();
}
}, callback);
}
Plugins.isActive = function(id, callback) { Plugins.isActive = function(id, callback) {
db.isSetMember('plugins:active', id, callback); db.isSetMember('plugins:active', id, callback);

Loading…
Cancel
Save