@hippy/debug-server-next
Version:
Debug server for hippy.
122 lines (121 loc) • 4.17 kB
JavaScript
"use strict";
/*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisSubscriber = exports.RedisPublisher = void 0;
/**
* ⚠️ publish and subscribe must behind connection, otherwise redis client will change to PubSub mode, could not send AUTH command
*/
const log_1 = require("@debug-server-next/utils/log");
const redis_db_1 = require("./redis-db");
const log = new log_1.Logger('redis-pub-sub');
class RedisPublisher {
constructor(channel) {
if (!channel) {
const e = new Error('channelId should not be empty');
log.error('%s', e === null || e === void 0 ? void 0 : e.stack);
throw e;
}
this.channel = channel;
this.client = redis_db_1.RedisDB.client.duplicate();
(0, redis_db_1.listenRedisEvent)(this.client);
}
async publish(message) {
await this.connect();
const msgStr = typeof message !== 'string' ? JSON.stringify(message) : message;
try {
if (this.client.isOpen)
await this.client.publish(this.channel, msgStr);
}
catch (e) {
log.error('publish %s to channel %s error: %s', msgStr, this.channel, e.stack);
}
}
/**
* nullish, redis could send other command in PubSub mode
*/
async disconnect() {
if (this.client.isOpen)
await this.client.quit();
}
async connect() {
if (this.client.isOpen)
return;
await this.client.connect();
log.verbose('redis publisher client created, %s', this.channel);
}
}
exports.RedisPublisher = RedisPublisher;
class RedisSubscriber {
constructor(channel) {
if (!channel) {
const e = new Error('channelId should not be empty');
log.error('%s', e === null || e === void 0 ? void 0 : e.stack);
throw e;
}
this.channel = channel;
this.client = redis_db_1.RedisDB.client.duplicate();
(0, redis_db_1.listenRedisEvent)(this.client);
}
async subscribe(cb) {
await this.connect();
if (this.client.isOpen)
await this.client.subscribe(this.channel, cb);
}
/**
* subscribe channel with glob character, such as `*`
*/
async pSubscribe(cb) {
await this.connect();
if (this.client.isOpen)
await this.client.pSubscribe(this.channel, cb);
}
async unsubscribe() {
if (this.client.isOpen)
await this.client.unsubscribe(this.channel);
}
async pUnsubscribe() {
if (this.client.isOpen)
await this.client.pUnsubscribe(this.channel);
}
/**
* nullish, redis could send other command in PubSub mode
*/
async disconnect() {
if (this.client.isOpen) {
// must unsubscribe first, other will receive error: `Cannot send commands in PubSub mode`
await this.client.unsubscribe();
await this.client.pUnsubscribe();
try {
await this.client.quit();
}
catch (e) {
log.warn(`redis disconnect error %j`, e || e.stack);
}
}
}
async connect() {
if (this.client.isOpen)
return;
await this.client.connect();
log.verbose('redis subscriber client created, %s', this.channel);
}
}
exports.RedisSubscriber = RedisSubscriber;