File

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

Description

Framework static class with debugging functions to be used by the framework or external libraries during stabilization

Index

Properties
Methods
Accessors

Properties

Private Static __enabledRegisterUse
Type : boolean
Default value : false

Flag to enable the functionality of RegisterUse function. Default value = false

Private Static __globalDebuggerName
Type : string
Default value : '__wmsDebugger'
Private Static __registerUsesPlainForm
Type : boolean
Default value : true
Private Static __registryOfUses
Type : any
Default value : {}
Static EnabledThrowExceptions
Type : boolean
Default value : false

Flag to enable or not the throwing or excepton. Default value = true

Static ThrowCallbackFunction
Type : ThrowCallbackFunction
Default value : null

A function callback to call when a Throw is called

Methods

Private Static checkGlobalInstance
checkGlobalInstance()

Makes sure there is a global reference to this class in window top most object in case EnabledRegisterUse is true

Returns : void
Static Clear
Clear()

Clear the statistics

Returns : void
Private Static getTopWindow
getTopWindow()

Get the top most window of the page

Returns : any

{*}

Static RegisterUse
RegisterUse(itemName: string)

Register an use of a stub or anything, syntax expected is [A | A.B | A.B.C...]

Parameters :
Name Type Optional
itemName string No
Returns : void

{void}

Private Static sanitizeKey
sanitizeKey(key: string)

Some key names are not allowed

Parameters :
Name Type Optional
key string No
Returns : string

{string}

Static Throw
Throw(exception: string | Error)

Function to throw exceptions (string | Error), the flag 'EnabledThrowExceptions' can turn off the functionality for debugging purposes in runtime

Parameters :
Name Type Optional
exception string | Error No
Returns : void

Accessors

EnabledRegisterUse
getEnabledRegisterUse()

To enable/disable the registration of uses

Returns : boolean
setEnabledRegisterUse(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
RegisterUsesPlainForm
getRegisterUsesPlainForm()

Changes how the information is stored, nested or as a plain object, changing this value will clean the current statistics

Returns : boolean
setRegisterUsesPlainForm(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
RegistryOfUses
getRegistryOfUses()

Returns a copy of the registry of uses that has been stored so far

Returns : any
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 ""