<commit_before>import { autorun, observe } from "mobx"
import { isNotNull, isNotUndefined } from "../../common/helpers/array"
import { emptySelection } from "../../common/selection/Selection"
import { resetSelection } from "../actions"
import MIDIOutput from "../services/MIDIOutput"
import RootStore from "./RootStore"

export const registerReactions = (rootStore: RootStore) => {
  // reset selection when tool changed
  observe(rootStore.pianoRollStore, "mouseMode", resetSelection(rootStore))

  // sync synthGroup.output to enabledOutputIds/isFactorySoundEnabled
  const updateOutputDevices = () => {
    const { midiDeviceStore } = rootStore

    rootStore.services.player.allSoundsOff()

    const getMIDIDeviceEntries = () =>
      Array.from(midiDeviceStore.enabledOutputIds.values())
        .map((deviceId) => {
          const device = midiDeviceStore.outputs.find((d) => d.id === deviceId)
          if (device === undefined) {
            console.error(`device not found: ${deviceId}`)
            return null
          }
          return { synth: new MIDIOutput(device), isEnabled: true }
        })
        .filter(isNotNull)

    rootStore.services.synthGroup.outputs = [
      {
        synth: rootStore.services.synth,
        isEnabled: midiDeviceStore.isFactorySoundEnabled,
      },
      ...getMIDIDeviceEntries(),
    ]
  }

  observe(rootStore.midiDeviceStore, "enabledOutputIds", updateOutputDevices)
  observe(
    rootStore.midiDeviceStore,
    "isFactorySoundEnabled",
    updateOutputDevices
  )

  const updateInputDevices = () => {
    const { midiDeviceStore } = rootStore

    const devices = Array.from(midiDeviceStore.enabledInputIds.values())
      .map((deviceId) => midiDeviceStore.inputs.find((d) => d.id === deviceId))
      .filter(isNotUndefined)

    rootStore.services.midiInputGroup.devices = devices
  }

  observe(rootStore.midiDeviceStore, "enabledInputIds", updateInputDevices)

  // reset selection when change track
  autorun(() => {
    // deep observe
    const _ = rootStore.song.selectedTrackId
    rootStore.pianoRollStore.selection = emptySelection
  })
}
<commit_msg>Refactor<commit_after>import { observe } from "mobx"
import { isNotNull, isNotUndefined } from "../../common/helpers/array"
import { resetSelection } from "../actions"
import MIDIOutput from "../services/MIDIOutput"
import RootStore from "./RootStore"

export const registerReactions = (rootStore: RootStore) => {
  // reset selection when tool changed
  observe(rootStore.pianoRollStore, "mouseMode", resetSelection(rootStore))

  observe(
    rootStore.midiDeviceStore,
    "enabledOutputIds",
    updateOutputDevices(rootStore)
  )

  observe(
    rootStore.midiDeviceStore,
    "isFactorySoundEnabled",
    updateOutputDevices(rootStore)
  )

  observe(
    rootStore.midiDeviceStore,
    "enabledInputIds",
    updateInputDevices(rootStore)
  )

  // reset selection when change track
  observe(rootStore.song, "selectedTrackId", resetSelection(rootStore))
}

type Reaction = (rootStore: RootStore) => () => void

// sync synthGroup.output to enabledOutputIds/isFactorySoundEnabled
const updateOutputDevices: Reaction = (rootStore) => () => {
  const { midiDeviceStore } = rootStore

  rootStore.services.player.allSoundsOff()

  const getMIDIDeviceEntries = () =>
    Array.from(midiDeviceStore.enabledOutputIds.values())
      .map((deviceId) => {
        const device = midiDeviceStore.outputs.find((d) => d.id === deviceId)
        if (device === undefined) {
          console.error(`device not found: ${deviceId}`)
          return null
        }
        return { synth: new MIDIOutput(device), isEnabled: true }
      })
      .filter(isNotNull)

  rootStore.services.synthGroup.outputs = [
    {
      synth: rootStore.services.synth,
      isEnabled: midiDeviceStore.isFactorySoundEnabled,
    },
    ...getMIDIDeviceEntries(),
  ]
}

const updateInputDevices: Reaction = (rootStore) => () => {
  const { midiDeviceStore } = rootStore

  const devices = Array.from(midiDeviceStore.enabledInputIds.values())
    .map((deviceId) => midiDeviceStore.inputs.find((d) => d.id === deviceId))
    .filter(isNotUndefined)

  rootStore.services.midiInputGroup.devices = devices
}

{'fraction_non_alphanumeric': 0.12161520190023753, 'fraction_numerical': 0.0, 'mean_word_length': 3.5524324324324326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '7433708', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1236, 'n_words': 236}