Skip to content

Commit

Permalink
Merge pull request #2 from fedeghe/iso
Browse files Browse the repository at this point in the history
Iso
  • Loading branch information
fedeghe committed Apr 13, 2023
2 parents 28c6ffd + 2fb4f7c commit 007b75e
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"extends": "standard",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
"SharedArrayBuffer": "readonly",
"maltaF": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
Expand Down
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.11)</small></sub>
# countdown <sub><small>(v. 0.0.13)</small></sub>

A really simple function to provide and extended version of `[native] setTimeout` which can be
A really simple function to provide and extended isomorphic version of the native `setTimeout` which can be
- paused / resumed
- updated while running
- have a ticking function
Expand Down
8 changes: 4 additions & 4 deletions dist/index.js

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

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fedeghe/countdown",
"version": "0.0.12",
"version": "0.0.13",
"main": "dist/index.js",
"repository": "git@github.com:fedeghe/countdown.git",
"author": "fedeghe <fedeghe@gmail.com>",
Expand All @@ -18,6 +18,7 @@
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
},
"devDependencies": {
"@fedeghe/interval": "^1.0.19",
"coveralls": "^3.0.4",
"eslint": "^6.0.1",
"eslint-config-standard": "^12.0.0",
Expand All @@ -33,7 +34,5 @@
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^14.1.1"
},
"dependencies": {
"@fedeghe/interval": "^1.0.19"
}
"dependencies": {}
}
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 version of `[native] setTimeout` which can be
A really simple function to provide and extended isomorphic version of the native `setTimeout` which can be
- paused / resumed
- updated while running
- have a ticking function
Expand Down
4 changes: 2 additions & 2 deletions source/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rm -rf dist test",
"mkdir dist test"
],
"#source/index.js": "dist -plugins=malta-js-uglify...malta-header-comment[name:\"source/header.txt\"]",
"#source/test/unit.js": "test",
"#source/index.js": "dist -plugins=malta-js-uglify...malta-header-comment[name:\"source/header.txt\"] -options=placeholderMode:'func'",
"#source/test/unit.js": "test -options=placeholderMode:'func'",
"#source/README.md": "."
}
4 changes: 2 additions & 2 deletions source/builddev.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rm -rf dist test",
"mkdir dist test"
],
"source/index.js": "dist -plugins=malta-js-uglify...malta-header-comment[name:\"source/header.txt\"]...malta-mocha",
"source/test/unit.js": "test -plugins=malta-mocha",
"source/index.js": "dist -plugins=malta-js-uglify...malta-header-comment[name:\"source/header.txt\"]...malta-mocha -options=placeholderMode:'func'",
"source/test/unit.js": "test -plugins=malta-mocha -options=placeholderMode:'func'",
"source/README.md": "."
}
226 changes: 112 additions & 114 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,125 @@
const 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;
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;

this._onUpdate = null;
this._update = null;
this._onPause = null;
this._pause = null;
this._onResume = null;
this._resume = null;
}
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.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; };
Countdown.prototype.onEnd = function (f) { if (isFunction(f)) { this._onEnd = f; } return this; };

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

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

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

Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = f; } return this; };
Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = 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.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.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.start();
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.update = function (amount) {
var am = parseInt(amount);
this.updates = am;
// eslint-disable-next-line one-var
var now = +new Date(),
elapsed = now - this.startTime - this.pauseSpan,
remaining = this.baseHorizont - elapsed,
newHorizont = am + remaining;
this.baseHorizont = elapsed + newHorizont;
if (newHorizont && newHorizont > 0) {
this._onUpdate && this._onUpdate(this);
this.horizont = newHorizont;
this.to && clearTimeout(this.to);
this.run();
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;
}
return this;
};
}, this.horizont);
!avoid && this.ticker && this.ticker.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;
};
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);
8 changes: 0 additions & 8 deletions source/test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('basic operations', () => {
}, horizont+pause+10);
}).timeout(8000);


it('should pause-resume using a ticker and end within a good range (2s run, 5 s pause in the middle, 0.01% tolerance)', done => {
var horizont = 2e3,
tolerance = horizont * 0.01,
Expand Down Expand Up @@ -100,8 +99,6 @@ describe('basic operations', () => {
}, horizont + pause + 10);
}).timeout(8000);



it('should update as expected the countdown (0.01% tolerance)', done => {
var horizont = 2e3,
tolerance = 0.01,
Expand Down Expand Up @@ -132,9 +129,6 @@ describe('basic operations', () => {
}, horizont + up*4 + 10);
}).timeout(25000);




it('should throw an error', done => {
var horizont = 2e3,
failed = false;
Expand All @@ -152,6 +146,4 @@ describe('basic operations', () => {
done();
}, horizont + 10);
}).timeout(8000);


});

0 comments on commit 007b75e

Please sign in to comment.