From a9c1b58ba501a7f759e0573a3093821ae4acf627 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 22 Jan 2013 13:26:22 -0800 Subject: [PATCH] Defer tree view load when possible --- src/app/deferred-atom-package.coffee | 6 +++- src/packages/tree-view/index.coffee | 30 +++++++++++++++---- .../tree-view/spec/tree-view-spec.coffee | 9 ++---- src/packages/tree-view/src/tree-view.coffee | 6 +--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/app/deferred-atom-package.coffee b/src/app/deferred-atom-package.coffee index d2ac6f76b..5b040e3a9 100644 --- a/src/app/deferred-atom-package.coffee +++ b/src/app/deferred-atom-package.coffee @@ -16,7 +16,11 @@ class DeferredAtomPackage extends AtomPackage deactivate: -> @instance?.deactivate?() - serialize: -> @instance?.serialize?() + serialize: -> + if @instance + @instance.serialize?() + else + @state getInstance: -> unless @instance diff --git a/src/packages/tree-view/index.coffee b/src/packages/tree-view/index.coffee index 44f5d4478..b2228f183 100644 --- a/src/packages/tree-view/index.coffee +++ b/src/packages/tree-view/index.coffee @@ -1,8 +1,26 @@ -AtomPackage = require 'atom-package' -TreeView = require './src/tree-view' +DeferredAtomPackage = require 'deferred-atom-package' module.exports = -class Tree extends AtomPackage - activate: (rootView, state) -> TreeView.activate(rootView, state) - deactivate: -> TreeView.deactivate() - serialize: -> TreeView.serialize() +class Tree extends DeferredAtomPackage + + loadEvents: [ + 'tree-view:toggle' + 'tree-view:reveal-active-file' + ] + + instanceClass: 'tree-view/src/tree-view' + + activate: (rootView, state) -> + super + + if state + @getInstance().attach() if state.attached + else if rootView.project.getPath() and not rootView.pathToOpenIsFile + @getInstance().attach() + + onLoadEvent: (event, instance) -> + switch event.type + when 'tree-view:toggle' + instance.toggle() + when 'tree-view:reveal-active-file' + instance.revealActiveFile() diff --git a/src/packages/tree-view/spec/tree-view-spec.coffee b/src/packages/tree-view/spec/tree-view-spec.coffee index 580aaa846..c788842bf 100644 --- a/src/packages/tree-view/spec/tree-view-spec.coffee +++ b/src/packages/tree-view/spec/tree-view-spec.coffee @@ -13,8 +13,7 @@ describe "TreeView", -> rootView = new RootView(require.resolve('fixtures/tree-view')) project = rootView.project - atom.loadPackage("tree-view") - treeView = TreeView.instance + treeView = atom.loadPackage("tree-view").getInstance() treeView.root = treeView.find('ol > li:first').view() sampleJs = treeView.find('.file:contains(tree-view.js)') sampleTxt = treeView.find('.file:contains(tree-view.txt)') @@ -51,8 +50,7 @@ describe "TreeView", -> rootView.deactivate() rootView = new RootView - atom.loadPackage 'tree-view' - treeView = TreeView.instance + treeView = atom.loadPackage("tree-view").getInstance() it "does not attach to the root view or create a root node when initialized", -> expect(treeView.hasParent()).toBeFalsy() @@ -78,8 +76,7 @@ describe "TreeView", -> rootView.deactivate() rootView = new RootView(require.resolve('fixtures/tree-view/tree-view.js')) - atom.loadPackage 'tree-view' - treeView = TreeView.instance + treeView = atom.loadPackage("tree-view").getInstance() it "does not attach to the root view but does create a root node when initialized", -> expect(treeView.hasParent()).toBeFalsy() diff --git a/src/packages/tree-view/src/tree-view.coffee b/src/packages/tree-view/src/tree-view.coffee index 1068f9f1f..66b2b556f 100644 --- a/src/packages/tree-view/src/tree-view.coffee +++ b/src/packages/tree-view/src/tree-view.coffee @@ -17,9 +17,6 @@ class TreeView extends ScrollView else @instance = new TreeView(rootView) - if rootView.project.getPath() and not rootView.pathToOpenIsFile - @instance.attach() - @deactivate: -> @instance.deactivate() @@ -65,8 +62,7 @@ class TreeView extends ScrollView @selectEntryForPath(@selectedPath) if @selectedPath else @selectActiveFile() - @rootView.command 'tree-view:toggle', => @toggle() - @rootView.command 'tree-view:reveal-active-file', => @revealActiveFile() + @rootView.on 'root-view:active-path-changed', => @selectActiveFile() @rootView.project.on 'path-changed', => @updateRoot() @observeConfig 'core.hideGitIgnoredFiles', => @updateRoot()