All files / src/server session.ts

70% Statements 28/40
50% Branches 6/12
61.53% Functions 8/13
70% Lines 28/40

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 1x                 1x 5x   5x   5x   5x                                                                                                                     1x       3x       5x     5x         3x 3x 3x 3x             2x 2x 2x   2x       102x 82x 82x     20x 20x       5x       5x 5x 5x      
import {Dictionary} from "../dictionary";
import {Logger} from "../logger";
import {IRespCommand} from "../resp/command/resp-command";
import {RedisToken} from "../resp/protocol/redis-token";
import {TimedEmitter} from "../timed-emitter";
import {IRequest} from "./request";
export interface ICmdReq {
  command: IRespCommand;
  request: IRequest;
}
export abstract class Session {
  protected logger: Logger = new Logger(module.id);
 
  protected commands?: ICmdReq[] = [];
 
  private subscriptions: Dictionary<string, TimedEmitter> = new Dictionary();
 
  private errored: boolean = false;
 
  public abstract getId(): string;
 
  public abstract getName(): string;
 
  public abstract setName(name: string): void;
 
  public abstract publish(message: RedisToken): void;
 
  public abstract close(): void;
 
  public abstract getValue(key: string): any;
 
  public abstract putValue(key: string, value: any): void;
 
  public abstract removeValue(key: string): any;
 
  public abstract getAddress(): string;
 
  public abstract getCurrentDb(): number;
 
  public abstract setCurrentDb(db: number): void;
 
  public abstract getLastCommand(): string;
 
  public abstract setLastCommand(command: string): void;
 
  public isSubscribed(channel: string): boolean {
      return this.subscriptions.exists(channel);
  }
 
  public subscribe(channel: string, emitter: TimedEmitter): void {
      this.subscriptions.put(
          channel,
          emitter
      );
  }
 
  public getSubscription(channel: string): TimedEmitter {
      return this.subscriptions.get(channel);
  }
 
  public unSubscribe(channel: string): void {
      if (this.isSubscribed(channel)) {
          const emitter: TimedEmitter = this.subscriptions.get(channel);
          emitter.off(
              channel,
              emitter.callback
          );
          this.subscriptions.remove(channel);
      }
  }
 
  public getSubscriptionNames(): string[] {
      return this.subscriptions.keys();
  }
 
  public setError(): void {
      this.errored = true;
  }
 
  public isErrored(): boolean {
      return this.errored;
  }
 
  public queueRequest(cmdReq: ICmdReq): void {
      Iif (!this.commands) {
          this.commands = [cmdReq];
      } else {
          this.commands.push(cmdReq);
      }
  }
 
  public abortTransaction(): boolean {
      Eif (this.inTransaction()) {
          delete this.commands;
          this.errored = false;
          return true;
      }
 
      return false;
  }
 
  public getTransaction(): ICmdReq[] {
      const retVal: ICmdReq[] = this.commands as ICmdReq[];
      Eif (this.commands) {
          delete this.commands;
      }
      return retVal;
  }
 
  public inTransaction(): boolean {
      if (this.commands === undefined) {
          this.logger.debug(`inTransaction(FALSE) = ${this.commands !== undefined}`);
          return false;
      }
 
      this.logger.debug(`inTransaction(TRUE) = ${this.commands !== undefined}`);
      return true;
  }
 
  public startTransaction(): boolean {
      Iif (this.inTransaction()) {
          delete this.commands;
          return false;
      }
      this.commands = [];
      this.errored = false;
      return true;
  }
}