Remove Q from PackageManager, ThemeManager, Package, ThemePackage

Signed-off-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo
2015-08-06 12:21:08 -06:00
parent e835bc0777
commit 4a7cbff6d3
4 changed files with 73 additions and 77 deletions

View File

@@ -213,12 +213,16 @@ describe "PackageManager", ->
mainModule = null
it "defers requiring/activating the main module until an activation event bubbles to the root view", ->
expect(promise.isFulfilled()).not.toBeTruthy()
expect(Package.prototype.requireMainModule.callCount).toBe 0
workspaceElement.dispatchEvent(new CustomEvent('activation-command', bubbles: true))
waitsForPromise ->
promise
runs ->
expect(Package.prototype.requireMainModule.callCount).toBe 1
it "triggers the activation event on all handlers registered during activation", ->
waitsForPromise ->
atom.workspace.open()
@@ -299,8 +303,8 @@ describe "PackageManager", ->
promise = atom.packages.activatePackage('package-with-activation-hooks')
it "defers requiring/activating the main module until an triggering of an activation hook occurs", ->
expect(promise.isFulfilled()).not.toBeTruthy()
expect(Package.prototype.requireMainModule.callCount).toBe 0
atom.packages.triggerActivationHook('language-fictitious:grammar-used')
waitsForPromise ->
@@ -889,7 +893,7 @@ describe "PackageManager", ->
# enabling of theme
pack = atom.packages.enablePackage(packageName)
waitsFor ->
waitsFor 'theme to enable', 500, ->
pack in atom.packages.getActivePackages()
runs ->
@@ -902,7 +906,7 @@ describe "PackageManager", ->
pack = atom.packages.disablePackage(packageName)
waitsFor ->
waitsFor 'did-change-active-themes event to fire', 500, ->
didChangeActiveThemesHandler.callCount is 1
runs ->

View File

@@ -6,7 +6,6 @@ async = require 'async'
CSON = require 'season'
fs = require 'fs-plus'
{Emitter, CompositeDisposable} = require 'event-kit'
Q = require 'q'
{includeDeprecatedAPIs, deprecate} = require 'grim'
ModuleCache = require './module-cache'
@@ -139,20 +138,21 @@ class Package
activate: ->
@grammarsPromise ?= @loadGrammars()
@activationPromise ?=
new Promise (resolve, reject) =>
@resolveActivationPromise = resolve
@rejectActivationPromise = reject
@measure 'activateTime', =>
try
@activateResources()
if @activationShouldBeDeferred()
@subscribeToDeferredActivation()
else
@activateNow()
catch error
@handleError("Failed to activate the #{@name} package", error)
unless @activationDeferred?
@activationDeferred = Q.defer()
@measure 'activateTime', =>
try
@activateResources()
if @activationShouldBeDeferred()
@subscribeToDeferredActivation()
else
@activateNow()
catch error
@handleError("Failed to activate the #{@name} package", error)
Q.all([@grammarsPromise, @settingsPromise, @activationDeferred.promise])
Promise.all([@grammarsPromise, @settingsPromise, @activationPromise])
activateNow: ->
try
@@ -165,7 +165,7 @@ class Package
catch error
@handleError("Failed to activate the #{@name} package", error)
@activationDeferred?.resolve()
@resolveActivationPromise?()
activateConfig: ->
return if @configActivated
@@ -345,7 +345,7 @@ class Package
@grammarsActivated = true
loadGrammars: ->
return Q() if @grammarsLoaded
return Promise.resolve() if @grammarsLoaded
loadGrammar = (grammarPath, callback) =>
atom.grammars.readGrammar grammarPath, (error, grammar) =>
@@ -360,14 +360,13 @@ class Package
grammar.activate() if @grammarsActivated
callback()
deferred = Q.defer()
grammarsDirPath = path.join(@path, 'grammars')
fs.exists grammarsDirPath, (grammarsDirExists) ->
return deferred.resolve() unless grammarsDirExists
new Promise (resolve) =>
grammarsDirPath = path.join(@path, 'grammars')
fs.exists grammarsDirPath, (grammarsDirExists) ->
return resolve() unless grammarsDirExists
fs.list grammarsDirPath, ['json', 'cson'], (error, grammarPaths=[]) ->
async.each grammarPaths, loadGrammar, -> deferred.resolve()
deferred.promise
fs.list grammarsDirPath, ['json', 'cson'], (error, grammarPaths=[]) ->
async.each grammarPaths, loadGrammar, -> resolve()
loadSettings: ->
@settings = []
@@ -383,20 +382,18 @@ class Package
settings.activate() if @settingsActivated
callback()
deferred = Q.defer()
new Promise (resolve) =>
if fs.isDirectorySync(path.join(@path, 'scoped-properties'))
settingsDirPath = path.join(@path, 'scoped-properties')
deprecate("Store package settings files in the `settings/` directory instead of `scoped-properties/`", packageName: @name)
else
settingsDirPath = path.join(@path, 'settings')
if fs.isDirectorySync(path.join(@path, 'scoped-properties'))
settingsDirPath = path.join(@path, 'scoped-properties')
deprecate("Store package settings files in the `settings/` directory instead of `scoped-properties/`", packageName: @name)
else
settingsDirPath = path.join(@path, 'settings')
fs.exists settingsDirPath, (settingsDirExists) ->
return resolve() unless settingsDirExists
fs.exists settingsDirPath, (settingsDirExists) ->
return deferred.resolve() unless settingsDirExists
fs.list settingsDirPath, ['json', 'cson'], (error, settingsPaths=[]) ->
async.each settingsPaths, loadSettingsFile, -> deferred.resolve()
deferred.promise
fs.list settingsDirPath, ['json', 'cson'], (error, settingsPaths=[]) ->
async.each settingsPaths, loadSettingsFile, -> resolve()
serialize: ->
if @mainActivated
@@ -406,8 +403,10 @@ class Package
console.error "Error serializing package '#{@name}'", e.stack
deactivate: ->
@activationDeferred?.reject()
@activationDeferred = null
@rejectActivationPromise?()
@activationPromise = null
@resolveActivationPromise = null
@rejectActivationPromise = null
@activationCommandSubscriptions?.dispose()
@deactivateResources()
@deactivateConfig()

