<commit_before>import { MessageBus, Events, Parameters } from 'protocol';

// Todo: check if this is used anywhere? If not decide whether or not to keep it.
export type Callback = (
  message: any,
  sender: chrome.runtime.MessageSender,
  sendResponse: (response?: any) => void
) => void;

interface ChromeMessage<T, K extends keyof T> {
  topic: K;
  args: Parameters<T[K]>;
}

type AnyEventCallback<Ev> = <E extends keyof Ev>(topic: E, args: Parameters<Ev[E]>) => void;

export class ChromeMessageBus extends MessageBus<Events> {
  private _disconnected = false;
  private _listeners: any[] = [];

  constructor(private _port: chrome.runtime.Port) {
    super();

    _port.onDisconnect.addListener(() => {
      console.log('Disconnected the port');
      this._disconnected = true;
    });
  }

  onAny(cb: AnyEventCallback<Events>): () => void {
    const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
      console.log('Received message', msg);
      cb(msg.topic, msg.args);
    };
    this._port.onMessage.addListener(listener);
    this._listeners.push(listener);
    return () => {
      this._listeners.splice(this._listeners.indexOf(listener), 1);
      this._port.onMessage.removeListener(listener);
    };
  }

  on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
    const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
      console.log('Received message', msg);
      if (msg.topic === topic) {
        cb.apply(null, msg.args);
      }
    };
    this._port.onMessage.addListener(listener);
    this._listeners.push(listener);
    return () => {
      this._listeners.splice(this._listeners.indexOf(listener), 1);
      this._port.onMessage.removeListener(listener);
    };
  }

  once<E extends keyof Events>(topic: E, cb: Events[E]): void {
    const listener = (msg: ChromeMessage<Events, keyof Events>) => {
      if (msg.topic === topic) {
        cb.apply(null, msg.args);
        this._port.onMessage.removeListener(listener);
      }
    };
    this._port.onMessage.addListener(listener);
  }

  emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): void {
    console.log('@@ Sending message', topic, args);
    if (this._disconnected) {
      return;
    }
    this._port.postMessage({
      topic,
      args,
    });
  }

  destroy(): void {
    this._listeners.forEach(l => window.removeEventListener('message', l));
    this._listeners = [];
  }
}
<commit_msg>refactor(devtools): remove unused type<commit_after>import { MessageBus, Events, Parameters } from 'protocol';

interface ChromeMessage<T, K extends keyof T> {
  topic: K;
  args: Parameters<T[K]>;
}

type AnyEventCallback<Ev> = <E extends keyof Ev>(topic: E, args: Parameters<Ev[E]>) => void;

export class ChromeMessageBus extends MessageBus<Events> {
  private _disconnected = false;
  private _listeners: any[] = [];

  constructor(private _port: chrome.runtime.Port) {
    super();

    _port.onDisconnect.addListener(() => {
      console.log('Disconnected the port');
      this._disconnected = true;
    });
  }

  onAny(cb: AnyEventCallback<Events>): () => void {
    const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
      console.log('Received message', msg);
      cb(msg.topic, msg.args);
    };
    this._port.onMessage.addListener(listener);
    this._listeners.push(listener);
    return () => {
      this._listeners.splice(this._listeners.indexOf(listener), 1);
      this._port.onMessage.removeListener(listener);
    };
  }

  on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
    const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
      console.log('Received message', msg);
      if (msg.topic === topic) {
        cb.apply(null, msg.args);
      }
    };
    this._port.onMessage.addListener(listener);
    this._listeners.push(listener);
    return () => {
      this._listeners.splice(this._listeners.indexOf(listener), 1);
      this._port.onMessage.removeListener(listener);
    };
  }

  once<E extends keyof Events>(topic: E, cb: Events[E]): void {
    const listener = (msg: ChromeMessage<Events, keyof Events>) => {
      if (msg.topic === topic) {
        cb.apply(null, msg.args);
        this._port.onMessage.removeListener(listener);
      }
    };
    this._port.onMessage.addListener(listener);
  }

  emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): void {
    console.log('@@ Sending message', topic, args);
    if (this._disconnected) {
      return;
    }
    this._port.postMessage({
      topic,
      args,
    });
  }

  destroy(): void {
    this._listeners.forEach(l => window.removeEventListener('message', l));
    this._listeners = [];
  }
}

{'fraction_non_alphanumeric': 0.15451278799408158, 'fraction_numerical': 0.0008454872120059184, 'mean_word_length': 3.3254113345521024, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 53, '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': '2762310', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1464, 'n_words': 317}