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 42 43 44 45 46 47 48 49 50 51 52
|
"use strict";
var compose = require("es5-ext/function/#/compose")
, callable = require("es5-ext/object/valid-callable")
, d = require("d")
, validTimeout = require("timers-ext/valid-timeout");
var chars = "-\\|/", l = chars.length, ThrobberIterator;
ThrobberIterator = function () {
// no setup needed
};
Object.defineProperties(ThrobberIterator.prototype, {
index: d(-1),
running: d(false),
next: d(function () {
var str = this.running ? "\u0008" : "";
if (!this.running) this.running = true;
return str + chars[(this.index = (this.index + 1) % l)];
}),
reset: d(function () {
if (!this.running) return "";
this.index = -1;
this.running = false;
return "\u0008";
})
});
module.exports = exports = function (write, interval /*, format*/) {
var format = arguments[2], token, iterator = new ThrobberIterator();
callable(write);
interval = validTimeout(interval);
if (format !== undefined) write = compose.call(write, callable(format));
return {
start: function () {
if (token) return;
token = setInterval(function () { write(iterator.next()); }, interval);
},
restart: function () {
this.stop();
this.start();
},
stop: function () {
if (!token) return;
clearInterval(token);
token = null;
write(iterator.reset());
}
};
};
Object.defineProperty(exports, "Iterator", d(ThrobberIterator));
|