Manage package lifecycle in Atom instead of in RootView

This commit is contained in:
Corey Johnson & Kevin Sawicki
2013-02-08 17:19:45 -08:00
parent bb5778b659
commit 4619e1847f
6 changed files with 74 additions and 77 deletions

View File

@@ -19,9 +19,9 @@ describe "the `atom` global", ->
removeStylesheet(stylesheetPath)
it "requires and activates the package's main module if it exists", ->
spyOn(rootView, 'activatePackage').andCallThrough()
spyOn(atom, 'activateAtomPackage').andCallThrough()
atom.loadPackage("package-with-module")
expect(rootView.activatePackage).toHaveBeenCalled()
expect(atom.activateAtomPackage).toHaveBeenCalled()
it "logs warning instead of throwing an exception if a package fails to load", ->
config.set("core.disabledPackages", [])
@@ -84,3 +84,61 @@ describe "the `atom` global", ->
runs ->
expect(Worker.prototype.terminate).toHaveBeenCalled()
expect(Worker.prototype.terminate.calls.length).toBe 1
describe "package lifecycle", ->
[pack, packageModule] = []
beforeEach ->
pack =
name: "package"
packageMain:
activate: jasmine.createSpy("activate")
deactivate: ->
serialize: -> "it worked"
packageModule = pack.packageMain
describe ".activateAtomPackage(package)", ->
it "calls activate on the package", ->
atom.activateAtomPackage(pack)
expect(packageModule.activate).toHaveBeenCalledWith(undefined)
it "calls activate on the package module with its previous state", ->
atom.activateAtomPackage(pack)
packageModule.activate.reset()
serializedState = rootView.serialize()
rootView.deactivate()
RootView.deserialize(serializedState)
atom.activateAtomPackage(pack)
expect(packageModule.activate).toHaveBeenCalledWith("it worked")
describe ".deactivateAtomPackages()", ->
it "deactivates and removes the package module from the package module map", ->
atom.activateAtomPackage(pack)
spyOn(packageModule, "deactivate").andCallThrough()
atom.deactivateAtomPackages()
expect(packageModule.deactivate).toHaveBeenCalled()
expect(rootView.packages.length).toBe 0
describe ".serializeAtomPackages()", ->
it "absorbs exceptions that are thrown by the package module's serialize methods", ->
spyOn(console, 'error')
atom.activateAtomPackage
name: "bad-egg"
packageMain:
activate: ->
serialize: -> throw new Error("I'm broken")
atom.activateAtomPackage
name: "good-egg"
packageMain:
activate: ->
serialize: -> "I still get called"
packageStates = atom.serializeAtomPackages()
expect(packageStates['good-egg']).toBe "I still get called"
expect(packageStates['bad-egg']).toBeUndefined()
expect(console.error).toHaveBeenCalled()

View File

@@ -50,6 +50,14 @@ describe "RootView", ->
expect(rootView.getEditors()[0].getText()).toEqual ""
expect(rootView.getTitle()).toBe 'untitled'
describe ".deactivate()", ->
it "deactivates all packages", ->
pack = atom.loadPackage("package-with-module")
atom.activateAtomPackage(pack)
spyOn(pack.packageMain, "deactivate").andCallThrough()
rootView.deactivate()
expect(pack.packageMain.deactivate).toHaveBeenCalled()
describe "@deserialize()", ->
viewState = null
@@ -151,27 +159,6 @@ describe "RootView", ->
expect(rootView.find('.pane').length).toBe 1
expect(rootView.find('.pane').children().length).toBe 0
describe ".serialize()", ->
it "absorbs exceptions that are thrown by the package module's serialize methods", ->
spyOn(console, 'error')
rootView.activatePackage
name: "bad-egg"
packageMain:
activate: ->
serialize: -> throw new Error("I'm broken")
rootView.activatePackage
name: "good-egg"
packageMain:
activate: ->
serialize: -> "I still get called"
data = rootView.serialize()
expect(data.packageStates['good-egg']).toBe "I still get called"
expect(data.packageStates['bad-egg']).toBeUndefined()
expect(console.error).toHaveBeenCalled()
describe "focus", ->
describe "when there is an active editor", ->
it "hands off focus to the active editor", ->
@@ -420,48 +407,6 @@ describe "RootView", ->
rootView.focusNextPane()
expect(view1.focus).toHaveBeenCalled()
describe "packages", ->
[pack, packageModule] = []
beforeEach ->
pack =
name: "package"
packageMain:
configDefaults: foo: { bar: 2, baz: 3 }
activate: jasmine.createSpy("activate")
deactivate: ->
serialize: -> "it worked"
packageModule = pack.packageMain
describe ".activatePackage(name, package)", ->
it "calls activate on the package", ->
rootView.activatePackage(pack)
expect(packageModule.activate).toHaveBeenCalledWith(undefined)
it "calls activate on the package module with its previous state", ->
rootView.activatePackage(pack)
packageModule.activate.reset()
newRootView = RootView.deserialize(rootView.serialize())
newRootView.activatePackage(pack)
expect(packageModule.activate).toHaveBeenCalledWith("it worked")
newRootView.remove()
describe ".deactivatePackage(packageName)", ->
it "deactivates and removes the package module from the package module map", ->
rootView.activatePackage(pack)
spyOn(packageModule, "deactivate").andCallThrough()
rootView.deactivatePackages()
expect(packageModule.deactivate).toHaveBeenCalled()
expect(rootView.packages.length).toBe 0
it "is called when the rootView is deactivated to deactivate all packages", ->
rootView.activatePackage(pack)
spyOn(packageModule, "deactivate").andCallThrough()
rootView.deactivate()
expect(packageModule.deactivate).toHaveBeenCalled()
describe "keymap wiring", ->
commandHandler = null
beforeEach ->

View File

@@ -4,3 +4,5 @@ module.exports =
activate: ->
@activateCalled = true
deactivate: ->

View File

@@ -30,6 +30,7 @@ window.loadTextMatePackages()
beforeEach ->
window.fixturesProject = new Project(require.resolve('fixtures'))
window.resetTimeouts()
atom.atomPackageStates = {}
# used to reset keymap after each spec
bindingSetsToRestore = _.clone(keymap.bindingSets)

View File

@@ -65,7 +65,7 @@ class AtomPackage extends Package
if fs.isFile(mainPath)
@packageMain = require(mainPath)
config.setDefaults(@name, @packageMain.configDefaults)
rootView?.activatePackage(this)
atom.activateAtomPackage(this)
loadMetadata: ->
if metadataPath = fs.resolveExtension(fs.join(@path, 'package'), ['cson', 'json'])

View File

@@ -68,7 +68,7 @@ class RootView extends View
serialize: ->
projectState: @project?.serialize()
panesViewState: @panes.children().view()?.serialize()
packageStates: @serializePackages()
packageStates: atom.serializeAtomPackages()
handleFocus: (e) ->
if @getActiveEditor()
@@ -126,7 +126,7 @@ class RootView extends View
deactivate: ->
atom.setRootViewStateForPath(@project.getPath(), @serialize())
@deactivatePackages()
atom.deactivateAtomPackages()
@remove()
open: (path, options = {}) ->
@@ -261,12 +261,3 @@ class RootView extends View
eachBuffer: (callback) ->
@project.eachBuffer(callback)
activatePackage: (pack) ->
atom.activateAtomPackage(pack)
deactivatePackages: ->
atom.deactivateAtomPackages()
serializePackages: ->
atom.serializeAtomPackages()