• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const validate = require('aproba')
3const log = require('npmlog')
4const Bluebird = require('bluebird')
5
6let pulsers = 0
7let pulse
8
9function pulseStart (prefix) {
10  if (++pulsers > 1) return
11  pulse = setInterval(function () {
12    log.gauge.pulse(prefix)
13  }, 150)
14}
15function pulseStop () {
16  if (--pulsers > 0) return
17  clearInterval(pulse)
18}
19
20module.exports = function (prefix, cb) {
21  validate('SF', [prefix, cb])
22  if (!prefix) prefix = 'network'
23  pulseStart(prefix)
24  return function () {
25    pulseStop()
26    cb.apply(null, arguments)
27  }
28}
29module.exports.withPromise = pulseWhile
30
31function pulseWhile (prefix, promise) {
32  if (!promise) {
33    promise = prefix
34    prefix = ''
35  }
36  pulseStart(prefix)
37  return Bluebird.resolve(promise).finally(() => pulseStop())
38}
39