timeoutcontrol
Advanced tools
| declare class TimeoutControl { | ||
| protected readonly params: any[]; | ||
| protected readonly callback: (this: TimeoutControl, callback: (...params: any[]) => void) => void; | ||
| private readonly duration; | ||
| /** | ||
| * `.timeStop` exists solely for the purpose of calculating `.timeLeft`. | ||
| * So every value that it get assigned is for that purpose, | ||
| * though it might look weird in some cases. | ||
| */ | ||
| private timeStop; | ||
| private timeStart; | ||
| private id; | ||
| constructor(callback: (...params: any[]) => void, duration: number, ...params: any[]); | ||
| protected readonly timeLeft: number; | ||
| done(): boolean; | ||
| pause(): void; | ||
| resume(): void; | ||
| clear(): void; | ||
| restart(): void; | ||
| } | ||
| export default TimeoutControl; |
+81
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global = global || self, global.TimeoutControl = factory()); | ||
| }(this, function () { 'use strict'; | ||
| var TimeoutControl = /** @class */ (function () { | ||
| function TimeoutControl(callback, duration) { | ||
| var params = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| params[_i - 2] = arguments[_i]; | ||
| } | ||
| var instErr = 'Failed to create TimeoutControl instance:'; | ||
| if (arguments.length < 2) | ||
| throw new TypeError(instErr + " at least 2 arguments required, only " + arguments.length + " supplied."); | ||
| if (typeof callback !== 'function') | ||
| throw new TypeError(instErr + " first argument must be a function."); | ||
| if (typeof duration !== 'number') | ||
| throw new TypeError(instErr + " second argument must be a number."); | ||
| if (duration < 0) | ||
| throw new RangeError(instErr + " second argument must be a number greater than 0."); | ||
| this.params = params; | ||
| this.duration = duration; | ||
| this.timeStart = Date.now(); | ||
| /** | ||
| * Init `.timeStop` with `.timeStart` value | ||
| * so that `.timeLeft` can equal to `.duration` on init. | ||
| */ | ||
| this.timeStop = this.timeStart; | ||
| /** | ||
| * Decorate to pass extra params into timeout callback. | ||
| * Params are passed here instead of directly on `setTimeout`, | ||
| * so that this can also act as shim for browsers that | ||
| * don't support native "rest" param on `setTimeout`. | ||
| */ | ||
| this.callback = function (callback) { | ||
| callback.apply(void 0, this.params); | ||
| this.timeStop = Date.now(); | ||
| }.bind(this, callback); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| Object.defineProperty(TimeoutControl.prototype, "timeLeft", { | ||
| get: function () { | ||
| var ret = this.duration - (this.timeStop - this.timeStart); | ||
| return ret < 0 | ||
| ? 0 | ||
| : ret; | ||
| }, | ||
| enumerable: true, | ||
| configurable: true | ||
| }); | ||
| TimeoutControl.prototype.done = function () { | ||
| return this.timeLeft === 0; | ||
| }; | ||
| TimeoutControl.prototype.pause = function () { | ||
| clearTimeout(this.id); | ||
| this.timeStop = Date.now(); | ||
| }; | ||
| TimeoutControl.prototype.resume = function () { | ||
| this.id = setTimeout(this.callback, this.timeLeft); | ||
| }; | ||
| TimeoutControl.prototype.clear = function () { | ||
| clearTimeout(this.id); | ||
| this.timeStart = Date.now(); | ||
| this.timeStop = this.timeStart; | ||
| }; | ||
| TimeoutControl.prototype.restart = function () { | ||
| this.clear(); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| }; | ||
| return TimeoutControl; | ||
| }()); | ||
| return TimeoutControl; | ||
| })); |
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).TimeoutControl=e()}(this,function(){"use strict";return function(){function t(t,e){for(var i=[],o=2;o<arguments.length;o++)i[o-2]=arguments[o];var r="Failed to create TimeoutControl instance:";if(arguments.length<2)throw new TypeError(r+" at least 2 arguments required, only "+arguments.length+" supplied.");if("function"!=typeof t)throw new TypeError(r+" first argument must be a function.");if("number"!=typeof e)throw new TypeError(r+" second argument must be a number.");if(e<0)throw new RangeError(r+" second argument must be a number greater than 0.");this.params=i,this.duration=e,this.timeStart=Date.now(),this.timeStop=this.timeStart,this.callback=function(t){t.apply(void 0,this.params),this.timeStop=Date.now()}.bind(this,t),this.id=setTimeout(this.callback,this.duration)}return Object.defineProperty(t.prototype,"timeLeft",{get:function(){var t=this.duration-(this.timeStop-this.timeStart);return t<0?0:t},enumerable:!0,configurable:!0}),t.prototype.done=function(){return 0===this.timeLeft},t.prototype.pause=function(){clearTimeout(this.id),this.timeStop=Date.now()},t.prototype.resume=function(){this.id=setTimeout(this.callback,this.timeLeft)},t.prototype.clear=function(){clearTimeout(this.id),this.timeStart=Date.now(),this.timeStop=this.timeStart},t.prototype.restart=function(){this.clear(),this.id=setTimeout(this.callback,this.duration)},t}()}); |
+12
-5
| { | ||
| "name": "timeoutcontrol", | ||
| "version": "1.0.1", | ||
| "version": "1.1.0", | ||
| "description": "A JavaScript tool to control setTimeout operations.", | ||
| "main": "./lib/timeoutcontrol.js", | ||
| "browser": "./lib/timeoutcontrol.min.js", | ||
| "main": "./lib/index.js", | ||
| "browser": "./lib/index.min.js", | ||
| "license": "MIT", | ||
@@ -14,4 +14,7 @@ "author": "Yong Quan Lim <yqlim2394@gmail.com>", | ||
| "build": "npm run rollup && npm run terser", | ||
| "dev": "rollup -c -w", | ||
| "rollup": "rollup -c", | ||
| "terser": "terser -c -m --comments /license/i -o ./lib/timeoutcontrol.min.js ./lib/timeoutcontrol.js" | ||
| "terser": "terser -c -m --comments /license/i -o ./lib/index.min.js ./lib/index.js", | ||
| "test": "mocha", | ||
| "testing": "mocha -w" | ||
| }, | ||
@@ -21,5 +24,9 @@ "devDependencies": { | ||
| "@babel/preset-env": "^7.2.3", | ||
| "chai": "^4.2.0", | ||
| "mocha": "^6.2.0", | ||
| "rollup": "^1.1.0", | ||
| "rollup-plugin-babel": "^4.3.0", | ||
| "terser": "^3.14.1" | ||
| "rollup-plugin-typescript2": "^0.22.1", | ||
| "terser": "^3.14.1", | ||
| "typescript": "^3.5.3" | ||
| }, | ||
@@ -26,0 +33,0 @@ "repository": { |
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global = global || self, global.TimeoutControl = factory()); | ||
| }(this, function () { 'use strict'; | ||
| function _classCallCheck(instance, Constructor) { | ||
| if (!(instance instanceof Constructor)) { | ||
| throw new TypeError("Cannot call a class as a function"); | ||
| } | ||
| } | ||
| function _defineProperties(target, props) { | ||
| for (var i = 0; i < props.length; i++) { | ||
| var descriptor = props[i]; | ||
| descriptor.enumerable = descriptor.enumerable || false; | ||
| descriptor.configurable = true; | ||
| if ("value" in descriptor) descriptor.writable = true; | ||
| Object.defineProperty(target, descriptor.key, descriptor); | ||
| } | ||
| } | ||
| function _createClass(Constructor, protoProps, staticProps) { | ||
| if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
| if (staticProps) _defineProperties(Constructor, staticProps); | ||
| return Constructor; | ||
| } | ||
| var TimeoutControl = | ||
| /*#__PURE__*/ | ||
| function () { | ||
| function TimeoutControl(callback, duration) { | ||
| for (var _len = arguments.length, params = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
| params[_key - 2] = arguments[_key]; | ||
| } | ||
| _classCallCheck(this, TimeoutControl); | ||
| var instErr = 'Failed to create TimeoutControl instance:'; | ||
| if (arguments.length < 2) throw new TypeError("".concat(instErr, " at least 2 arguments required, only ").concat(arguments.length, " supplied.")); | ||
| if (typeof callback !== 'function') throw new TypeError("".concat(instErr, " first argument must be a function.")); | ||
| if (typeof duration !== 'number') throw new TypeError("".concat(instErr, " second argument must be a number greater than 0.")); | ||
| if (duration < 0) throw new RangeError("".concat(instErr, " second argument must be a number greater than 0.")); | ||
| this.params = params; | ||
| this.duration = duration; | ||
| this.timeStart = Date.now(); | ||
| this.timeStop = this.timeStart; | ||
| this.callback = function (callback) { | ||
| // Decorate to pass extra params into timeout callback | ||
| // Params are passed here instead of directly on setTimeout | ||
| // so that this can also act as polyfill for browsers that | ||
| // don't support native "rest" param on setTimeout | ||
| callback.apply(null, this.params); | ||
| this.timeStop = Date.now(); | ||
| }.bind(this, callback); | ||
| Object.defineProperty(this, 'timeLeft', { | ||
| get: function get() { | ||
| var ret = this.duration - (this.timeStop - this.timeStart); | ||
| return ret < 0 ? 0 : ret; | ||
| } | ||
| }); | ||
| Object.defineProperty(this, 'done', { | ||
| get: function get() { | ||
| return this.timeLeft === 0; | ||
| } | ||
| }); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| _createClass(TimeoutControl, [{ | ||
| key: "pause", | ||
| value: function pause() { | ||
| clearTimeout(this.id); | ||
| this.timeStop = Date.now(); | ||
| } | ||
| }, { | ||
| key: "resume", | ||
| value: function resume() { | ||
| this.id = setTimeout(this.callback, this.timeLeft); | ||
| } | ||
| }, { | ||
| key: "clear", | ||
| value: function clear() { | ||
| clearTimeout(this.id); | ||
| this.timeStart = Date.now(); | ||
| this.timeStop = this.timeStart; | ||
| this.id = null; | ||
| } | ||
| }, { | ||
| key: "restart", | ||
| value: function restart() { | ||
| this.clear(); | ||
| this.id = setTimeout(this.callback, this.duration); | ||
| } | ||
| }]); | ||
| return TimeoutControl; | ||
| }(); | ||
| return TimeoutControl; | ||
| })); |
| /*! | ||
| * TimeoutControl | ||
| * (c) 2019 Yong Quan Lim | ||
| * Released under MIT License. | ||
| */ | ||
| !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).TimeoutControl=e()}(this,function(){"use strict";function t(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(){function e(t,i){for(var n=arguments.length,r=new Array(n>2?n-2:0),o=2;o<n;o++)r[o-2]=arguments[o];!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);var a="Failed to create TimeoutControl instance:";if(arguments.length<2)throw new TypeError("".concat(a," at least 2 arguments required, only ").concat(arguments.length," supplied."));if("function"!=typeof t)throw new TypeError("".concat(a," first argument must be a function."));if("number"!=typeof i)throw new TypeError("".concat(a," second argument must be a number greater than 0."));if(i<0)throw new RangeError("".concat(a," second argument must be a number greater than 0."));this.params=r,this.duration=i,this.timeStart=Date.now(),this.timeStop=this.timeStart,this.callback=function(t){t.apply(null,this.params),this.timeStop=Date.now()}.bind(this,t),Object.defineProperty(this,"timeLeft",{get:function(){var t=this.duration-(this.timeStop-this.timeStart);return t<0?0:t}}),Object.defineProperty(this,"done",{get:function(){return 0===this.timeLeft}}),this.id=setTimeout(this.callback,this.duration)}var i,n,r;return i=e,(n=[{key:"pause",value:function(){clearTimeout(this.id),this.timeStop=Date.now()}},{key:"resume",value:function(){this.id=setTimeout(this.callback,this.timeLeft)}},{key:"clear",value:function(){clearTimeout(this.id),this.timeStart=Date.now(),this.timeStop=this.timeStart,this.id=null}},{key:"restart",value:function(){this.clear(),this.id=setTimeout(this.callback,this.duration)}}])&&t(i.prototype,n),r&&t(i,r),e}()}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
9618
0.6%6
20%104
1.96%9
80%1
Infinity%