<commit_before>package io.youi

import scala.concurrent.Future
import scala.util.{Failure, Success}

import scribe.Execution.global

/**
  * KeyedSequence allows the use of a unique key to build sequences of futures so that key is not concurrently utilized.
  *
  * Should self-clean upon completion.
  */
object KeyedSequence {
  var errorHandler: Throwable => Unit = (t: Throwable) => scribe.error(t)

  private var map = Map.empty[Any, Future[Any]]

  def apply[Key, Return](key: Key)(builder: => Future[Return]): Future[Return] = synchronized {
    val future: Future[Return] = map.get(key) match {
      case Some(running) => {
        running.transformWith { _ =>
          builder
        }
      }
      case None => {
        builder
      }
    }
    map += key -> future
    future.onComplete { result =>
      if (map(key) eq future) {
        map -= key
      }
      result match {
        case Success(_) => // Success, nothing to do
        case Failure(exception) => errorHandler(exception)
      }
    }
    future
  }
}<commit_msg>Minor updates to KeyedSequence<commit_after>package io.youi

import scala.concurrent.Future
import scala.util.{Failure, Success}

import scribe.Execution.global

/**
  * KeyedSequence allows the use of a unique key to build sequences of futures so that key is not concurrently utilized.
  *
  * Should self-clean upon completion.
  */
object KeyedSequence {
  var errorHandler: Throwable => Unit = (t: Throwable) => scribe.error(t)

  private var map = Map.empty[Any, Future[Any]]

  def apply[Key, Return](key: Key)(builder: => Future[Return]): Future[Return] = synchronized {
    val future: Future[Return] = map.get(key) match {
      case Some(running) => {
        running.transformWith { _ =>
          builder
        }
      }
      case None => {
        builder
      }
    }
    map += key -> future
    future.onComplete { result =>
      KeyedSequence.synchronized {
        if (map(key) eq future) {
          map -= key
        }
        result match {
          case Success(_) => // Success, nothing to do
          case Failure(exception) => errorHandler(exception)
        }
      }
    }
    future
  }
}
{'fraction_non_alphanumeric': 0.11177552897884084, 'fraction_numerical': 0.0, 'mean_word_length': 2.419811320754717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6032891', 'n_tokens_mistral': 640, 'n_tokens_neox': 624, 'n_words': 182}