From 8591c86733c5b6f6878a2de555ea831a2e7a1a7e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 4 Jan 2013 14:33:01 -0700 Subject: [PATCH] Pass a name string to `RootView.activatePackage` & `deactivatePackage` Previously we were relying on the package module itself to have a `name` field. But now that we're using `atom.loadPackage` to load up packages, we can infer the name of the module from the name of the package directory. --- spec/app/atom-spec.coffee | 2 +- spec/app/root-view-spec.coffee | 30 ++++++++----------- src/app/atom-package.coffee | 2 +- src/app/root-view.coffee | 15 +++++----- .../spec/command-logger-spec.coffee | 2 +- .../spec/command-panel-spec.coffee | 6 ++-- .../tree-view/spec/tree-view-spec.coffee | 14 +++------ 7 files changed, 29 insertions(+), 42 deletions(-) diff --git a/spec/app/atom-spec.coffee b/spec/app/atom-spec.coffee index 055533762..58fa6f810 100644 --- a/spec/app/atom-spec.coffee +++ b/spec/app/atom-spec.coffee @@ -16,7 +16,7 @@ describe "the `atom` global", -> it "requires and activates the package's main module if it exists", -> spyOn(rootView, 'activatePackage').andCallThrough() atom.loadPackage("package-with-module") - expect(rootView.activatePackage).toHaveBeenCalledWith(extension) + expect(rootView.activatePackage).toHaveBeenCalledWith('package-with-module', extension) describe "keymap loading", -> describe "when package.json does not contain a 'keymaps' manifest", -> diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index 102a1d906..0bf78785c 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -128,14 +128,12 @@ describe "RootView", -> it "absorbs exceptions that are thrown by the package module's serialize methods", -> spyOn(console, 'error') - rootView.activatePackage( - name: "bad-egg" + rootView.activatePackage("bad-egg", activate: -> serialize: -> throw new Error("I'm broken") ) - rootView.activatePackage( - name: "good-egg" + rootView.activatePackage("good-egg" activate: -> serialize: -> "I still get called" ) @@ -398,39 +396,35 @@ describe "RootView", -> beforeEach -> packageModule = - name: 'package' deactivate: -> activate: jasmine.createSpy("activate") serialize: -> "it worked" - describe ".activatePackage(packageModule)", -> + describe ".activatePackage(name, packageModule)", -> it "calls activate on the package module", -> - rootView.activatePackage(packageModule) + rootView.activatePackage('package', packageModule) expect(packageModule.activate).toHaveBeenCalledWith(rootView, undefined) it "calls activate on the package module with its previous state", -> - rootView.activatePackage(packageModule) + rootView.activatePackage('package', packageModule) packageModule.activate.reset() newRootView = RootView.deserialize(rootView.serialize()) - newRootView.activatePackage(packageModule) + newRootView.activatePackage('package', packageModule) expect(packageModule.activate).toHaveBeenCalledWith(newRootView, "it worked") newRootView.remove() - it "throws an exception if the package module has no 'name' property", -> - expect(-> rootView.activatePackage({ activate: -> })).toThrow() - - describe ".deactivatePackage(packageModule)", -> + describe ".deactivatePackage(packageName)", -> it "deactivates and removes the package module from the package module map", -> - rootView.activatePackage(packageModule) - expect(rootView.packageModules[packageModule.name]).toBeTruthy() + rootView.activatePackage('package', packageModule) + expect(rootView.packageModules['package']).toBeTruthy() spyOn(packageModule, "deactivate").andCallThrough() - rootView.deactivatePackage(packageModule) + rootView.deactivatePackage('package') expect(packageModule.deactivate).toHaveBeenCalled() - expect(rootView.packageModules[packageModule.name]).toBeFalsy() + expect(rootView.packageModules['package']).toBeFalsy() it "is called when the rootView is deactivated to deactivate all packages", -> - rootView.activatePackage(packageModule) + rootView.activatePackage('package', packageModule) spyOn(rootView, "deactivatePackage").andCallThrough() spyOn(packageModule, "deactivate").andCallThrough() rootView.deactivate() diff --git a/src/app/atom-package.coffee b/src/app/atom-package.coffee index 16ab72d15..bb91b60af 100644 --- a/src/app/atom-package.coffee +++ b/src/app/atom-package.coffee @@ -18,7 +18,7 @@ class AtomPackage extends Package @loadMetadata() @loadKeymaps() @loadStylesheets() - rootView.activatePackage(@module) if @module + rootView.activatePackage(@name, @module) if @module catch e console.error "Failed to load package named '#{@name}'", e.stack diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index 624132c78..d58f082a2 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -112,18 +112,17 @@ class RootView extends View when 'PaneColumn' then PaneColumn.deserialize(viewState, this) when 'Editor' then Editor.deserialize(viewState, this) - activatePackage: (packageModule) -> - throw new Error("Trying to activate a package module with no name attribute") unless packageModule.name? - @packageModules[packageModule.name] = packageModule - packageModule.activate(this, @packageStates[packageModule.name]) + activatePackage: (name, packageModule) -> + @packageModules[name] = packageModule + packageModule.activate(this, @packageStates[name]) - deactivatePackage: (packageModule) -> - packageModule.deactivate?() - delete @packageModules[packageModule.name] + deactivatePackage: (name) -> + @packageModules[name].deactivate?() + delete @packageModules[name] deactivate: -> atom.setRootViewStateForPath(@project.getPath(), @serialize()) - @deactivatePackage(packageModule) for name, packageModule of @packageModules + @deactivatePackage(name) for name of @packageModules @remove() open: (path, options = {}) -> diff --git a/src/packages/command-logger/spec/command-logger-spec.coffee b/src/packages/command-logger/spec/command-logger-spec.coffee index ec6a82556..c7164454a 100644 --- a/src/packages/command-logger/spec/command-logger-spec.coffee +++ b/src/packages/command-logger/spec/command-logger-spec.coffee @@ -6,7 +6,7 @@ describe "CommandLogger", -> beforeEach -> rootView = new RootView(require.resolve('fixtures/sample.js')) - rootView.activatePackage(CommandLogger) + atom.loadPackage 'command-logger' editor = rootView.getActiveEditor() commandLogger = CommandLogger.instance rootView.attachToDom() diff --git a/src/packages/command-panel/spec/command-panel-spec.coffee b/src/packages/command-panel/spec/command-panel-spec.coffee index 7a074115c..0e076fd3a 100644 --- a/src/packages/command-panel/spec/command-panel-spec.coffee +++ b/src/packages/command-panel/spec/command-panel-spec.coffee @@ -36,7 +36,7 @@ describe "CommandPanel", -> rootView.deactivate() rootView2.attachToDom() - commandPanel = rootView2.activatePackage(CommandPanel) + commandPanel = rootView2.activatePackage('command-panel', CommandPanel) expect(rootView2.find('.command-panel')).toExist() expect(commandPanel.miniEditor.getText()).toBe 'abc' expect(commandPanel.miniEditor.isFocused).toBeTruthy() @@ -49,7 +49,7 @@ describe "CommandPanel", -> rootView3 = RootView.deserialize(rootView2.serialize()) rootView2.deactivate() rootView3.attachToDom() - commandPanel = rootView3.activatePackage(CommandPanel) + commandPanel = rootView3.activatePackage('command-panel', CommandPanel) expect(commandPanel.miniEditor.isFocused).toBeFalsy() rootView3.deactivate() @@ -71,7 +71,7 @@ describe "CommandPanel", -> rootView.deactivate() rootView2.attachToDom() - commandPanel = rootView2.activatePackage(CommandPanel) + commandPanel = rootView2.activatePackage('command-panel', CommandPanel) expect(commandPanel.history.length).toBe(2) expect(commandPanel.history[0]).toBe('/test2') expect(commandPanel.history[1]).toBe('/test3') diff --git a/src/packages/tree-view/spec/tree-view-spec.coffee b/src/packages/tree-view/spec/tree-view-spec.coffee index 6da0dec8f..611f4d9d7 100644 --- a/src/packages/tree-view/spec/tree-view-spec.coffee +++ b/src/packages/tree-view/spec/tree-view-spec.coffee @@ -51,7 +51,7 @@ describe "TreeView", -> rootView.deactivate() rootView = new RootView - rootView.activatePackage(TreeView) + atom.loadPackage 'tree-view' treeView = rootView.find(".tree-view").view() it "does not create a root node", -> @@ -71,12 +71,6 @@ describe "TreeView", -> expect(treeView.root).not.toEqual oldRoot expect(oldRoot.hasParent()).toBeFalsy() - describe "when the prototypes deactivate method is called", -> - it "calls the deactivate on tree view instance", -> - spyOn(treeView, "deactivate").andCallThrough() - rootView.deactivatePackage(TreeView) - expect(treeView.deactivate).toHaveBeenCalled() - describe "serialization", -> [newRootView, newTreeView] = [] @@ -89,7 +83,7 @@ describe "TreeView", -> newRootView = RootView.deserialize(rootView.serialize()) rootView.deactivate() # Deactivates previous TreeView - newRootView.activatePackage(TreeView) + newRootView.activatePackage('tree-view', TreeView) newTreeView = newRootView.find(".tree-view").view() @@ -106,7 +100,7 @@ describe "TreeView", -> rootView.deactivate() # Deactivates previous TreeView newRootView.attachToDom() - newRootView.activatePackage(TreeView) + newRootView.activatePackage('tree-view', TreeView) newTreeView = newRootView.find(".tree-view").view() expect(newTreeView).toMatchSelector ':focus' @@ -589,7 +583,7 @@ describe "TreeView", -> rootView = new RootView(rootDirPath) project = rootView.project - rootView.activatePackage(TreeView) + atom.loadPackage('tree-view') treeView = rootView.find(".tree-view").view() dirView = treeView.root.entries.find('.directory:contains(test-dir)').view() dirView.expand()