View File

@@ -3,7 +3,6 @@ _ = require 'underscore-plus'
{Emitter, Disposable, CompositeDisposable} = require 'event-kit'
{File} = require 'pathwatcher'
fs = require 'fs-plus'
Q = require 'q'
Grim = require 'grim'
# Extended: Handles loading and activating available themes.
@@ -260,32 +259,29 @@ class ThemeManager
string.replace(/\\/g, '/')
activateThemes: ->
deferred = Q.defer()
new Promise (resolve) =>
# atom.config.observe runs the callback once, then on subsequent changes.
atom.config.observe 'core.themes', =>
@deactivateThemes()
# atom.config.observe runs the callback once, then on subsequent changes.
atom.config.observe 'core.themes', =>
@deactivateThemes()
@refreshLessCache() # Update cache for packages in core.themes config
@refreshLessCache() # Update cache for packages in core.themes config
promises = []
for themeName in @getEnabledThemeNames()
if @packageManager.resolvePackagePath(themeName)
promises.push(@packageManager.activatePackage(themeName))
else
console.warn("Failed to activate theme '#{themeName}' because it isn't installed.")
promises = []
for themeName in @getEnabledThemeNames()
if @packageManager.resolvePackagePath(themeName)
promises.push(@packageManager.activatePackage(themeName))
else
console.warn("Failed to activate theme '#{themeName}' because it isn't installed.")
Q.all(promises).then =>
@addActiveThemeClasses()
@refreshLessCache() # Update cache again now that @getActiveThemes() is populated
@loadUserStylesheet()
@reloadBaseStylesheets()
@initialLoadComplete = true
@emit 'reloaded' if Grim.includeDeprecatedAPIs
@emitter.emit 'did-change-active-themes'
deferred.resolve()
deferred.promise
Promise.all(promises).then =>
@addActiveThemeClasses()
@refreshLessCache() # Update cache again now that @getActiveThemes() is populated
@loadUserStylesheet()
@reloadBaseStylesheets()
@initialLoadComplete = true
@emit 'reloaded' if Grim.includeDeprecatedAPIs
@emitter.emit 'did-change-active-themes'
resolve()
deactivateThemes: ->
@removeActiveThemeClasses()

View File

@@ -1,4 +1,3 @@
Q = require 'q'
Package = require './package'
module.exports =
@@ -18,14 +17,12 @@ class ThemePackage extends Package
this
activate: ->
return @activationDeferred.promise if @activationDeferred?
@activationDeferred = Q.defer()
@measure 'activateTime', =>
try
@loadStylesheets()
@activateNow()
catch error
@handleError("Failed to activate the #{@name} theme", error)
@activationDeferred.promise
@activationPromise ?= new Promise (resolve, reject) =>
@resolveActivationPromise = resolve
@rejectActivationPromise = reject
@measure 'activateTime', =>
try
@loadStylesheets()
@activateNow()
catch error
@handleError("Failed to activate the #{@name} theme", error)