Don't serialize packages unless the main module was activated

The settings-view activates the config for each package before it renders
causing the main module to be required in order to call activateConfig().
This was causing serialize to be called when the window state was being
saved which was incorrect since activate hadn't actually been called even
though the main module was required.
This commit is contained in:
Kevin Sawicki
2013-08-29 14:44:00 -07:00
parent 19b5f3e202
commit fd9f3d6543
3 changed files with 32 additions and 8 deletions

View File

@@ -219,7 +219,7 @@ describe "the `atom` global", ->
describe ".deactivatePackage(id)", ->
describe "atom packages", ->
it "calls `deactivate` on the package's main module", ->
it "calls `deactivate` on the package's main module if activate was successful", ->
pack = atom.activatePackage("package-with-deactivate")
expect(atom.isPackageActive("package-with-deactivate")).toBeTruthy()
spyOn(pack.mainModule, 'deactivate').andCallThrough()
@@ -228,6 +228,23 @@ describe "the `atom` global", ->
expect(pack.mainModule.deactivate).toHaveBeenCalled()
expect(atom.isPackageActive("package-with-module")).toBeFalsy()
spyOn(console, 'warn')
badPack = atom.activatePackage("package-that-throws-on-activate")
expect(atom.isPackageActive("package-that-throws-on-activate")).toBeTruthy()
spyOn(badPack.mainModule, 'deactivate').andCallThrough()
atom.deactivatePackage("package-that-throws-on-activate")
expect(badPack.mainModule.deactivate).not.toHaveBeenCalled()
expect(atom.isPackageActive("package-that-throws-on-activate")).toBeFalsy()
it "does not serialize packages that have not been activated called on their main module", ->
spyOn(console, 'warn')
badPack = atom.activatePackage("package-that-throws-on-activate")
spyOn(badPack.mainModule, 'serialize').andCallThrough()
atom.deactivatePackage("package-that-throws-on-activate")
expect(badPack.mainModule.serialize).not.toHaveBeenCalled()
it "absorbs exceptions that are thrown by the package module's serialize methods", ->
spyOn(console, 'error')
atom.activatePackage('package-with-serialize-error', immediate: true)
@@ -265,7 +282,7 @@ describe "the `atom` global", ->
atom.deactivatePackage("package-with-scoped-properties")
expect(syntax.getProperty ['.source.omg'], 'editor.increaseIndentPattern').toBeUndefined()
describe "texmate packages", ->
describe "textmate packages", ->
it "removes the package's grammars", ->
expect(syntax.selectGrammar("file.rb").name).toBe "Null Grammar"
atom.activatePackage('ruby-tmbundle', sync: true)

View File

@@ -0,0 +1,4 @@
module.exports =
activate: -> throw new Error('Top that')
deactivate: ->
serialize: ->

View File

@@ -44,7 +44,9 @@ class AtomPackage extends Package
activateNow: ->
try
@activateConfig()
@mainModule.activate(atom.getPackageState(@name) ? {}) if @requireMainModule()
if @requireMainModule()
@mainModule.activate(atom.getPackageState(@name) ? {})
@mainActivated = true
catch e
console.warn "Failed to activate package named '#{@name}'", e.stack
@@ -98,16 +100,17 @@ class AtomPackage extends Package
@scopedProperties.push([scopedPropertiesPath, selector, properties])
serialize: ->
try
@mainModule?.serialize?()
catch e
console.error "Error serializing package '#{@name}'", e.stack
if @mainActivated
try
@mainModule?.serialize?()
catch e
console.error "Error serializing package '#{@name}'", e.stack
deactivate: ->
@unsubscribeFromActivationEvents()
@deactivateResources()
@deactivateConfig()
@mainModule?.deactivate?()
@mainModule?.deactivate?() if @mainActivated
deactivateConfig: ->
@mainModule?.deactivateConfig?()