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) {
|
2020-07-30 03:49:41 +08:00
|
|
|
if (typeof name === 'object') {
|
2016-06-16 07:36:44 +08:00
|
|
|
context = func || null;
|
|
|
|
func = null;
|
|
|
|
|
2020-07-22 20:49:18 +08:00
|
|
|
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 {
|
2020-07-30 03:49:41 +08:00
|
|
|
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
|
|
|
|
2020-07-30 03:49:41 +08:00
|
|
|
if (undefined !== SUBS[name]) {
|
2020-07-22 20:49:18 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|