<commit_before>'use strict';

export interface ISubscriptionAction {
  (args: any[]): void;
}

interface ISubscriptionCallback {
  action: ISubscriptionAction;
  thisArg?: any;
}

export interface ISubscriptionHandle {
  id: number;
  key: string;
}

interface ISubscription {
  callback: ISubscriptionCallback;
  handle: ISubscriptionHandle;
}

class SubscriptionList {
  private currentHandle = 0;
  private list: ISubscription[] = [];

  public add(key: string, callback: ISubscriptionCallback) {
    let handle = <ISubscriptionHandle>{ id: ++this.currentHandle, key };

    this.list.push(<ISubscription>{ callback, handle })

    return handle;
  }

  public remove(handle: ISubscriptionHandle) {
    let index = -1;
    this.list.some((x, i) => x.handle.id === handle.id ? ((index = i), true) : false );
    if (index >= 0) {
      this.list.splice(index, 1);
    }
  }

  public invoke(args: any[]) {
    if (this.list.length > 0) {
      this.list.forEach(x => x.callback.action.apply(x.callback.thisArg, [args]))
    }
  }
}

interface IPubSubMap {
  [key: string]: SubscriptionList;
}

export class PubSub {
  private map = <IPubSubMap>{};

  private getList(key: string, action: (list: SubscriptionList) => void, createMissing = false) {
    let list = this.map[key];

    if (list == null && createMissing) {
      list = new SubscriptionList();
      this.map[key] = list;
    }

    if (list != null) {
      action(list);
    }
  }

  public subscribe(key: string, action: ISubscriptionAction, thisArg?: any) {
    let handle: ISubscriptionHandle;

    this.getList(key, x => { handle = x.add(key, <ISubscriptionCallback>{action, thisArg}) }, true);

    return handle;
  }

  public unsubscribe(handle: ISubscriptionHandle) {
    this.getList(handle.key, x => x.remove(handle));
  }

  public publish(key: string, ...args: any[]) {
    this.getList(key, x => x.invoke(args));
  }
}

export let Default = new PubSub();

export default Default;<commit_msg>pubsub's unsubscribe now returns null (for one liners)<commit_after>'use strict';

export interface ISubscriptionAction {
  (args: any[]): void;
}

interface ISubscriptionCallback {
  action: ISubscriptionAction;
  thisArg?: any;
}

export interface ISubscriptionHandle {
  id: number;
  key: string;
}

interface ISubscription {
  callback: ISubscriptionCallback;
  handle: ISubscriptionHandle;
}

class SubscriptionList {
  private currentHandle = 0;
  private list: ISubscription[] = [];

  public add(key: string, callback: ISubscriptionCallback) {
    let handle = <ISubscriptionHandle>{ id: ++this.currentHandle, key };

    this.list.push(<ISubscription>{ callback, handle })

    return handle;
  }

  public remove(handle: ISubscriptionHandle) {
    let index = -1;
    this.list.some((x, i) => x.handle.id === handle.id ? ((index = i), true) : false );
    if (index >= 0) {
      this.list.splice(index, 1);
    }
  }

  public invoke(args: any[]) {
    if (this.list.length > 0) {
      this.list.forEach(x => x.callback.action.apply(x.callback.thisArg, [args]))
    }
  }
}

interface IPubSubMap {
  [key: string]: SubscriptionList;
}

export class PubSub {
  private map = <IPubSubMap>{};

  private getList(key: string, action: (list: SubscriptionList) => void, createMissing = false) {
    let list = this.map[key];

    if (list == null && createMissing) {
      list = new SubscriptionList();
      this.map[key] = list;
    }

    if (list != null) {
      action(list);
    }
  }

  public subscribe(key: string, action: ISubscriptionAction, thisArg?: any) {
    let handle: ISubscriptionHandle;

    this.getList(key, x => { handle = x.add(key, <ISubscriptionCallback>{action, thisArg}) }, true);

    return handle;
  }

  public unsubscribe(handle: ISubscriptionHandle): ISubscriptionHandle {
    this.getList(handle.key, x => x.remove(handle));

    return null;
  }

  public publish(key: string, ...args: any[]) {
    this.getList(key, x => x.invoke(args));
  }
}

export let Default = new PubSub();

export default Default;
{'fraction_non_alphanumeric': 0.14203980099502486, 'fraction_numerical': 0.0024875621890547263, 'mean_word_length': 3.558956916099773, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, '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': '2715289', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1290, 'n_words': 321}