mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
🎨 Introduce Config::transactAsync
This commit is contained in:
@@ -677,6 +677,33 @@ class Config
|
||||
finally
|
||||
@endTransaction()
|
||||
|
||||
# Extended: Suppress calls to handler functions registered with
|
||||
# {::onDidChange} and {::observe} for the duration of the {Promise} returned
|
||||
# by `callback`. After the {Promise} is either resolved or rejected, handlers
|
||||
# will be called once if the value for their key-path has changed.
|
||||
#
|
||||
# * `callback` {Function} that returns a {Promise}, which will be executed
|
||||
# while suppressing calls to handlers.
|
||||
#
|
||||
# Returns a {Promise} that is either resolved or rejected according to the
|
||||
# `{Promise}` returned by `callback`. If `callback` throws an error, a
|
||||
# rejected {Promise} will be returned.
|
||||
transactAsync: (callback) ->
|
||||
@beginTransaction()
|
||||
endTransaction = (resolveOrReject) => (args...) =>
|
||||
@endTransaction()
|
||||
resolveOrReject(args...)
|
||||
|
||||
try
|
||||
result = callback()
|
||||
new Promise (resolve, reject) =>
|
||||
result
|
||||
.then(endTransaction(resolve))
|
||||
.catch(endTransaction(reject))
|
||||
catch error
|
||||
@endTransaction()
|
||||
Promise.reject(error)
|
||||
|
||||
beginTransaction: ->
|
||||
@transactDepth++
|
||||
|
||||
|
||||
@@ -418,13 +418,11 @@ class PackageManager
|
||||
|
||||
activatePackages: (packages) ->
|
||||
promises = []
|
||||
@config.beginTransaction()
|
||||
for pack in packages
|
||||
promise = @activatePackage(pack.name)
|
||||
promises.push(promise) unless pack.activationShouldBeDeferred()
|
||||
Promise.all(promises)
|
||||
.then(=> @config.endTransaction())
|
||||
.catch(=> @config.endTransaction())
|
||||
@config.transactAsync =>
|
||||
for pack in packages
|
||||
promise = @activatePackage(pack.name)
|
||||
promises.push(promise) unless pack.activationShouldBeDeferred()
|
||||
Promise.all(promises)
|
||||
@observeDisabledPackages()
|
||||
@observePackagesWithKeymapsDisabled()
|
||||
promises
|
||||
|
||||
Reference in New Issue
Block a user