export default class NotificationCenter {
  private eventTarget: cc.EventTarget = new cc.EventTarget();

  private static instance: NotificationCenter = null;
  public static get Instance(): NotificationCenter {
    if (this.instance == null) {
      this.instance = new NotificationCenter();
    }
    return this.instance;
  }

  /**
   * Listen to a notification
   * @param name
   * @param callback
   */
  public on(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
    this.eventTarget.on(type, callback, target);
  }

  /**
   * Dispatch a notification
   * @param name
   */
  public dispatch(type: string, data?: T) {
    this.eventTarget.emit(type, type, data);
  }

  public hasEventListener($type): boolean {
    return this.eventTarget != null && this.eventTarget.hasEventListener($type);
  }

  /**
   * Cancel listen
   * @param name
   */
  public off(type: string, callback: ($type: string, $data: T) => void, target?: any): void {
    this.eventTarget.off(type, callback, target);
  }
}