File

projects/wms-framework/src/lib/diagnostics/Debugger.ts

Description

Interface for a function callback to be use when Throw function is called

Index

Properties

Properties

Signature : [exception: string | Error]
Returns : void
export interface ThrowCallbackFunction {
  (exception: string | Error): void;
}

/**
 * Framework static class with debugging functions to be used by the framework or external libraries
 * during stabilization
 *
 * @export
 * @class Debugger
 */
// @dynamic
export class Debugger {
  /**
   * Flag to enable or not the throwing or excepton. Default value = true
   *
   * @static
   * @type {boolean}
   * @memberof Debugger
   */
  public static EnabledThrowExceptions: boolean = false;

  /**
   * Flag to enable the functionality of RegisterUse function. Default value = false
   *
   * @static
   * @type {boolean}
   * @memberof Debugger
   */
  private static __enabledRegisterUse: boolean = false;
  private static __registerUsesPlainForm: boolean = true;
  private static __registryOfUses: any = {};
  private static __globalDebuggerName = '__wmsDebugger';

  /**
   * To enable/disable the registration of uses
   *
   * @static
   * @type {boolean}
   * @memberof Debugger
   */
  public static get EnabledRegisterUse(): boolean {
    return this.__enabledRegisterUse;
  }

  public static set EnabledRegisterUse(value: boolean) {
    this.__enabledRegisterUse = value;
    this.Clear();
    this.checkGlobalInstance();
  }

  /**
   * Changes how the information is stored, nested or as a plain object, changing
   * this value will clean the current statistics
   *
   * @static
   * @type {boolean}
   * @memberof Debugger
   */
  public static get RegisterUsesPlainForm(): boolean {
    return this.__registerUsesPlainForm;
  }
  public static set RegisterUsesPlainForm(value: boolean) {
    this.__registerUsesPlainForm = value;
    this.Clear();
  }

  /**
   * Returns a copy of the registry of uses that has been stored so far
   *
   * @readonly
   * @static
   * @type {*}
   * @memberof Debugger
   */
  public static get RegistryOfUses(): any {
    this.__registryOfUses = this.__registryOfUses || {};
    return JSON.parse(JSON.stringify(this.__registryOfUses));
  }

  /**
   * A function callback to call when a Throw is called
   *
   * @static
   * @type {ThrowCallbackFunction}
   * @memberof Debugger
   */
  public static ThrowCallbackFunction: ThrowCallbackFunction = null;

  /**
   * Function to throw exceptions (string  | Error), the flag 'EnabledThrowExceptions' can turn off the functionality
   * for debugging purposes in runtime
   *
   * @static
   * @param {(string | Error)} exception
   * @memberof Debugger
   */
  public static Throw(exception: string | Error): void {
    if (this.ThrowCallbackFunction) {
      try {
        this.ThrowCallbackFunction(exception);
      } catch (ex) {}
    }
    if (this.EnabledThrowExceptions) {
      if (exception instanceof Error) throw exception;
      else throw new Error(exception);
    }
  }

  /**
   * Register an use of a stub or anything, syntax expected is [A | A.B | A.B.C...]
   *
   * @static
   * @param {string} itemName
   * @return {*}  {void}
   * @memberof Debugger
   */
  public static RegisterUse(itemName: string): void {
    try {
      if (
        !this.EnabledRegisterUse ||
        itemName === null ||
        itemName === undefined
      )
        return;

      let __uses = this.__registryOfUses;

      if (!this.RegisterUsesPlainForm) {
        let itemsParts: string[] = itemName.split('.');
        while (itemsParts && itemsParts.length > 0) {
          let key: string = this.sanitizeKey(itemsParts.shift());
          if (itemsParts.length > 0) {
            __uses = __uses[key] = __uses[key] || {};
          } else {
            __uses[key] = !__uses[key] ? 1 : __uses[key] + 1;
          }
        }
      } else {
        let key: string = this.sanitizeKey(itemName);
        __uses[itemName] = !__uses[itemName] ? 1 : __uses[itemName] + 1;
      }
    } catch (ex) {
      console.warn(ex);
    }
  }

  /**
   * Clear the statistics
   *
   * @static
   * @memberof Debugger
   */
  public static Clear(): void {
    try {
      this.__registryOfUses = {};
    } catch (ex) {
      console.warn(ex);
    }
  }

  /**
   * Some key names are not allowed
   *
   * @private
   * @static
   * @param {string} key
   * @return {*}  {string}
   * @memberof Debugger
   */
  private static sanitizeKey(key: string): string {
    if (key === 'constructor') key = '__constructor';
    return key;
  }

  /**
   * Makes sure there is a global reference to this class in window top most object in case EnabledRegisterUse is true
   *
   * @private
   * @static
   * @memberof Debugger
   */
  private static checkGlobalInstance(): void {
    try {
      let topWindow = this.getTopWindow();

      if (this.EnabledRegisterUse && !topWindow[this.__globalDebuggerName]) {
        topWindow[this.__globalDebuggerName] = this;
      } else if (
        !this.EnabledRegisterUse &&
        topWindow[this.__globalDebuggerName]
      ) {
        delete topWindow[this.__globalDebuggerName];
      }
    } catch (ex) {
      console.warn(ex);
    }
  }

  /**
   * Get the top most window of the page
   *
   * @private
   * @static
   * @return {*}  {*}
   * @memberof StubsDebug
   */
  private static getTopWindow(): any {
    let topWindow: any = window;
    while (topWindow.parent && topWindow.parent != topWindow) {
      topWindow = topWindow.parent;
    }
    return topWindow;
  }
}

//To force the global registration, uncomment when needs to enable the registration
//Debugger.EnabledRegisterUse = true;

result-matching ""

    No results matching ""