Use previous package state when the wasn't activated

This commit is contained in:
Corey Johnson 2013-02-13 16:32:50 -08:00
parent 958bc2bfab
commit 8375c8df38
6 changed files with 65 additions and 54 deletions

View File

@ -86,60 +86,53 @@ describe "the `atom` global", ->
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({})
it "calls activate on the package module with its previous state", ->
atom.activateAtomPackage(pack)
packageModule.activate.reset()
describe "activation", ->
it "calls activate on the package main with its previous state", ->
pack = window.loadPackage('package-with-module')
spyOn(pack.packageMain, 'activate')
serializedState = rootView.serialize()
rootView.deactivate()
RootView.deserialize(serializedState)
window.loadPackage('package-with-module')
atom.activateAtomPackage(pack)
expect(packageModule.activate).toHaveBeenCalledWith("it worked")
expect(pack.packageMain.activate).toHaveBeenCalledWith(someNumber: 1)
describe ".deactivateAtomPackages()", ->
describe "deactivation", ->
it "deactivates and removes the package module from the package module map", ->
atom.activateAtomPackage(pack)
pack = window.loadPackage('package-with-module')
expect(atom.activatedAtomPackages.length).toBe 1
spyOn(packageModule, "deactivate").andCallThrough()
spyOn(pack.packageMain, "deactivate").andCallThrough()
atom.deactivateAtomPackages()
expect(packageModule.deactivate).toHaveBeenCalled()
expect(pack.packageMain.deactivate).toHaveBeenCalled()
expect(atom.activatedAtomPackages.length).toBe 0
describe ".serializeAtomPackages()", ->
describe "serialization", ->
it "uses previous serialization state on unactivated packages", ->
atom.atomPackageStates['package-with-activation-events'] = {previousData: 'exists'}
unactivatedPackage = window.loadPackage('package-with-activation-events')
activatedPackage = window.loadPackage('package-with-module')
expect(atom.serializeAtomPackages()).toEqual
'package-with-module':
'someNumber': 1
'package-with-activation-events':
'previousData': 'exists'
# ensure serialization occurs when the packageis activated
unactivatedPackage.activatePackageMain()
expect(atom.serializeAtomPackages()).toEqual
'package-with-module':
'someNumber': 1
'package-with-activation-events':
'previousData': 'overwritten'
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"
window.loadPackage('package-with-module')
window.loadPackage('package-with-serialize-error', activateImmediately: true)
packageStates = atom.serializeAtomPackages()
expect(packageStates['good-egg']).toBe "I still get called"
expect(packageStates['bad-egg']).toBeUndefined()
expect(packageStates['package-with-module']).toEqual someNumber: 1
expect(packageStates['package-with-serialize-error']).toBeUndefined()
expect(console.error).toHaveBeenCalled()

View File

@ -4,3 +4,6 @@ module.exports =
activate: ->
rootView.getActiveEditor()?.command 'activation-event', =>
@activationEventCallCount++
serialize: ->
previousData: 'overwritten'

View File

@ -2,7 +2,12 @@ module.exports =
configDefaults:
numbers: { one: 1, two: 2 }
someNumber: 0
activate: ->
@activateCalled = true
@someNumber = 1
deactivate: ->
serialize: ->
{@someNumber}

View File

@ -0,0 +1,7 @@
module.exports =
activate: ->
deactivate: ->
serialize: ->
throw new Error("I'm no good at this.")

View File

@ -0,0 +1,5 @@
# This package loads async, otherwise it would log errors when it
# is automatically serialized when rootView is deactivatated
'main': 'index.coffee'
'activationEvents': ['activation-event']

View File

@ -26,18 +26,16 @@ _.extend atom,
serializeAtomPackages: ->
packageStates = {}
for pack in @activatedAtomPackages
for pack in @loadedPackages
if pack in @activatedAtomPackages
try
packageStates[pack.name] = pack.packageMain.serialize?()
catch e
console?.error("Exception serializing '#{pack.name}' package's module\n", e.stack)
else
packageStates[pack.name] = @atomPackageStates[pack.name]
packageStates
loadPackage: (name, options) ->
packagePath = _.find @getPackagePaths(), (packagePath) -> fs.base(packagePath) == name
pack = Package.build(packagePath)
pack?.load(options)
loadPackages: ->
textMatePackages = []
for path in @getPackagePaths()