Debian Codegraph @

cli-color/throbber.js

raw
1 "use strict";
2
3 var compose = require("es5-ext/function/#/compose")
4 , callable = require("es5-ext/object/valid-callable")
5 , d = require("d")
6 , validTimeout = require("timers-ext/valid-timeout");
7
8 var chars = "-\\|/", l = chars.length, ThrobberIterator;
9
10 ThrobberIterator = function () {
11 // no setup needed
12 };
13 Object.defineProperties(ThrobberIterator.prototype, {
14 index: d(-1),
15 running: d(false),
16 next: d(function () {
17 var str = this.running ? "\u0008" : "";
18 if (!this.running) this.running = true;
19 return str + chars[(this.index = (this.index + 1) % l)];
20 }),
21 reset: d(function () {
22 if (!this.running) return "";
23 this.index = -1;
24 this.running = false;
25 return "\u0008";
26 })
27 });
28
29 module.exports = exports = function (write, interval /*, format*/) {
30 var format = arguments[2], token, iterator = new ThrobberIterator();
31 callable(write);
32 interval = validTimeout(interval);
33 if (format !== undefined) write = compose.call(write, callable(format));
34 return {
35 start: function () {
36 if (token) return;
37 token = setInterval(function () { write(iterator.next()); }, interval);
38 },
39 restart: function () {
40 this.stop();
41 this.start();
42 },
43 stop: function () {
44 if (!token) return;
45 clearInterval(token);
46 token = null;
47 write(iterator.reset());
48 }
49 };
50 };
51
52 Object.defineProperty(exports, "Iterator", d(ThrobberIterator));