Repository: atom/atom
File: src/main-process/atom-application.js (JavaScript)

Code before:

  async destroy () {
    const windowsClosePromises = this.getAllWindows().map(window => {
      window.close()
      return window.closedPromise
    })
    await Promise.all(windowsClosePromises)
    this.disposable.dispose()
  }

  async launch (options) {
    if (!this.configFilePromise) {
      this.configFilePromise = this.configFile.watch()
      this.disposable.add(await this.configFilePromise)
      this.config.onDidChange('core.titleBar', () => this.promptForRestart())
      this.config.onDidChange('core.colorProfile', () => this.promptForRestart())
    }

    let optionsForWindowsToOpen = []
    let shouldReopenPreviousWindows = false

    if (options.test || options.benchmark || options.benchmarkTest) {
      optionsForWindowsToOpen.push(options)
    } else if (options.newWindow) {
      shouldReopenPreviousWindows = false
    } else if ((options.pathsToOpen && options.pathsToOpen.length > 0) ||
               (options.urlsToOpen && options.urlsToOpen.length > 0)) {
      optionsForWindowsToOpen.push(options)
      shouldReopenPreviousWindows = this.config.get('core.restorePreviousWindowsOnStart') === 'always'
    } else {
      shouldReopenPreviousWindows = this.config.get('core.restorePreviousWindowsOnStart') !== 'no'
    }

    if (shouldReopenPreviousWindows) {
      optionsForWindowsToOpen = [...await this.loadPreviousWindowOptions(), ...optionsForWindowsToOpen]
    }

    if (optionsForWindowsToOpen.length === 0) {
      optionsForWindowsToOpen.push(options)
    }

    // Preserve window opening order
    const windows = []
    for (const options of optionsForWindowsToOpen) {
      windows.push(await this.openWithOptions(options))
    }
    return windows
  }

  openWithOptions (options) {
    const {

Review comment (suggestion):
The `this.config.onDidChange` calls below this `await` really do need to happen *after* the config file is loaded. That is what is causing #19323. I have a fix in the works and will get it out later today.

Code after:
    const result = await this.launch(options)
    this.autoUpdateManager.initialize()
    await socketServerPromise

    return result
  }

  async destroy () {
    const windowsClosePromises = this.getAllWindows().map(window => {
      window.close()
      return window.closedPromise
    })
    await Promise.all(windowsClosePromises)
    this.disposable.dispose()
  }

  async launch (options) {
    if (!this.configFilePromise) {
      this.configFilePromise = this.configFile.watch()

      // TodoElectronIssue: In electron v2 awaiting the watcher causes some delay
      // in Windows machines, which affects directly the startup time.
      if (process.platform === 'win32') {
        this.configFilePromise.then(disposable => this.disposable.add(disposable))
      } else {
        this.disposable.add(await this.configFilePromise)
      }
      this.config.onDidChange('core.titleBar', () => this.promptForRestart())
      this.config.onDidChange('core.colorProfile', () => this.promptForRestart())
    }

    let optionsForWindowsToOpen = []
    let shouldReopenPreviousWindows = false

    if (options.test || options.benchmark || options.benchmarkTest) {
      optionsForWindowsToOpen.push(options)
    } else if (options.newWindow) {
      shouldReopenPreviousWindows = false
    } else if ((options.pathsToOpen && options.pathsToOpen.length > 0) ||
               (options.urlsToOpen && options.urlsToOpen.length > 0)) {
      optionsForWindowsToOpen.push(options)
      shouldReopenPreviousWindows = this.config.get('core.restorePreviousWindowsOnStart') === 'always'
    } else {
      shouldReopenPreviousWindows = this.config.get('core.restorePreviousWindowsOnStart') !== 'no'
    }

    if (shouldReopenPreviousWindows) {
      optionsForWindowsToOpen = [...await this.loadPreviousWindowOptions(), ...optionsForWindowsToOpen]
    }

    if (optionsForWindowsToOpen.length === 0) {