snappymail/dev/Common/Events.js

42 lines
765 B
JavaScript
Raw Normal View History

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 (typeof name === 'object') {
2016-06-16 07:36:44 +08:00
context = func || null;
func = null;
Object.entries(name).forEach(([subFunc, subName]) => {
2016-06-16 07:36:44 +08:00
sub(subName, subFunc, context);
});
2019-07-05 03:19:24 +08:00
} else {
if (undefined === 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
if (undefined !== SUBS[name]) {
SUBS[name].forEach(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
}
}