pulse-till-done.js 783 B

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