diff options
Diffstat (limited to 'deps/npm/lib/utils/pulse-till-done.js')
-rw-r--r-- | deps/npm/lib/utils/pulse-till-done.js | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/deps/npm/lib/utils/pulse-till-done.js b/deps/npm/lib/utils/pulse-till-done.js index 2669241306..b292c2fa56 100644 --- a/deps/npm/lib/utils/pulse-till-done.js +++ b/deps/npm/lib/utils/pulse-till-done.js @@ -1,22 +1,38 @@ 'use strict' -var validate = require('aproba') -var log = require('npmlog') +const validate = require('aproba') +const log = require('npmlog') +const Bluebird = require('bluebird') -var pulsers = 0 -var pulse +let pulsers = 0 +let pulse + +function pulseStart (prefix) { + if (++pulsers > 1) return + pulse = setInterval(function () { + log.gauge.pulse(prefix) + }, 150) +} +function pulseStop () { + if (--pulsers > 0) return + clearInterval(pulse) +} module.exports = function (prefix, cb) { validate('SF', [prefix, cb]) if (!prefix) prefix = 'network' - if (!pulsers++) { - pulse = setInterval(function () { - log.gauge.pulse(prefix) - }, 250) - } + pulseStart(prefix) return function () { - if (!--pulsers) { - clearInterval(pulse) - } + pulseStop() cb.apply(null, arguments) } } +module.exports.withPromise = pulseWhile + +function pulseWhile (prefix, promise) { + if (!promise) { + promise = prefix + prefix = '' + } + pulseStart(prefix) + return Bluebird.resolve(promise).finally(() => pulseStop()) +} |