Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pulse-till-done.js « utils « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13147bae1661370561ccff3f8003f12a203ac3a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const log = require('npmlog')

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) {
  if (!prefix)
    prefix = 'network'
  pulseStart(prefix)
  return (er, ...args) => {
    pulseStop()
    cb(er, ...args)
  }
}

const pulseWhile = async (prefix, promise) => {
  if (!promise) {
    promise = prefix
    prefix = ''
  }
  pulseStart(prefix)
  try {
    return await promise
  } finally {
    pulseStop()
  }
}
module.exports.withPromise = pulseWhile