Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Fix race condition between opening new files and restoring window state #16530

Merged
merged 2 commits into from Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 23 additions & 9 deletions spec/atom-environment-spec.js
@@ -1,5 +1,6 @@
const {it, fit, ffit, beforeEach, afterEach, conditionPromise} = require('./async-spec-helpers')
const _ = require('underscore-plus')
const fs = require('fs')
const path = require('path')
const temp = require('temp').track()
const AtomEnvironment = require('../src/atom-environment')
Expand Down Expand Up @@ -471,15 +472,28 @@ describe('AtomEnvironment', () => {
await atom.workspace.open()
})

it('automatically restores the saved state into the current environment', () => {
const state = {}
spyOn(atom.workspace, 'open')
spyOn(atom, 'restoreStateIntoThisEnvironment')

atom.attemptRestoreProjectStateForPaths(state, [__dirname], [__filename])
expect(atom.restoreStateIntoThisEnvironment).toHaveBeenCalledWith(state)
expect(atom.workspace.open.callCount).toBe(1)
expect(atom.workspace.open).toHaveBeenCalledWith(__filename)
it('automatically restores the saved state into the current environment', async () => {
const projectPath = temp.mkdirSync()
const filePath1 = path.join(projectPath, 'file-1')
const filePath2 = path.join(projectPath, 'file-2')
const filePath3 = path.join(projectPath, 'file-3')
fs.writeFileSync(filePath1, 'abc')
fs.writeFileSync(filePath2, 'def')
fs.writeFileSync(filePath3, 'ghi')

const env1 = new AtomEnvironment({applicationDelegate: atom.applicationDelegate})
env1.project.setPaths([projectPath])
await env1.workspace.open(filePath1)
await env1.workspace.open(filePath2)
await env1.workspace.open(filePath3)
const env1State = env1.serialize()
env1.destroy()

const env2 = new AtomEnvironment({applicationDelegate: atom.applicationDelegate})
await env2.attemptRestoreProjectStateForPaths(env1State, [projectPath], [filePath2])
const restoredURIs = env2.workspace.getPaneItems().map(p => p.getURI())
expect(restoredURIs).toEqual([filePath1, filePath2, filePath3])
env2.destroy()
})

describe('when a dock has a non-text editor', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/atom-environment.js
Expand Up @@ -373,7 +373,7 @@ class AtomEnvironment {
if (this.project) this.project.destroy()
this.project = null
this.commands.clear()
this.stylesElement.remove()
if (this.stylesElement) this.stylesElement.remove()
this.config.unobserveUserConfig()
this.autoUpdater.destroy()
this.uriHandlerRegistry.destroy()
Expand Down Expand Up @@ -1121,7 +1121,7 @@ class AtomEnvironment {
}

if (windowIsUnused()) {
this.restoreStateIntoThisEnvironment(state)
await this.restoreStateIntoThisEnvironment(state)
return Promise.all(filesToOpen.map(file => this.workspace.open(file)))
} else {
let resolveDiscardStatePromise = null
Expand Down