<commit_before>import * as LogManager from 'aurelia-logging';

const logger = LogManager.getLogger('event-aggregator');

class Handler {
  constructor(messageType, callback) {
    this.messageType = messageType;
    this.callback = callback;
  }

  handle(message) {
    if (message instanceof this.messageType) {
      this.callback.call(null, message);
    }
  }
}

/**
* Represents a disposable subsciption to an EventAggregator event.
*/
interface Subscription {
  /**
  * Disposes the subscription.
  */
  dispose(): void;
}

/**
* Enables loosely coupled publish/subscribe messaging.
*/
export class EventAggregator {
  /**
  * Creates an instance of the EventAggregator class.
  */
  constructor() {
    this.eventLookup = {};
    this.messageHandlers = [];
  }

  /**
  * Publishes a message.
  * @param event The event or channel to publish to.
  * @param data The data to publish on the channel.
  */
  publish(event: string | any, data?: any): void {
    let subscribers;
    let i;

    if (!event) {
      throw new Error('Event was invalid.');
    }

    if (typeof event === 'string') {
      subscribers = this.eventLookup[event];
      if (subscribers) {
        subscribers = subscribers.slice();
        i = subscribers.length;

        while (i--) {
          try {
            subscribers[i](data, event);
          } catch (e) {
            logger.error(e);
          }
        }
      }
    } else {
      subscribers = this.messageHandlers.slice();
      i = subscribers.length;

      while (i--) {
        try {
          subscribers[i].handle(event);
        } catch (e) {
          logger.error(e);
        }
      }
    }
  }

  /**
  * Subscribes to a message channel or message type.<commit_msg>isolate try/catch code in separate function<commit_after>import * as LogManager from 'aurelia-logging';

const logger = LogManager.getLogger('event-aggregator');

class Handler {
  constructor(messageType, callback) {
    this.messageType = messageType;
    this.callback = callback;
  }

  handle(message) {
    if (message instanceof this.messageType) {
      this.callback.call(null, message);
    }
  }
}

function invokeCallback(callback, data, event) {
  try {
    callback(data, event);
  } catch (e) {
    logger.error(e);
  }
}

function invokeHandler(handler, data) {
  try {
    handler.handle(data);
  } catch (e) {
    logger.error(e);
  }
}

/**
* Represents a disposable subsciption to an EventAggregator event.
*/
interface Subscription {
  /**
  * Disposes the subscription.
  */
  dispose(): void;
}

/**
* Enables loosely coupled publish/subscribe messaging.
*/
export class EventAggregator {
  /**
  * Creates an instance of the EventAggregator class.
  */
  constructor() {
    this.eventLookup = {};
    this.messageHandlers = [];
  }

  /**
  * Publishes a message.
  * @param event The event or channel to publish to.
  * @param data The data to publish on the channel.
  */
  publish(event: string | any, data?: any): void {
    let subscribers;
    let i;

    if (!event) {
      throw new Error('Event was invalid.');
    }

    if (typeof event === 'string') {
      subscribers = this.eventLookup[event];
      if (subscribers) {
        subscribers = subscribers.slice();
        i = subscribers.length;

        while (i--) {
          invokeCallback(subscribers[i], data, event);
        }
      }
    } else {
      subscribers = this.messageHandlers.slice();
      i = subscribers.length;

      while (i--) {
        invokeHandler(subscribers[i], event);
      }
    }
  }

  /**
  * Subscribes to a message channel or message type.
{'fraction_non_alphanumeric': 0.12604340567612687, 'fraction_numerical': 0.0, 'mean_word_length': 2.433619866284623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4572749', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1087, 'n_words': 284}