All files / src timed-emitter.ts

21.42% Statements 3/14
0% Branches 0/2
0% Functions 0/3
23.07% Lines 3/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x                               1x                                                                            
import {EventEmitter} from "events";
import {Logger} from "./logger";
 
/**
 * Setup a blocking wait.  Used in B(locking)* commands, such as [[BRPOP]][[BRPopCommand]].
 *
 * Unlike redis, which allows an infinite wait when timoutSeconds is ZERO, **unit-redis-ness**
 * allows only for a maximum of about 24.85 days
 *
 * @param timeoutSeconds Number of seconds to wait
 * @param keyEventNames Array of events to wait for
 * @param emitter The serverContext is the emitter
 * @returns TimedEmitter EventEmitter
 *
 * ### Events: timout
 */
 
export class TimedEmitter extends EventEmitter {
  public callback: any = null;
 
  private logger: Logger = new Logger(module.id);
 
  constructor(private timeoutSeconds: number, private keyEventNames: string[], emitter: EventEmitter) {
      super();
      setTimeout(
          () => {
              this.emit(
                  "timeout",
                  keyEventNames
              );
              // This is 24.855134803 days
          },
          timeoutSeconds === 0
              // eslint-disable-next-line no-magic-numbers
              ? 2147483647
              // eslint-disable-next-line no-magic-numbers
              : timeoutSeconds * 1000
      );
      for (const eventName of keyEventNames) {
          emitter.on(
              eventName,
              (data: any) => {
                  this.logger.debug(
                      `Triggered channel "${eventName}" with %j`,
                      data
                  );
                  this.emit(
                      eventName,
                      data
                  );
              }
          );
      }
  }
}