File

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

Description

Copyright (C) Mobilize.Net info@mobilize.net - All Rights Reserved

This file is part of the Mobilize Frameworks, which is proprietary and confidential.

NOTICE: All information contained herein is, and remains the property of Mobilize.Net Corporation. The intellectual and technical concepts contained herein are proprietary to Mobilize.Net Corporation and may be covered by U.S. Patents, and are protected by trade secret or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden unless prior written permission is obtained from Mobilize.Net Corporation.

Index

Properties
Methods

Properties

Public disabled
Type : boolean
Default value : false
handlers
Type : []
Default value : []

Methods

Public addHandler
addHandler(handler: T, self: any)

Add handler to the event

Parameters :
Name Type Optional Default value
handler T No
self any No null
Returns : T
Public fire
fire(args: any[])

Fires the event and executes each handler

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

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()

Removes all handlers from the event

Returns : void
Public removeHandler
removeHandler(handler: T, self: any)

Removes handler from the event

Parameters :
Name Type Optional Default value
handler T No
self any No null
Returns : void
Public toFunction
toFunction()
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 ""