"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryPubSub = void 0;
// 扩展 eventemitter3 以支持 redis 的通配符
const event_emitter_1 = require("@/utils/event-emitter");
const pubsub = new event_emitter_1.EventEmitter();
class MemoryPubSub {
    constructor(channel) {
        this.channel = channel;
    }
    /**
     * 发布 message 到 channel
     */
    publish(message) {
        let msgStr;
        if (typeof message !== 'string')
            msgStr = JSON.stringify(message);
        else
            msgStr = message;
        pubsub.emit(this.channel, msgStr, null, null, null, null);
    }
    /**
     * 订阅 channel
     */
    subscribe(cb) {
        pubsub.on(this.channel, cb);
    }
    /**
     * 含通配符 * 的订阅
     */
    pSubscribe(cb) {
        pubsub.on(this.channel, cb);
    }
    /**
     * 取消订阅
     */
    unsubscribe(cb) {
        if (cb)
            pubsub.off(this.channel, cb);
        else
            pubsub.removeAllListeners(this.channel);
    }
    /**
     * 含通配符 * 的取消订阅
     */
    pUnsubscribe(cb) {
        if (cb)
            pubsub.off(this.channel, cb);
        else
            pubsub.removeAllListeners(this.channel);
    }
    /**
     * 断开连接，emitter 无需实现，故写作空
     */
    disconnect() { }
}
exports.MemoryPubSub = MemoryPubSub;
