<commit_before>import Signal from './signal'

/**
 * This module defines keyboard signals.
 *
 * @module bulb/keyboard
 * @summary Keyboard Signals
 */

/**
 * Returns a new signal that emits a value every time the keyboard state
 * changes.
 *
 * @summary Creates a keyboard state signal.
 * @param target The event target that the signal listens on.
 * @returns A new signal.
 */
export function keys (target) {
  let state = {}

  return new Signal(observer => {
    const downHandler = e => {
      state[e.keyCode] = true
      observer.next(state)
    }

    const upHandler = e => {
      delete state[e.keyCode]
      observer.next(state)
    }

    target.addEventListener('keydown', downHandler, true)
    target.addEventListener('keyup', upHandler, true)

    return () => {
      target.removeEventListener('keydown', downHandler, true)
      target.removeEventListener('keyup', upHandler, true)
    }<commit_msg>Don't generate duplicate keyboard events<commit_after>import Signal from './signal'

/**
 * This module defines keyboard signals.
 *
 * @module bulb/keyboard
 * @summary Keyboard Signals
 */

/**
 * Returns a new signal that emits a value every time the keyboard state
 * changes.
 *
 * @summary Creates a keyboard state signal.
 * @param target The event target that the signal listens on.
 * @returns A new signal.
 */
export function keys (target) {
  let state = {}

  return new Signal(observer => {
    const downHandler = e => {
      if (state[e.keyCode] !== true) {
        state[e.keyCode] = true
        observer.next(state)
      }
    }

    const upHandler = e => {
      if (state[e.keyCode] !== undefined) {
        delete state[e.keyCode]
        observer.next(state)
      }
    }

    target.addEventListener('keydown', downHandler, true)
    target.addEventListener('keyup', upHandler, true)

    return () => {
      target.removeEventListener('keydown', downHandler, true)
      target.removeEventListener('keyup', upHandler, true)
    }
{'fraction_non_alphanumeric': 0.11643145161290322, 'fraction_numerical': 0.0, 'mean_word_length': 2.993963782696177, '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': '7271415', 'n_tokens_mistral': 571, 'n_tokens_neox': 552, 'n_words': 174}