function onState (err, _state) {
  if (err) return onError(err)

  // Make available for easier debugging
  state = window.state = _state
  window.dispatch = dispatch

  telemetry.init(state)
  sound.init(state)

  // Log uncaught JS errors
  window.addEventListener(
    'error', (e) => telemetry.logUncaughtError('window', e), true /* capture */
  )

  // Create controllers
  controllers = {
    media: createGetter(() => {
      const MediaController = require('./controllers/media-controller')
      return new MediaController(state)
    }),
    playback: createGetter(() => {
      const PlaybackController = require('./controllers/playback-controller')
      return new PlaybackController(state, config, update)
    }),
    prefs: createGetter(() => {
      const PrefsController = require('./controllers/prefs-controller')
      return new PrefsController(state, config)
    }),
    subtitles: createGetter(() => {
      const SubtitlesController = require('./controllers/subtitles-controller')
      return new SubtitlesController(state)
    }),
    audioTracks: createGetter(() => {
      const AudioTracksController = require('./controllers/audio-tracks-controller')
      return new AudioTracksController(state)
    }),
    torrent: createGetter(() => {
      const TorrentController = require('./controllers/torrent-controller')
      return new TorrentController(state)
    }),
    torrentList: createGetter(() => new TorrentListController(state)),
    update: createGetter(() => {
      const UpdateController = require('./controllers/update-controller')
      return new UpdateController(state)
    }),
    folderWatcher: createGetter(() => {
      const FolderWatcherController = require('./controllers/folder-watcher-controller')
      return new FolderWatcherController()
    })
  }

  // Add first page to location history
  state.location.go({
    url: 'home',
    setup: (cb) => {
      state.window.title = config.APP_WINDOW_TITLE
      cb(null)
    }
  })

  // Give global trackers
  setGlobalTrackers()

  // Restart everything we were torrenting last time the app ran
  resumeTorrents()

  // Initialize ReactDOM
  ReactDOM.render(
    <App state={state} ref={elem => { app = elem }} />,
    document.querySelector('#body')
  )

  // Calling update() updates the UI given the current state
  // Do this at least once a second to give every file in every torrentSummary
  // a progress bar and to keep the cursor in sync when playing a video
  setInterval(update, 1000)

  // Listen for messages from the main process
  setupIpc()

  // Drag and drop files/text to start torrenting or seeding
  dragDrop('body', {
    onDrop: onOpen,
    onDropText: onOpen
  })

  // ...same thing if you paste a torrent
  document.addEventListener('paste', onPaste)

  // Add YouTube style hotkey shortcuts
  window.addEventListener('keydown', onKeydown)

  const debouncedFullscreenToggle = debounce(() => {
    dispatch('toggleFullScreen')
  }, 1000, true)

  document.addEventListener('wheel', event => {
    // ctrlKey detects pinch to zoom, http://crbug.com/289887
    if (event.ctrlKey) {
      event.preventDefault()
      debouncedFullscreenToggle()
    }
  })

  // ...focus and blur. Needed to show correct dock icon text ('badge') in OSX
  window.addEventListener('focus', onFocus)
  window.addEventListener('blur', onBlur)

  if (electron.remote.getCurrentWindow().isVisible()) {
    sound.play('STARTUP')
  }

  // To keep app startup fast, some code is delayed.
  window.setTimeout(delayedInit, config.DELAYED_INIT)

  // Done! Ideally we want to get here < 500ms after the user clicks the app
  console.timeEnd('init')
}
Perf optimization: Hook into require() to modify how certain modules load:

- `inline-style-prefixer` (used by `material-ui`) takes ~40ms. It is not
  actually used because auto-prefixing is disabled with
  `darkBaseTheme.userAgent = false`. Return a fake object.