mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
Activate packages immediately if hook had already been triggered
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
@@ -492,6 +491,16 @@ 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()
|
||||
|
||||
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")
|
||||
|
||||
@@ -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"))
|
||||
@@ -67,6 +68,7 @@ class PackageManager
|
||||
@deactivatePackages()
|
||||
@loadedPackages = {}
|
||||
@packageStates = {}
|
||||
@triggeredActivationHooks.clear()
|
||||
|
||||
###
|
||||
Section: Event Subscription
|
||||
@@ -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}'"))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user