<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){
      executeHandler(() => this.callback.call(null, message));
    }
  }
}

function executeHandler(handler) {
  try {
    handler();
  } catch(e) {
    logger.error(e);
  }
}

export class EventAggregator {
  constructor(){
    this.eventLookup = {};
    this.messageHandlers = [];
  }

  publish(event: string | any, data?: any){
    var subscribers, i;

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

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

      while(i--) {
        subscribers[i].handle(event);
      }
    }
  }

  subscribe(event, callback){
    var subscribers, handler;

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

      subscribers.push(callback);

      return function(){
        var idx = subscribers.indexOf(callback);
        if (idx != -1) {
          subscribers.splice(idx, 1);
        }
      };
    }else{
      handler = new Handler(event, callback);
      subscribers = this.messageHandlers;

      subscribers.push(handler);

      return function(){
        var idx = subscribers.indexOf(handler);
        if (idx != -1) {
          subscribers.splice(idx, 1);
        }
      };
    }
  }

  subscribeOnce(event, callback){
    var sub = this.subscribe(event,function(data,event){
      sub();
      return callback(data,event);
    });
    return sub;
  }
}

export function includeEventsIn(obj){
  var ea = new EventAggregator();

  obj.subscribeOnce = function(event, callback){
    return ea.subscribeOnce(event, callback);
  };

  obj.subscribe = function(event, callback){
    return ea.subscribe(event, callback);
  };

  obj.publish = function(event, data){
    ea.publish(event, data);
  };

  return ea;
}

export function configure(aurelia){
  aurelia.withInstance(EventAggregator, includeEventsIn(aurelia));
}<commit_msg>fix(index): update to latest framework configuration api<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){
      executeHandler(() => this.callback.call(null, message));
    }
  }
}

function executeHandler(handler) {
  try {
    handler();
  } catch(e) {
    logger.error(e);
  }
}

export class EventAggregator {
  constructor(){
    this.eventLookup = {};
    this.messageHandlers = [];
  }

  publish(event: string | any, data?: any){
    var subscribers, i;

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

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

      while(i--) {
        subscribers[i].handle(event);
      }
    }
  }

  subscribe(event, callback){
    var subscribers, handler;

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

      subscribers.push(callback);

      return function(){
        var idx = subscribers.indexOf(callback);
        if (idx != -1) {
          subscribers.splice(idx, 1);
        }
      };
    }else{
      handler = new Handler(event, callback);
      subscribers = this.messageHandlers;

      subscribers.push(handler);

      return function(){
        var idx = subscribers.indexOf(handler);
        if (idx != -1) {
          subscribers.splice(idx, 1);
        }
      };
    }
  }

  subscribeOnce(event, callback){
    var sub = this.subscribe(event,function(data,event){
      sub();
      return callback(data,event);
    });
    return sub;
  }
}

export function includeEventsIn(obj){
  var ea = new EventAggregator();

  obj.subscribeOnce = function(event, callback){
    return ea.subscribeOnce(event, callback);
  };

  obj.subscribe = function(event, callback){
    return ea.subscribe(event, callback);
  };

  obj.publish = function(event, data){
    ea.publish(event, data);
  };

  return ea;
}

export function configure(config){
  config.instance(EventAggregator, includeEventsIn(config.aurelia));
}
{'fraction_non_alphanumeric': 0.13859362224039248, 'fraction_numerical': 0.001635322976287817, 'mean_word_length': 2.775462962962963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1719354', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1467, 'n_words': 296}