Skip to content

Commit

Permalink
added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
robotmayo committed Sep 20, 2016
1 parent 4f950a2 commit 40ad58a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A nodejs blogging platform",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava",
"docs": "documentation build src/* -o docs -f html"
},
"author": "robotmayo",
"license": "GPL-3.0",
Expand All @@ -18,6 +19,7 @@
"passport-local": "^1.0.0"
},
"devDependencies": {
"ava": "^0.16.0"
"ava": "^0.16.0",
"documentation": "^4.0.0-beta10"
}
}
65 changes: 52 additions & 13 deletions src/plugins/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,42 @@ const HOOK_TYPES = {
ACTION : 'action'
};
const HOOK_TYPES_ARR = [HOOK_TYPES.FILTER, HOOK_TYPES.STATIC, HOOK_TYPES.ACTION];

/**
* @namespace Plugins
*/
const Plugins = {
hookMap : new Map()
/**
* @memberOf Plugins
* @type {Map<String,hookData>}
*/
hookMap : new Map(),
addHook,
fireHook,
fireActionHook,
fireFilterHook,
fireStaticHook
};
module.exports = Plugins;

/**
* Hooks come in 2 types action, filter, static
*
* @typedef {Object} hookData
* @property {string} hook
* @property {number} priority
* @property {function(context:object):Promise<Any|Error>} fn
* @property {number} id pluginID
*/


/**
*
*
* @memberof Plugins
* @param {number} pluginID
* @param {object} opts
* @param {string} opts.hook
* @param {number} opts.priority
* @param {function} opts.fn
* @returns {Promise<Resolve|Error>}
* @returns {Promise<undefined|Error>}
*/
function addHook(pluginID, opts){
if(pluginID == null) return Promise.reject(new Error('PluginID required'));
Expand All @@ -40,8 +58,14 @@ function addHook(pluginID, opts){
Plugins.hookMap.get(data.hook).push(data);
return Promise.resolve();
}
Plugins.addHook = addHook;

/**
*
* @memberof Plugins
* @param {string} hook
* @param {object} context
* @returns {Promise<Any|Error>}
*/
function fireHook(hook, context){
const hookType = hook.split('::')[0];
if(HOOK_TYPES.indexOf(hookType) === -1) return Promise.reject(new Error('Invalid hooktype'));
Expand All @@ -51,15 +75,28 @@ function fireHook(hook, context){
else if(hookType === HOOK_TYPES.STATIC) return fireStaticHook(hooks, context);
else if(hookType === HOOK_TYPES.ACTION) return fireActionHook(hooks, context);
}
Plugins.fireHook = fireHook;


/**
*
* @memberof Plugins
* @param {hookData[]} hooks
* @param {object} context
* @returns {Promise<Any|Error>}
*/
function fireFilterHook(hooks, context){
return Promise.reduce(hooks, function(accum, data){
return data.fn(accum);
}, context);
}
Plugins.fireFilterHook = fireFilterHook;

/**
*
* @memberof Plugins
* @param {hookData[]} hooks
* @param {object} context
* @returns {Promise<Any|Error>}
*/
function fireStaticHook(hooks, context){
return Promise.each(hooks, function(data){
return data.fn(context)
Expand All @@ -72,15 +109,17 @@ function fireStaticHook(hooks, context){
})
});
}
Plugins.fireStaticHook = fireStaticHook;

/**
*
* @memberof Plugins
* @param {hookData[]} hooks
* @param {object} context
* @returns {Promise<Any|Error>}
*/
function fireActionHook(hooks, context){
return Promise.all(hooks.map(d => d.fn(context)));
}
Plugins.fireActionHook = fireActionHook;






0 comments on commit 40ad58a

Please sign in to comment.