<commit_before>import { Deferred } from "./Deferred"

export type Parser<T> = (data: string) => T

export class Task<T> {
  retries = 0
  private readonly d = new Deferred<T>()
  /**
   * @param {string} command is the value written to stdin to perform the given
   * task.
   * @param {Parser<T>} parser is used to parse resulting data from the
   * underlying process to a typed object.
   */
  constructor(readonly command: string, readonly parser: Parser<T>) { }

  /**
   * @return the resolution or rejection of this task.
   */
  get promise(): Promise<T> {
    return this.d.promise
  }

  get pending(): boolean {
    return this.d.pending
  }

  toString() {
    return this.constructor.name + "(" + this.command + ")"
  }

  /**
   * This is for use by `BatchProcess` only, and will only be called when the
   * process is complete for this task's command
   */
  onData(data: string): void {
    try {
      this.d.resolve(this.parser(data))
    } catch (error) {
      this.d.reject(error)
    }
  }

  /**
   * This is for use by `BatchProcess` only, and will only be called when the
   * process has errored after N retries
   */
  onError(error: any): void {
    this.d.reject(error)
  }
}<commit_msg>use logger onData and onError<commit_after>import { Deferred } from "./Deferred"
import { logger } from "./Logger"

export type Parser<T> = (data: string) => T

export class Task<T> {
  retries = 0
  private readonly d = new Deferred<T>()
  /**
   * @param {string} command is the value written to stdin to perform the given
   * task.
   * @param {Parser<T>} parser is used to parse resulting data from the
   * underlying process to a typed object.
   */
  constructor(readonly command: string, readonly parser: Parser<T>) {}

  /**
   * @return the resolution or rejection of this task.
   */
  get promise(): Promise<T> {
    return this.d.promise
  }

  get pending(): boolean {
    return this.d.pending
  }

  toString() {
    return this.constructor.name + "(" + this.command + ")"
  }

  /**
   * This is for use by `BatchProcess` only, and will only be called when the
   * process is complete for this task's command
   */
  onData(data: string): void {
    try {
      const result = this.parser(data)
      logger().debug("Task.onData(): resolved", { result, command: this.command, data })
      this.d.resolve(result)
    } catch (error) {
      logger().warn("Task.onData(): parser raised " + error, { command: this.command, data })
      this.d.reject(error)
    }
  }

  /**
   * This is for use by `BatchProcess` only, and will only be called when the
   * process has errored after N retries
   */
  onError(error: any): void {
    logger().warn("Task.onError(): Failed to run " + this.command, error)
    this.d.reject(error)
  }
}
{'fraction_non_alphanumeric': 0.1282514450867052, 'fraction_numerical': 0.000722543352601156, 'mean_word_length': 2.835180055401662, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '3247452', 'n_tokens_mistral': 879, 'n_tokens_neox': 856, 'n_words': 294}