<commit_before>import fsmIterator, { qEnd, safeName } from './fsmIterator'
import { delay, fork, race, take } from '../io'
import { END } from '../channel'

export default function debounceHelper(delayLength, patternOrChannel, worker, ...args) {
  let action, raceOutput

  const yTake = {done: false, value: take(patternOrChannel)}
  const yRace = {
    done: false,
    value: race({
      action: take(patternOrChannel),
      debounce: delay(delayLength),
    }),
  }
  const yFork = ac => ({done: false, value: fork(worker, ...args, ac)})
  const yNoop = (value) => ({done: false, value})

  const setAction = ac => action = ac
  const setRaceOutput = ro => raceOutput = ro

  return fsmIterator(
    {
      q1() {
        return {nextState: 'q2', effect: yTake, stateUpdater: setAction}
      },
      q2() {
        return action === END ?
          {nextState: qEnd} :
          {nextState: 'q3', effect: yRace, stateUpdater: setRaceOutput}
      },
      q3() {
        return raceOutput.debounce ? 
          {nextState: 'q1', effect: yFork(action)} :
          {nextState: 'q2', effect: yNoop(raceOutput.action), stateUpdater: setAction}
      },
    },
    'q1',
    `debounce(${safeName(patternOrChannel)}, ${worker.name})`
  )
}<commit_msg>core: remove END check<commit_after>import fsmIterator, { safeName } from './fsmIterator'
import { delay, fork, race, take } from '../io'

export default function debounceHelper(delayLength, patternOrChannel, worker, ...args) {
  let action, raceOutput

  const yTake = {done: false, value: take(patternOrChannel)}
  const yRace = {
    done: false,
    value: race({
      action: take(patternOrChannel),
      debounce: delay(delayLength),
    }),
  }
  const yFork = ac => ({done: false, value: fork(worker, ...args, ac)})
  const yNoop = (value) => ({done: false, value})

  const setAction = ac => action = ac
  const setRaceOutput = ro => raceOutput = ro

  return fsmIterator(
    {
      q1() {
        return {nextState: 'q2', effect: yTake, stateUpdater: setAction}
      },
      q2() {
        return {nextState: 'q3', effect: yRace, stateUpdater: setRaceOutput}
      },
      q3() {
        return raceOutput.debounce ? 
          {nextState: 'q1', effect: yFork(action)} :
          {nextState: 'q2', effect: yNoop(raceOutput.action), stateUpdater: setAction}
      },
    },
    'q1',
    `debounce(${safeName(patternOrChannel)}, ${worker.name})`
  )
}
{'fraction_non_alphanumeric': 0.14981427981840692, 'fraction_numerical': 0.006603384234420141, 'mean_word_length': 2.9414634146341463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4435048', 'n_tokens_mistral': 801, 'n_tokens_neox': 789, 'n_words': 206}