From 26f0ef5424ba3e7e45dcb5a9a56f47348119678d Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 2 Oct 2015 12:12:16 -0600 Subject: [PATCH] Move Project.deserialize to instance method So we can instantiate atom.project during Atom environment construction. Signed-off-by: Max Brunsfeld --- spec/git-spec.coffee | 3 ++- spec/project-spec.coffee | 13 +++++++++---- spec/spec-helper.coffee | 3 ++- spec/workspace-spec.coffee | 4 +++- src/atom.coffee | 4 ++-- src/project.coffee | 40 ++++++++++++++++++-------------------- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/spec/git-spec.coffee b/spec/git-spec.coffee index a72cd47cd..e627ec520 100644 --- a/spec/git-spec.coffee +++ b/spec/git-spec.coffee @@ -277,7 +277,8 @@ describe "GitRepository", -> atom.workspace.open('file.txt') runs -> - project2 = Project.deserialize(atom.project.serialize()) + project2 = new Project() + project2.deserialize(atom.project.serialize(), atom.deserializers) buffer = project2.getBuffers()[0] waitsFor -> diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 13dd3e70c..3de55a02d 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -66,7 +66,9 @@ describe "Project", -> runs -> expect(atom.project.getBuffers().length).toBe 1 - deserializedProject = Project.deserialize(atom.project.serialize()) + + deserializedProject = new Project() + deserializedProject.deserialize(atom.project.serialize(), atom.deserializers) expect(deserializedProject.getBuffers().length).toBe 0 it "listens for destroyed events on deserialized buffers and removes them when they are destroyed", -> @@ -75,7 +77,8 @@ describe "Project", -> runs -> expect(atom.project.getBuffers().length).toBe 1 - deserializedProject = Project.deserialize(atom.project.serialize()) + deserializedProject = new Project + deserializedProject.deserialize(atom.project.serialize(), atom.deserializers) expect(deserializedProject.getBuffers().length).toBe 1 deserializedProject.getBuffers()[0].destroy() @@ -91,7 +94,8 @@ describe "Project", -> runs -> expect(atom.project.getBuffers().length).toBe 1 fs.mkdirSync(pathToOpen) - deserializedProject = Project.deserialize(atom.project.serialize()) + deserializedProject = new Project + deserializedProject.deserialize(atom.project.serialize(), atom.deserializers) expect(deserializedProject.getBuffers().length).toBe 0 it "does not deserialize buffers when their path is inaccessible", -> @@ -104,7 +108,8 @@ describe "Project", -> runs -> expect(atom.project.getBuffers().length).toBe 1 fs.chmodSync(pathToOpen, '000') - deserializedProject = Project.deserialize(atom.project.serialize()) + deserializedProject = new Project() + deserializedProject.deserialize(atom.project.serialize(), atom.deserializers) expect(deserializedProject.getBuffers().length).toBe 0 describe "when an editor is saved and the project has no path", -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index ec06b9087..8764c5eb0 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -85,7 +85,8 @@ beforeEach -> documentTitle = null projectPath = specProjectPath ? path.join(@specDirectory, 'fixtures') atom.packages.serviceHub = new ServiceHub - atom.project = new Project(paths: [projectPath]) + atom.project = new Project + atom.project.setPaths([projectPath]) atom.workspace = new Workspace() atom.themes.workspace = atom.workspace atom.keymaps.keyBindings = _.clone(keyBindingsToRestore) diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index c60508640..0dacf7468 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -1,6 +1,7 @@ path = require 'path' temp = require 'temp' Workspace = require '../src/workspace' +Project = require '../src/project' Pane = require '../src/pane' platform = require './spec-helper-platform' _ = require 'underscore-plus' @@ -21,7 +22,8 @@ describe "Workspace", -> projectState = atom.project.serialize() atom.workspace.destroy() atom.project.destroy() - atom.project = atom.deserializers.deserialize(projectState) + atom.project = new Project() + atom.project.deserialize(projectState, atom.deserializers) atom.workspace = Workspace.deserialize(workspaceState) describe "when the workspace contains text editors", -> diff --git a/src/atom.coffee b/src/atom.coffee index 91d9f6e77..2a3831de2 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -652,9 +652,9 @@ class Atom extends Model deserializeProject: -> Project = require './project' - startTime = Date.now() - @project ?= @deserializers.deserialize(@state.project) ? new Project() + @project = new Project() + @project.deserialize(@state.project, @deserializers) if @state.project? @deserializeTimings.project = Date.now() - startTime deserializeWorkspace: -> diff --git a/src/project.coffee b/src/project.coffee index faead038e..45a62c799 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -21,23 +21,10 @@ class Project extends Model Section: Construction and Destruction ### - @deserialize: (state) -> - state.buffers = _.compact state.buffers.map (bufferState) -> - # Check that buffer's file path is accessible - return if fs.isDirectorySync(bufferState.filePath) - if bufferState.filePath - try - fs.closeSync(fs.openSync(bufferState.filePath, 'r')) - catch error - return unless error.code is 'ENOENT' - - atom.deserializers.deserialize(bufferState) - - new this(state) - - constructor: ({path, paths, @buffers}={}) -> + constructor: -> @emitter = new Emitter - @buffers ?= [] + @buffers = [] + @paths = [] @rootDirectories = [] @repositories = [] @@ -68,11 +55,6 @@ class Project extends Model @setPaths(@getPaths()) ) - @subscribeToBuffer(buffer) for buffer in @buffers - - paths ?= _.compact([path]) - @setPaths(paths) - destroyed: -> buffer.destroy() for buffer in @getBuffers() @setPaths([]) @@ -85,6 +67,22 @@ class Project extends Model Section: Serialization ### + deserialize: (state, deserializerManager) -> + states.paths = [state.path] if state.path? # backward compatibility + + @buffers = _.compact state.buffers.map (bufferState) -> + # Check that buffer's file path is accessible + return if fs.isDirectorySync(bufferState.filePath) + if bufferState.filePath + try + fs.closeSync(fs.openSync(bufferState.filePath, 'r')) + catch error + return unless error.code is 'ENOENT' + deserializerManager.deserialize(bufferState) + + @subscribeToBuffer(buffer) for buffer in @buffers + @setPaths(state.paths) + serialize: -> deserializer: 'Project' paths: @getPaths()