50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
// 极简事件总线,用于在不建立父子 props 链的前提下让页面间互相通知
|
|
// 典型场景:任务日志轮询到某任务完成后,通知使用日志刷新,使额度/费用统计即时更新
|
|
const listeners = new Map();
|
|
|
|
export const EVENTS = {
|
|
TASK_COMPLETED: 'task_completed',
|
|
};
|
|
|
|
export const subscribe = (event, handler) => {
|
|
if (!listeners.has(event)) listeners.set(event, new Set());
|
|
listeners.get(event).add(handler);
|
|
return () => {
|
|
const set = listeners.get(event);
|
|
if (set) set.delete(handler);
|
|
};
|
|
};
|
|
|
|
export const emit = (event, payload) => {
|
|
const set = listeners.get(event);
|
|
if (!set) return;
|
|
set.forEach((fn) => {
|
|
try {
|
|
fn(payload);
|
|
} catch (e) {
|
|
// 单个订阅者异常不应阻断其他订阅者
|
|
// eslint-disable-next-line no-console
|
|
console.error('eventBus listener error:', e);
|
|
}
|
|
});
|
|
};
|