Skip to content

Commit

Permalink
rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeghe committed Apr 13, 2023
1 parent ea8424f commit d73e527
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 116 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![Coverage Status](https://coveralls.io/repos/github/fedeghe/countdown/badge.svg?branch=master)](https://coveralls.io/github/fedeghe/countdown?branch=master)

# countdown <sub><small>(v. 0.0.13)</small></sub>
# countdown <sub><small>(v. 0.0.14)</small></sub>

A really simple function to provide and extended isomorphic version of the native `setTimeout` which can be
A really simple function to provide and extended version of the native `setTimeout` which can be
- paused / resumed
- updated while running
- have a ticking function
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# countdown <sub><small>(v. $PACKAGE.version$)</small></sub>

A really simple function to provide and extended isomorphic version of the native `setTimeout` which can be
A really simple function to provide and extended version of the native `setTimeout` which can be
- paused / resumed
- updated while running
- have a ticking function
Expand Down
224 changes: 112 additions & 112 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,125 @@
var countdown = (function () {
maltaF('./../node_modules/@fedeghe/interval/dist/index.js');
var isFunction = function (f) { return typeof f === 'function'; };
function Countdown (fn, horizont) {
this.fn = fn;
this.horizont = horizont;
this.baseHorizont = horizont;
this.to = null;
this.active = false;
this.paused = false;
this._onErr = null;
this._onEnd = null;
this._onTick = null;
this.tick = null;
this.ticker = null;
this.startPause = 0;
this.pauseSpan = 0;
this.updates = 0;
var interval = require('@fedeghe/interval'),
countdown = (function () {
var isFunction = function (f) { return typeof f === 'function'; };
function Countdown (fn, horizont) {
this.fn = fn;
this.horizont = horizont;
this.baseHorizont = horizont;
this.to = null;
this.active = false;
this.paused = false;
this._onErr = null;
this._onEnd = null;
this._onTick = null;
this.tick = null;
this.ticker = null;
this.startPause = 0;
this.pauseSpan = 0;
this.updates = 0;

this._onUpdate = null;
this._update = null;
this._onPause = null;
this._pause = null;
this._onResume = null;
this._resume = null;
}

Countdown.prototype.end = function () {
this.active = false;
this._onEnd && this._onEnd();
this.ticker && this.ticker.end();
clearTimeout(this.to);
return this;
};

Countdown.prototype.onEnd = function (f) { if (isFunction(f)) { this._onEnd = f; } return this; };
this._onUpdate = null;
this._update = null;
this._onPause = null;
this._pause = null;
this._onResume = null;
this._resume = null;
}

Countdown.prototype.onErr = function (f) { if (isFunction(f)) { this._onErr = f; } return this; };
Countdown.prototype.end = function () {
this.active = false;
this._onEnd && this._onEnd();
this.ticker && this.ticker.end();
clearTimeout(this.to);
return this;
};

Countdown.prototype.onPause = function (f) { if (isFunction(f)) { this._onPause = f; } return this; };
Countdown.prototype.onEnd = function (f) { if (isFunction(f)) { this._onEnd = f; } return this; };

Countdown.prototype.onResume = function (f) { if (isFunction(f)) { this._onResume = f; } return this; };
Countdown.prototype.onErr = function (f) { if (isFunction(f)) { this._onErr = f; } return this; };

Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = f; } return this; };
Countdown.prototype.onPause = function (f) { if (isFunction(f)) { this._onPause = f; } return this; };

Countdown.prototype.pause = function () {
this.paused = true;
this._onPause && this._onPause(this);
this.to && clearTimeout(this.to);
this.startPause = +new Date();
this.ticker && this.ticker.pause();
return this;
};
Countdown.prototype.onResume = function (f) { if (isFunction(f)) { this._onResume = f; } return this; };

Countdown.prototype.resume = function () {
this.paused = false;
this._onResume && this._onResume(this);
// reset horizont removing the pause
this.pauseSpan = +new Date() - this.startPause;
this.horizont -= this.pauseSpan;
this.run(false, true);
this.ticker && this.ticker.resume();
return this;
};
Countdown.prototype.getStatus = function () {
var now = +new Date(),
elapsed = now - this.startTime - this.pauseSpan,
remaining = this.horizont - elapsed + this.updates,
progress = 100 * elapsed / this.horizont;
return { elapsed: elapsed, remaining: remaining, progress: progress };
};
Countdown.prototype.run = function (onStart, avoid) {
var self = this;
this.startTime = this.startTime || +new Date();
this.active = true;
onStart && onStart(self);
this.to = setTimeout(function () {
try {
self.fn();
self.end();
self.ticker && self.ticker.end();
} catch (e) {
self._onErr &&
self._onErr(e, self);
self.active = false;
}
}, this.horizont);
!avoid && this.ticker && this.ticker.run();
return this;
};
Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = f; } return this; };

Countdown.prototype.update = function (amount) {
var am = parseInt(amount),
now = +new Date(),
elapsed = now - this.startTime - this.pauseSpan,
remaining = this.baseHorizont - elapsed,
newHorizont = am + remaining;
this.updates = am;
this.baseHorizont = elapsed + newHorizont;
if (newHorizont && newHorizont > 0) {
this._onUpdate && this._onUpdate(this);
this.horizont = newHorizont;
Countdown.prototype.pause = function () {
this.paused = true;
this._onPause && this._onPause(this);
this.to && clearTimeout(this.to);
this.run();
}
return this;
};
this.startPause = +new Date();
this.ticker && this.ticker.pause();
return this;
};

Countdown.prototype.onTick = function (fn, tick) {
var self = this;
this.ticker = interval(function (cycle) {
Countdown.prototype.resume = function () {
this.paused = false;
this._onResume && this._onResume(this);
// reset horizont removing the pause
this.pauseSpan = +new Date() - this.startPause;
this.horizont -= this.pauseSpan;
this.run(false, true);
this.ticker && this.ticker.resume();
return this;
};
Countdown.prototype.getStatus = function () {
var now = +new Date(),
elapsed = now - self.startTime - self.pauseSpan,
remaining = self.baseHorizont - elapsed,
progress = parseFloat((100 * elapsed / self.baseHorizont).toFixed(3), 10);
fn({ cycle: cycle, elapsed: elapsed, remaining: remaining, progress: progress });
}, tick);
return this;
};
elapsed = now - this.startTime - this.pauseSpan,
remaining = this.horizont - elapsed + this.updates,
progress = 100 * elapsed / this.horizont;
return { elapsed: elapsed, remaining: remaining, progress: progress };
};
Countdown.prototype.run = function (onStart, avoid) {
var self = this;
this.startTime = this.startTime || +new Date();
this.active = true;
onStart && onStart(self);
this.to = setTimeout(function () {
try {
self.fn();
self.end();
self.ticker && self.ticker.end();
} catch (e) {
self._onErr &&
self._onErr(e, self);
self.active = false;
}
}, this.horizont);
!avoid && this.ticker && this.ticker.run();
return this;
};

Countdown.prototype.update = function (amount) {
var am = parseInt(amount),
now = +new Date(),
elapsed = now - this.startTime - this.pauseSpan,
remaining = this.baseHorizont - elapsed,
newHorizont = am + remaining;
this.updates = am;
this.baseHorizont = elapsed + newHorizont;
if (newHorizont && newHorizont > 0) {
this._onUpdate && this._onUpdate(this);
this.horizont = newHorizont;
this.to && clearTimeout(this.to);
this.run();
}
return this;
};

Countdown.prototype.onTick = function (fn, tick) {
var self = this;
this.ticker = interval(function (cycle) {
var now = +new Date(),
elapsed = now - self.startTime - self.pauseSpan,
remaining = self.baseHorizont - elapsed,
progress = parseFloat((100 * elapsed / self.baseHorizont).toFixed(3), 10);
fn({ cycle: cycle, elapsed: elapsed, remaining: remaining, progress: progress });
}, tick);
return this;
};

return function (fn, horizont) {
return new Countdown(fn, horizont);
};
})();
return function (fn, horizont) {
return new Countdown(fn, horizont);
};
})();
(typeof exports === 'object') && (module.exports = countdown);

0 comments on commit d73e527

Please sign in to comment.