From fe5640df4b2e00ce3965a898da2c1a190faec594 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 7 Feb 2014 15:22:58 -0800 Subject: [PATCH] Return promise if it already exists This prevents successive calls to atom.packages.activatePackage from activating an AtomPackage multiple times. --- spec/atom-spec.coffee | 14 +++++++++++++- src/atom-package.coffee | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/atom-spec.coffee b/spec/atom-spec.coffee index 8416a4ff2..9e37e2ac6 100644 --- a/spec/atom-spec.coffee +++ b/spec/atom-spec.coffee @@ -1,6 +1,7 @@ {$, $$, fs, WorkspaceView} = require 'atom' Exec = require('child_process').exec path = require 'path' +AtomPackage = require '../src/atom-package' ThemeManager = require '../src/theme-manager' describe "the `atom` global", -> @@ -58,6 +59,18 @@ describe "the `atom` global", -> describe ".activatePackage(id)", -> describe "atom packages", -> + describe "when called multiple times", -> + it "it only calls activate on the package once", -> + spyOn(AtomPackage.prototype, 'activateNow').andCallThrough() + atom.packages.activatePackage('package-with-index') + atom.packages.activatePackage('package-with-index') + + waitsForPromise -> + atom.packages.activatePackage('package-with-index') + + runs -> + expect(AtomPackage.prototype.activateNow.callCount).toBe 1 + describe "when the package has a main module", -> describe "when the metadata specifies a main module path˜", -> it "requires the module at the specified path", -> @@ -100,7 +113,6 @@ describe "the `atom` global", -> promise = atom.packages.activatePackage('package-with-activation-events') - it "defers requiring/activating the main module until an activation event bubbles to the root view", -> expect(promise.isFulfilled()).not.toBeTruthy() atom.workspaceView.trigger 'activation-event' diff --git a/src/atom-package.coffee b/src/atom-package.coffee index 3d92aed83..b37e5d66d 100644 --- a/src/atom-package.coffee +++ b/src/atom-package.coffee @@ -61,6 +61,8 @@ class AtomPackage extends Package @scopedProperties = [] activate: ({immediate}={}) -> + return @activationDeferred.promise if @activationDeferred? + @activationDeferred = Q.defer() @measure 'activateTime', => @activateResources()