debug-server-next
Version:
Dev server for hippy-core.
52 lines (51 loc) • 1.24 kB
JavaScript
;
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() {
pubsub.removeAllListeners(this.channel);
}
/**
* 含通配符 * 的取消订阅
*/
pUnsubscribe() {
pubsub.removeAllListeners(this.channel);
}
/**
* 断开连接,emitter 无需实现,故写作空
*/
disconnect() { }
}
exports.MemoryPubSub = MemoryPubSub;