control-timeout
Advanced tools
Comparing version
@@ -1,153 +0,225 @@ | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var Timeout, moduleName, types, | ||
slice = [].slice; | ||
"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; } | ||
// Generated by CoffeeScript 2.3.2 | ||
(function () { | ||
// control-timeout - A timeout class for controlling one or multiple timeouts. | ||
// MIT License | ||
// Copyright (c) 2016 Dennis Raymondo van der Sluis | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
var Timeout, moduleName, types; | ||
types = require('types.js'); | ||
moduleName = 'control-timeout'; | ||
Timeout = (function() { | ||
Timeout.setLog = function(log) { | ||
return Timeout.log = types.forceFunction(log); | ||
}; | ||
Timeout = function () { | ||
var Timeout = | ||
/*#__PURE__*/ | ||
function () { | ||
_createClass(Timeout, null, [{ | ||
key: "setLog", | ||
value: function setLog(log) { | ||
return Timeout.log = types.forceFunction(log); | ||
} | ||
}]); | ||
Timeout.log = Timeout.setLog(typeof console !== "undefined" && console !== null ? console.log : void 0); | ||
function Timeout(delay) { | ||
_classCallCheck(this, Timeout); | ||
Timeout.delay = 0; | ||
this.timeouts = {}; | ||
this.running = {}; | ||
this.delay = Math.abs(delay) || Timeout.delay; | ||
} | ||
function Timeout(delay) { | ||
this.timeouts = {}; | ||
this.running = {}; | ||
this.delay = (Math.abs(delay)) || Timeout.delay; | ||
} | ||
_createClass(Timeout, [{ | ||
key: "exists", | ||
value: function exists(id) { | ||
return this.timeouts.hasOwnProperty(id); | ||
} | ||
}, { | ||
key: "isRunning", | ||
value: function isRunning(id) { | ||
return this.running.hasOwnProperty(id); | ||
} | ||
}, { | ||
key: "_stopOne", | ||
value: function _stopOne(id) { | ||
if (this.isRunning(id)) { | ||
clearTimeout(this.running[id]); | ||
delete this.running[id]; | ||
} | ||
Timeout.prototype.exists = function(id) { | ||
return this.timeouts.hasOwnProperty(id); | ||
}; | ||
return this; | ||
} | ||
}, { | ||
key: "stop", | ||
value: function stop() { | ||
var i, id, len; | ||
Timeout.prototype.isRunning = function(id) { | ||
return this.running.hasOwnProperty(id); | ||
}; | ||
for (var _len = arguments.length, ids = new Array(_len), _key = 0; _key < _len; _key++) { | ||
ids[_key] = arguments[_key]; | ||
} | ||
Timeout.prototype._stopOne = function(id) { | ||
if (this.isRunning(id)) { | ||
clearTimeout(this.running[id]); | ||
delete this.running[id]; | ||
} | ||
return this; | ||
}; | ||
if (!ids.length) { | ||
for (id in this.running) { | ||
this._stopOne(id); | ||
} | ||
} else { | ||
for (i = 0, len = ids.length; i < len; i++) { | ||
id = ids[i]; | ||
Timeout.prototype.stop = function() { | ||
var i, id, ids, len; | ||
ids = 1 <= arguments.length ? slice.call(arguments, 0) : []; | ||
if (!ids.length) { | ||
for (id in this.running) { | ||
this._stopOne(id); | ||
this._stopOne(id); | ||
} | ||
} | ||
return this; | ||
} | ||
} else { | ||
for (i = 0, len = ids.length; i < len; i++) { | ||
id = ids[i]; | ||
this._stopOne(id); | ||
}, { | ||
key: "setDelay", | ||
value: function setDelay(id, delay) { | ||
if (this.exists(id)) { | ||
return this.timeouts[id].delay = types.forceNumber(delay, this.timeouts[id].delay); | ||
} | ||
} | ||
} | ||
return this; | ||
}; | ||
}, { | ||
key: "getTimeout", | ||
value: function getTimeout(id) { | ||
return this.running[id]; | ||
} | ||
}, { | ||
key: "_setTimeout", | ||
value: function _setTimeout(id, action, delay) { | ||
var _this = this; | ||
Timeout.prototype.setDelay = function(id, delay) { | ||
if (this.exists(id)) { | ||
return this.timeouts[id].delay = types.forceNumber(delay, this.timeouts[id].delay); | ||
} | ||
}; | ||
return this.running[id] = setTimeout(function () { | ||
delete _this.running[id]; | ||
return action(); | ||
}, delay); | ||
} | ||
}, { | ||
key: "run", | ||
value: function run(id) { | ||
var ref, timeout, timeouts; | ||
timeouts = []; | ||
Timeout.prototype.getTimeout = function(id) { | ||
return this.running[id]; | ||
}; | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
Timeout.prototype._setTimeout = function(id, action, delay) { | ||
return this.running[id] = setTimeout((function(_this) { | ||
return function() { | ||
delete _this.running[id]; | ||
return action(); | ||
}; | ||
})(this), delay); | ||
}; | ||
if (!id) { | ||
ref = this.timeouts; | ||
Timeout.prototype.run = function() { | ||
var args, id, ref, ref1, ref2, timeout, timeouts; | ||
id = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; | ||
timeouts = []; | ||
if (!id) { | ||
ref = this.timeouts; | ||
for (id in ref) { | ||
timeout = ref[id]; | ||
if (!this.isRunning(id)) { | ||
timeouts.push(this._setTimeout(id, (ref1 = timeout.action).bind.apply(ref1, [null].concat(slice.call(args))), timeout.delay)); | ||
for (id in ref) { | ||
timeout = ref[id]; | ||
if (!this.isRunning(id)) { | ||
var _timeout$action; | ||
timeouts.push(this._setTimeout(id, (_timeout$action = timeout.action).bind.apply(_timeout$action, [null].concat(args)), timeout.delay)); | ||
} | ||
} | ||
} else if (this.exists(id)) { | ||
var _this$timeouts$id$act; | ||
timeouts.push(this._setTimeout(id, (_this$timeouts$id$act = this.timeouts[id].action).bind.apply(_this$timeouts$id$act, [null].concat(args)), this.timeouts[id].delay)); | ||
} else { | ||
Timeout.log(moduleName + ': timeout with id: "' + id + '" was not found'); | ||
} | ||
switch (timeouts.length) { | ||
case 0: | ||
return null; | ||
case 1: | ||
return timeouts[0]; | ||
default: | ||
return timeouts; | ||
} | ||
} | ||
} else if (this.exists(id)) { | ||
timeouts.push(this._setTimeout(id, (ref2 = this.timeouts[id].action).bind.apply(ref2, [null].concat(slice.call(args))), this.timeouts[id].delay)); | ||
} else { | ||
Timeout.log(moduleName + ': timeout with id: "' + id + '" was not found'); | ||
} | ||
switch (timeouts.length) { | ||
case 0: | ||
return null; | ||
case 1: | ||
return timeouts[0]; | ||
default: | ||
return timeouts; | ||
} | ||
}; | ||
}, { | ||
key: "removeAll", | ||
value: function removeAll() { | ||
this.stop(); | ||
this.timeouts = {}; | ||
return this; | ||
} | ||
}, { | ||
key: "remove", | ||
value: function remove() { | ||
var i, id, len; | ||
Timeout.prototype.removeAll = function() { | ||
this.stop(); | ||
this.timeouts = {}; | ||
return this; | ||
}; | ||
for (var _len3 = arguments.length, ids = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
ids[_key3] = arguments[_key3]; | ||
} | ||
Timeout.prototype.remove = function() { | ||
var i, id, ids, len; | ||
ids = 1 <= arguments.length ? slice.call(arguments, 0) : []; | ||
if (ids.length) { | ||
for (i = 0, len = ids.length; i < len; i++) { | ||
id = ids[i]; | ||
this._stopOne(id); | ||
delete this.timeouts[id]; | ||
if (ids.length) { | ||
for (i = 0, len = ids.length; i < len; i++) { | ||
id = ids[i]; | ||
this._stopOne(id); | ||
delete this.timeouts[id]; | ||
} | ||
} else { | ||
Timeout.log(moduleName + ': cannot remove, invalid or non-existing timeout!'); | ||
} | ||
return this; | ||
} | ||
} else { | ||
Timeout.log(moduleName + ': cannot remove, invalid or non-existing timeout!'); | ||
} | ||
return this; | ||
}; | ||
}, { | ||
key: "_add", | ||
value: function _add(id, action, delay) { | ||
if (types.notString(id) || !id.length) { | ||
Timeout.log(moduleName + ': cannot add timeout, invalid or missing id!'); | ||
} else if (this.exists(id)) { | ||
Timeout.log(moduleName + ': cannot add timeout, id: ' + id + ' exists already!'); | ||
} else { | ||
this.timeouts[id] = { | ||
action: types.forceFunction(action), | ||
delay: Math.abs(types.forceNumber(delay, this.delay)) | ||
}; | ||
} | ||
Timeout.prototype._add = function(id, action, delay) { | ||
if (types.notString(id) || !id.length) { | ||
Timeout.log(moduleName + ': cannot add timeout, invalid or missing id!'); | ||
} else if (this.exists(id)) { | ||
Timeout.log(moduleName + ': cannot add timeout, id: ' + id + ' exists already!'); | ||
} else { | ||
this.timeouts[id] = { | ||
action: types.forceFunction(action), | ||
delay: Math.abs(types.forceNumber(delay, this.delay)) | ||
}; | ||
return id; | ||
} | ||
return void 0; | ||
}; | ||
return this; | ||
} | ||
}, { | ||
key: "add", | ||
value: function add(id, action, delay) { | ||
if (types.isObject(id)) { | ||
return this._add(id.id, id.action, id.delay); | ||
} else { | ||
return this._add(id, action, delay); | ||
} | ||
} | ||
}]); | ||
Timeout.prototype.add = function(id, action, delay) { | ||
if (types.isObject(id)) { | ||
return this._add(id.id, id.action, id.delay); | ||
} else { | ||
return this._add(id, action, delay); | ||
} | ||
}; | ||
return Timeout; | ||
}(); | ||
; | ||
Timeout.log = Timeout.setLog(typeof console !== "undefined" && console !== null ? console.log : void 0); | ||
Timeout.delay = 0; | ||
return Timeout; | ||
}.call(this); | ||
})(); | ||
module.exports = Timeout; | ||
}).call(this); | ||
}).call(void 0); |
@@ -1,1 +0,1 @@ | ||
(function(){var t,n,e,i=[].slice;e=require("types.js"),n="control-timeout",t=function(){function t(n){this.timeouts={},this.running={},this.delay=Math.abs(n)||t.delay}return t.setLog=function(n){return t.log=e.forceFunction(n)},t.log=t.setLog("undefined"!=typeof console&&null!==console?console.log:void 0),t.delay=0,t.prototype.exists=function(t){return this.timeouts.hasOwnProperty(t)},t.prototype.isRunning=function(t){return this.running.hasOwnProperty(t)},t.prototype._stopOne=function(t){return this.isRunning(t)&&(clearTimeout(this.running[t]),delete this.running[t]),this},t.prototype.stop=function(){var t,n,e,o;if(e=1<=arguments.length?i.call(arguments,0):[],e.length)for(t=0,o=e.length;o>t;t++)n=e[t],this._stopOne(n);else for(n in this.running)this._stopOne(n);return this},t.prototype.setDelay=function(t,n){return this.exists(t)?this.timeouts[t].delay=e.forceNumber(n,this.timeouts[t].delay):void 0},t.prototype.getTimeout=function(t){return this.running[t]},t.prototype._setTimeout=function(t,n,e){return this.running[t]=setTimeout(function(e){return function(){return delete e.running[t],n()}}(this),e)},t.prototype.run=function(){var e,o,s,r,u,l,a;if(o=arguments[0],e=2<=arguments.length?i.call(arguments,1):[],a=[],o)this.exists(o)?a.push(this._setTimeout(o,(u=this.timeouts[o].action).bind.apply(u,[null].concat(i.call(e))),this.timeouts[o].delay)):t.log(n+': timeout with id: "'+o+'" was not found');else{s=this.timeouts;for(o in s)l=s[o],this.isRunning(o)||a.push(this._setTimeout(o,(r=l.action).bind.apply(r,[null].concat(i.call(e))),l.delay))}switch(a.length){case 0:return null;case 1:return a[0];default:return a}},t.prototype.removeAll=function(){return this.stop(),this.timeouts={},this},t.prototype.remove=function(){var e,o,s,r;if(s=1<=arguments.length?i.call(arguments,0):[],s.length)for(e=0,r=s.length;r>e;e++)o=s[e],this._stopOne(o),delete this.timeouts[o];else t.log(n+": cannot remove, invalid or non-existing timeout!");return this},t.prototype._add=function(i,o,s){if(e.notString(i)||!i.length)t.log(n+": cannot add timeout, invalid or missing id!");else{if(!this.exists(i))return this.timeouts[i]={action:e.forceFunction(o),delay:Math.abs(e.forceNumber(s,this.delay))},i;t.log(n+": cannot add timeout, id: "+i+" exists already!")}return void 0},t.prototype.add=function(t,n,i){return e.isObject(t)?this._add(t.id,t.action,t.delay):this._add(t,n,i)},t}(),module.exports=t}).call(this); | ||
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),e}(function(){var e,c,i;i=require("types.js"),c="control-timeout",e=function(){var e=function(){function l(e){_classCallCheck(this,l),this.timeouts={},this.running={},this.delay=Math.abs(e)||l.delay}return _createClass(l,null,[{key:"setLog",value:function(e){return l.log=i.forceFunction(e)}}]),_createClass(l,[{key:"exists",value:function(e){return this.timeouts.hasOwnProperty(e)}},{key:"isRunning",value:function(e){return this.running.hasOwnProperty(e)}},{key:"_stopOne",value:function(e){return this.isRunning(e)&&(clearTimeout(this.running[e]),delete this.running[e]),this}},{key:"stop",value:function(){for(var e,t,n,i=arguments.length,s=new Array(i),o=0;o<i;o++)s[o]=arguments[o];if(s.length)for(e=0,n=s.length;e<n;e++)t=s[e],this._stopOne(t);else for(t in this.running)this._stopOne(t);return this}},{key:"setDelay",value:function(e,t){if(this.exists(e))return this.timeouts[e].delay=i.forceNumber(t,this.timeouts[e].delay)}},{key:"getTimeout",value:function(e){return this.running[e]}},{key:"_setTimeout",value:function(e,t,n){var i=this;return this.running[e]=setTimeout(function(){return delete i.running[e],t()},n)}},{key:"run",value:function(e){var t,n,i;i=[];for(var s=arguments.length,o=new Array(1<s?s-1:0),r=1;r<s;r++)o[r-1]=arguments[r];if(e)if(this.exists(e)){var u;i.push(this._setTimeout(e,(u=this.timeouts[e].action).bind.apply(u,[null].concat(o)),this.timeouts[e].delay))}else l.log(c+': timeout with id: "'+e+'" was not found');else for(e in t=this.timeouts){var a;if(n=t[e],!this.isRunning(e))i.push(this._setTimeout(e,(a=n.action).bind.apply(a,[null].concat(o)),n.delay))}switch(i.length){case 0:return null;case 1:return i[0];default:return i}}},{key:"removeAll",value:function(){return this.stop(),this.timeouts={},this}},{key:"remove",value:function(){for(var e,t,n,i=arguments.length,s=new Array(i),o=0;o<i;o++)s[o]=arguments[o];if(s.length)for(e=0,n=s.length;e<n;e++)t=s[e],this._stopOne(t),delete this.timeouts[t];else l.log(c+": cannot remove, invalid or non-existing timeout!");return this}},{key:"_add",value:function(e,t,n){return i.notString(e)||!e.length?l.log(c+": cannot add timeout, invalid or missing id!"):this.exists(e)?l.log(c+": cannot add timeout, id: "+e+" exists already!"):this.timeouts[e]={action:i.forceFunction(t),delay:Math.abs(i.forceNumber(n,this.delay))},this}},{key:"add",value:function(e,t,n){return i.isObject(e)?this._add(e.id,e.action,e.delay):this._add(e,t,n)}}]),l}();return e.log=e.setLog("undefined"!=typeof console&&null!==console?console.log:void 0),e.delay=0,e}.call(this),module.exports=e}).call(void 0); |
{ | ||
"name": "control-timeout", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "A timeout class for controlling one or multiple timeouts", | ||
"main": "control-timeout.min.js", | ||
"scripts": { | ||
"uglify": "uglifyjs control-timeout.js -c -m -o control-timeout.min.js", | ||
"build": "coffee -c --transpile control-timeout.coffee && npm run uglify", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -13,5 +15,9 @@ }, | ||
}, | ||
"dependencies" : { | ||
"types.js" : "latest" | ||
"dependencies": { | ||
"types.js": "latest" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "latest", | ||
"@babel/preset-env": "latest" | ||
}, | ||
"keywords": [ | ||
@@ -31,2 +37,2 @@ "timeout", | ||
"homepage": "https://github.com/phazelift/control-timeout" | ||
} | ||
} |
@@ -114,2 +114,13 @@ #control-timeout | ||
change log | ||
========== | ||
**0.1.0** | ||
- .add prototype now returns the context | ||
- adds babel transpiler as dev dependency for better cross browser compatibility | ||
--- | ||
###license | ||
@@ -116,0 +127,0 @@ |
Sorry, the diff of this file is not supported yet
19497
27.13%195
48.85%129
9.32%2
Infinity%