snappymail/dev/Common/Events.js

44 lines
793 B
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import _ from '_';
2019-07-05 03:19:24 +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
*/
2019-07-05 03:19:24 +08:00
export function sub(name, func, context) {
if (isObject(name)) {
2016-06-16 07:36:44 +08:00
context = func || null;
func = null;
_.each(name, (subFunc, subName) => {
sub(subName, subFunc, context);
});
2019-07-05 03:19:24 +08:00
} else {
if (isUnd(SUBS[name])) {
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
*/
2019-07-05 03:19:24 +08:00
export function pub(name, args) {
2016-06-16 07:36:44 +08:00
Plugins.runHook('rl-pub', [name, args]);
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
if (!isUnd(SUBS[name])) {
2016-06-16 07:36:44 +08:00
_.each(SUBS[name], (items) => {
2019-07-05 03:19:24 +08:00
if (items[0]) {
2016-06-16 07:36:44 +08:00
items[0].apply(items[1] || null, args || []);
}
});
2015-11-15 08:23:16 +08:00
}
}