Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13372 from atom/as-fix-package-activation-hook
Browse files Browse the repository at this point in the history
Activate packages immediately if hook had already been triggered
  • Loading branch information
Antonio Scandurra committed Dec 5, 2016
2 parents 2139f28 + e943f7a commit 7f4b430
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 13 additions & 3 deletions spec/package-manager-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,9 @@ describe "PackageManager", ->
spyOn(mainModule, 'activate').andCallThrough()
spyOn(Package.prototype, 'requireMainModule').andCallThrough()

promise = atom.packages.activatePackage('package-with-activation-hooks')

it "defers requiring/activating the main module until an triggering of an activation hook occurs", ->
promise = atom.packages.activatePackage('package-with-activation-hooks')
expect(Package.prototype.requireMainModule.callCount).toBe 0

atom.packages.triggerActivationHook('language-fictitious:grammar-used')
atom.packages.triggerDeferredActivationHooks()

Expand All @@ -458,6 +456,7 @@ describe "PackageManager", ->
expect(Package.prototype.requireMainModule.callCount).toBe 1

it "does not double register activation hooks when deactivating and reactivating", ->
promise = atom.packages.activatePackage('package-with-activation-hooks')
expect(mainModule.activate.callCount).toBe 0
atom.packages.triggerActivationHook('language-fictitious:grammar-used')
atom.packages.triggerDeferredActivationHooks()
Expand Down Expand Up @@ -492,6 +491,17 @@ describe "PackageManager", ->
expect(mainModule.activate.callCount).toBe 1
expect(Package.prototype.requireMainModule.callCount).toBe 1

it "activates the package immediately if the activation hook had already been triggered", ->
atom.packages.triggerActivationHook('language-fictitious:grammar-used')
atom.packages.triggerDeferredActivationHooks()
expect(Package.prototype.requireMainModule.callCount).toBe 0

waitsForPromise ->
atom.packages.activatePackage('package-with-activation-hooks')

runs ->
expect(Package.prototype.requireMainModule.callCount).toBe 1

describe "when the package has no main module", ->
it "does not throw an exception", ->
spyOn(console, "error")
Expand Down
10 changes: 9 additions & 1 deletion src/package-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PackageManager
@activationHookEmitter = new Emitter
@packageDirPaths = []
@deferredActivationHooks = []
@triggeredActivationHooks = new Set()
if configDirPath? and not safeMode
if @devMode
@packageDirPaths.push(path.join(configDirPath, "dev", "packages"))
Expand Down Expand Up @@ -67,6 +68,7 @@ class PackageManager
@deactivatePackages()
@loadedPackages = {}
@packageStates = {}
@triggeredActivationHooks.clear()

###
Section: Event Subscription
Expand Down Expand Up @@ -460,12 +462,17 @@ class PackageManager
Promise.resolve(pack)
else if pack = @loadPackage(name)
@activatingPackages[pack.name] = pack
pack.activate().then =>
activationPromise = pack.activate().then =>
if @activatingPackages[pack.name]?
delete @activatingPackages[pack.name]
@activePackages[pack.name] = pack
@emitter.emit 'did-activate-package', pack
pack

unless @deferredActivationHooks?
@triggeredActivationHooks.forEach((hook) => @activationHookEmitter.emit(hook))

activationPromise
else
Promise.reject(new Error("Failed to load package '#{name}'"))

Expand All @@ -476,6 +483,7 @@ class PackageManager

triggerActivationHook: (hook) ->
return new Error("Cannot trigger an empty activation hook") unless hook? and _.isString(hook) and hook.length > 0
@triggeredActivationHooks.add(hook)
if @deferredActivationHooks?
@deferredActivationHooks.push hook
else
Expand Down
2 changes: 2 additions & 0 deletions src/package.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class Package
@mainModule.activate?(@packageManager.getPackageState(@name) ? {})
@mainActivated = true
@activateServices()
@activationCommandSubscriptions?.dispose()
@activationHookSubscriptions?.dispose()
catch error
@handleError("Failed to activate the #{@name} package", error)

Expand Down

0 comments on commit 7f4b430

Please sign in to comment.