/**
 * React Hooks 支持
 * 提供便捷的 React 组件事件订阅能力
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get useAppEvent () {
        return useAppEvent;
    },
    get useAppEventEmitter () {
        return useAppEventEmitter;
    },
    get useAppEventOnce () {
        return useAppEventOnce;
    }
});
const _react = require("react");
function useAppEvent(eventBus, event, listener, options) {
    // 使用 ref 保存最新的 listener，避免重复订阅
    const listenerRef = (0, _react.useRef)(listener);
    listenerRef.current = listener;
    // 使用 ref 保存 options
    const optionsRef = (0, _react.useRef)(options);
    optionsRef.current = options;
    (0, _react.useEffect)(()=>{
        // 创建包装的监听器，使用最新的 listener
        const wrappedListener = (data, metadata)=>{
            return listenerRef.current(data, metadata);
        };
        // 订阅事件
        const unsubscribe = eventBus.on(event, wrappedListener, optionsRef.current);
        // 清理函数：取消订阅
        return unsubscribe;
    }, [
        eventBus,
        event
    ]); // 只在 eventBus 或 event 变化时重新订阅
}
function useAppEventOnce(eventBus, event, listener, options) {
    const listenerRef = (0, _react.useRef)(listener);
    listenerRef.current = listener;
    const optionsRef = (0, _react.useRef)(options);
    optionsRef.current = options;
    (0, _react.useEffect)(()=>{
        const wrappedListener = (data, metadata)=>{
            return listenerRef.current(data, metadata);
        };
        const unsubscribe = eventBus.once(event, wrappedListener, optionsRef.current);
        return unsubscribe;
    }, [
        eventBus,
        event
    ]);
}
function useAppEventEmitter(eventBus) {
    return (0, _react.useCallback)((event, data, metadata)=>{
        return eventBus.emit(event, data, metadata);
    }, [
        eventBus
    ]);
}

//# sourceMappingURL=hooks.js.map