snappymail/dev/Common/Events.js

53 lines
801 B
JavaScript
Raw Normal View History

2015-11-15 08:23:16 +08:00
2016-07-02 06:49:59 +08:00
import _ from '_';
2016-06-07 05:57:52 +08:00
import {isObject, isUnd} from 'Common/Utils';
2016-06-16 07:36:44 +08:00
import * as Plugins from 'Common/Plugins';
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
const SUBS = {};
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string|Object} name
* @param {Function} func
* @param {Object=} context
*/
export function sub(name, func, context)
{
if (isObject(name))
{
context = func || null;
func = null;
_.each(name, (subFunc, subName) => {
sub(subName, subFunc, context);
});
}
else
{
if (isUnd(SUBS[name]))
2015-11-15 08:23:16 +08:00
{
2016-06-16 07:36:44 +08:00
SUBS[name] = [];
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
SUBS[name].push([func, context]);
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} name
* @param {Array=} args
*/
export function pub(name, args)
{
Plugins.runHook('rl-pub', [name, args]);
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
if (!isUnd(SUBS[name]))
{
_.each(SUBS[name], (items) => {
if (items[0])
{
items[0].apply(items[1] || null, args || []);
}
});
2015-11-15 08:23:16 +08:00
}
}