<commit_before>idCounter = 0

module.exports =
class SpellCheckTask
  @handler: null
  @jobs: []

  constructor: (@task) ->
    @id = idCounter++

  terminate: ->
    @constructor.removeFromArray(@constructor.jobs, (j) -> j.args.id is @id)

  start: (buffer, onDidSpellCheck) ->
    # Figure out the paths since we need that for checkers that are project-specific.
    projectPath = null
    relativePath = null
    if buffer?.file?.path
      [projectPath, relativePath] = atom.project.relativizePath(buffer.file.path)

    # Remove old jobs for this SpellCheckTask from the shared jobs list.
    @constructor.removeFromArray(@constructor.jobs, (j) -> j.args.id is @id)

    # Create an job that contains everything we'll need to do the work.
    job = {
      task: @task,
      callbacks: [onDidSpellCheck],
      args: {
        id: @id,
        projectPath,
        relativePath,
        text: buffer.getText()
      }
    }

    if (@constructor.jobs.length > 0)
      for i in [0..@constructor.jobs.length-1]
        if (@isDuplicateRequest(@constructor.jobs[i], job))
          console.log('De-duping ' + relativePath)
          @constructor.jobs[i].callbacks.push(onDidSpellCheck)
          return

    # Do the work now if not busy or queue it for later.
    @constructor.jobs.unshift(job)
    if @constructor.jobs.length is 1
      @constructor.startNextJob()
    else
      console.log('Queuing work ' + job.args.id)

  isDuplicateRequest: (a, b) -><commit_msg>Minor clean-up of the code to clarify piggybacks<commit_after>idCounter = 0

module.exports =
class SpellCheckTask
  @handler: null
  @jobs: []

  constructor: (@task) ->
    @id = idCounter++

  terminate: ->
    @constructor.removeFromArray(@constructor.jobs, (j) -> j.args.id is @id)

  start: (buffer, onDidSpellCheck) ->
    # Figure out the paths since we need that for checkers that are project-specific.
    projectPath = null
    relativePath = null
    if buffer?.file?.path
      [projectPath, relativePath] = atom.project.relativizePath(buffer.file.path)

    # Remove old jobs for this SpellCheckTask from the shared jobs list.
    @constructor.removeFromArray(@constructor.jobs, (j) -> j.args.id is @id)

    # Create an job that contains everything we'll need to do the work.
    job = {
      task: @task,
      callbacks: [onDidSpellCheck],
      args: {
        id: @id,
        projectPath,
        relativePath,
        text: buffer.getText()
      }
    }

    # If we already have a job for this work piggy-back on it with our callback.
    return if @constructor.piggybackExistingJob(job)

    # Do the work now if not busy or queue it for later.
    @constructor.jobs.unshift(job)
    if @constructor.jobs.length is 1
      @constructor.startNextJob()
    else
      console.log('Queuing work ' + job.args.id)

  @piggybackExistingJob: (newJob) ->
    if (@jobs.length > 0)
      for i in [0..@jobs.length-1]
        job = @jobs[i]
        if (@isDuplicateRequest(job, newJob))
          console.log('Piggybacking ' + job.args.relativePath + ' on ' + job.args.id)
          job.callbacks = job.callbacks.concat(newJob.callbacks)
          return true
    return false

  @isDuplicateRequest: (a, b) ->
{'fraction_non_alphanumeric': 0.11350844277673545, 'fraction_numerical': 0.0031269543464665416, 'mean_word_length': 2.6727898966704937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5257423', 'n_tokens_mistral': 1027, 'n_tokens_neox': 988, 'n_words': 279}