Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类型脚本:类,静态变量,单例_Typescript - Fatal编程技术网

Typescript 类型脚本:类,静态变量,单例

Typescript 类型脚本:类,静态变量,单例,typescript,Typescript,您好,我有一个关于typescript、static和singleton的问题 我有以下逻辑我有一个主类,它的连接是一个静态变量,并且有一个通道是一个类的实例(也需要是静态的,也就是说,只是一个通道) 主类: class Broker { static connection: Connection = undefined; static channel: BrokerChannel = undefined; static urli: string = process.env.RABB

您好,我有一个关于typescript、static和singleton的问题 我有以下逻辑我有一个主类,它的连接是一个静态变量,并且有一个通道是一个类的实例(也需要是静态的,也就是说,只是一个通道)

主类:

class Broker {
  static connection: Connection = undefined;
  static channel: BrokerChannel = undefined;
  static urli: string = process.env.RABBIT_URL || 'url';
  private static listenConnectionEvents = (): Promise<Connection> => {
    return new Promise((resolve, reject) => {
      if (!Broker.connection) {
        Broker.urli ? Broker.start() : reject();
        reject();
      }
      resolve(
        Broker.connection.on('error', (err: Error) => {
          logger.error(err.message);
          setTimeout(Broker.start, 10000);
        }) &&
          Broker.connection.on('close', (err: Error) => {
            if (err) {
              logger.error('connection closed because err!');
              setTimeout(Broker.start, 10000);
            }
            logger.info('connection to RabbitQM closed!');
          })
      );
    });
  };
  private static connectRabbitMQ = () => {
    return new Promise<Connection>((resolve, reject) => {
      if (Broker.connection || !Broker.urli) {
        const message = !Broker.urli
          ? 'The host of rabbitmq was not found in the environment variables'
          : 'Connection has already been established';
        logger.info(message);
        reject(new Error(message));
      }
      retry<Connection>(() => connect(Broker.urli), 10, 1000)
        .then((conn) => {
          Broker.connection = conn;
          resolve(Broker.listenConnectionEvents());
        })
        .catch((err) => reject(new Error(err)));
    });
  };
  static start = () => {
    Broker.connectRabbitMQ().catch((error: Error) => {
      throw error;
    });
  };
}
export let ConnectionMQ: Connection = Broker.connection;
export let StartMQ = () => {
  return Broker.start();
};
import { ConnectionMQ } from './broker';
export class BrokerChannel {
  static Connection: ConnectionMQ = ConnectionMQ
  static queues: IQueue = undefined;
  static exchanges: IExchanges = undefined;
  static channel: Channel = undefined;
  static binds: IBinds = undefined;
  static getChannel = () => {
    return new Promise<Channel>((resolve, reject) => {
      if (BrokerChannel.channel) reject();
      Connection.createChannel().then((ch) => {
        BrokerChannel.channel = ch;
        resolve(BrokerChannel.channel);
      });
    });
  };
}
但要实现这一点,我需要将所有函数声明为静态函数,而且我不能有构造函数,也不能有依赖项注入 我想知道使用这个是否可行? 或者在我的用例中,最好创建一个主类的单例

像这样:

const server = Broker.getInstance();
server.setChannel(new BrokerChannel(server.getConnection()))

server.channel.subscribeQueue = BrokerChannel.subscribeQueue({ queue_name: 'A' });
我想知道哪一个更干净更适合我的用例

我觉得第二个选项可以注入依赖项,这在第一个选项中是不可能的

const server = Broker.getInstance();
server.setChannel(new BrokerChannel(server.getConnection()))

server.channel.subscribeQueue = BrokerChannel.subscribeQueue({ queue_name: 'A' });