File

projects/wms-framework/src/lib/utils/SubscriptionEvent.ts

Extends

SubscriptionEvent

Index

Properties
Methods

Constructor

constructor(addAccessor: (value: T) => void, removeAccessor: (value: T) => void)
Parameters :
Name Type Optional
addAccessor function No
removeAccessor function No

Properties

Public disabled
Type : boolean
Default value : false
Inherited from SubscriptionEvent
handlers
Type : []
Default value : []
Inherited from SubscriptionEvent

Methods

Public addHandler
addHandler(handler: T, self: any)
Inherited from SubscriptionEvent
Parameters :
Name Type Optional Default value
handler T No
self any No null
Returns : T
Public removeHandler
removeHandler(handler: T, self: any)
Inherited from SubscriptionEvent
Parameters :
Name Type Optional Default value
handler T No
self any No null
Returns : void
Public fire
fire(args: any[])
Inherited from SubscriptionEvent

Fires the event and executes each handler

Parameters :
Name Type Optional
args any[] No
Returns : void
Public hasHandlers
hasHandlers()
Inherited from SubscriptionEvent
Returns : boolean
Public insertHandler
insertHandler(index: number, handler: T, self: any)
Inherited from SubscriptionEvent

Inserts handler to the event at the position specified. Allows to add priority to a handler at handlers execution.

Parameters :
Name Type Optional Default value
index number No
handler T No
self any No null
Returns : T
Public removeAllHandlers
removeAllHandlers()
Inherited from SubscriptionEvent

Removes all handlers from the event

Returns : void
Public toFunction
toFunction()
Inherited from SubscriptionEvent
Returns : T
export class SubscriptionEvent<T> {
  public disabled: boolean = false;

  public toFunction(): T {
    return <T>(<any>((...args: any[]) => this.fire(args)));
  }
  handlers: [T, any][] = [];

  /**
   * Add handler to the event
   *
   * @param {T} handler
   * @param {*} [self=null]
   * @returns {T}
   * @memberof SubscriptionEvent
   */
  public addHandler(handler: T, self: any = null): T {
    this.handlers.push([handler, self]);
    return handler;
  }

  /**
   * Inserts handler to the event at the position specified.
   * Allows to add priority to a handler at handlers execution.
   *
   * @param {T} handler
   * @param {*} [self=null]
   * @returns {T}
   * @memberof SubscriptionEvent
   */
  public insertHandler(index: number, handler: T, self: any = null): T {
    this.handlers.splice(index, 0, [handler, self]);
    return handler;
  }

  /**
   * Removes handler from the event
   *
   * @param {T} handler
   * @param {*} [self=null]
   * @memberof SubscriptionEvent
   */
  public removeHandler(handler: T, self: any = null): void {
    if (handler) {
      let idx = this.handlers.findIndex((h) => h[0] == handler && h[1] == self);
      // Final search for lambda expression using toString()
      idx =
        idx > 0
          ? idx
          : this.handlers.findIndex(
              (h) => h[0].toString() == handler.toString() && h[1] == self
            );
      if (idx >= 0) {
        this.handlers.splice(idx, 1);
      }
    }
  }

  /**
   * Removes all handlers from the event
   *
   * @memberof SubscriptionEvent
   */
  public removeAllHandlers(): void {
    if (this.hasHandlers()) {
      this.handlers.splice(0, this.handlers.length);
    }
  }

  /**
   * Fires the event and executes each handler
   *
   * @param {any[]} args
   * @memberof SubscriptionEvent
   */
  public fire(args: any[]): void {
    if (!this.disabled) {
      //Map will create a copy to be immune of changes in this.handlers like calls to removeHandler, if no copy of the collection is created
      //changes in this.handlers tend to skip items in the loop
      for (let handler of this.handlers.map((x) => {
        return x;
      })) {
        //The handler to be invoked might be removed by code, so we have to double check
        const idx = this.handlers.findIndex((h) => h == handler);
        if (idx >= 0 && handler[0] instanceof Function) {
          handler[0].apply(handler[1] ?? window, args);
        }
      }
    }
  }
  public hasHandlers(): boolean {
    return this.handlers.length != 0;
  }
}

export class CustomSubscriptionEvent<T> extends SubscriptionEvent<T> {
  constructor(
    private addAccessor: (value: T) => void,
    private removeAccessor: (value: T) => void
  ) {
    super();
  }
  public addHandler(handler: T, self: any = null): T {
    this.addAccessor(handler);
    super.addHandler(handler, self);
    return handler;
  }
  public removeHandler(handler: T, self: any = null): void {
    this.removeAccessor(handler);
    super.removeHandler(handler, self);
  }
}

result-matching ""

    No results matching ""