UNPKG

schedule-js

Version:

Small library to execute function at specific time or by schedule.

1 lines 7.08 kB
'use strict';Object.defineProperty(exports,"__esModule",{value:true});var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj;};var _createClass=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);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i];}return arr2;}else{return Array.from(arr);}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var Schedule=function(){function Schedule(funcToRun){_classCallCheck(this,Schedule);this.stop=this.stop.bind(this);this.runAt=this.runAt.bind(this);this.scheduleAt=this.scheduleAt.bind(this);this.function=funcToRun;for(var _len=arguments.length,args=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key];}this.arguments=args;this.bind=false;this.timerIdR=false;this.timerIdS=false;this.interId=false;}_createClass(Schedule,[{key:'run',value:function run(){var execute=this.function;if(this.bind)execute=this.function.bind(this.bind);execute.apply(undefined,arguments);}},{key:'runAt',value:function runAt(time){if(typeof this.function!=='function'){return console.log('You have to use function as argument in "new Schedule(arg)"');}else if(time.split(':').length!==2||time.split(':')[0].length!==2||time.split(':')[1].length!==2){return console.log('first argument has to be string, which will present time: "**:**"');}else{var execute=this.function,now=new Date(),tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),time.split(':')[0],time.split(':')[1])-now;if(tillFirstStart<=0){tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,time.split(':')[0],time.split(':')[1])-now;}if(this.bind)execute=this.function.bind(this.bind);this.timerIdR=setTimeout.apply(undefined,[execute,tillFirstStart].concat(_toConsumableArray(this.arguments)));}}},{key:'scheduleAt',value:function scheduleAt(inter,timeString){var _this=this;var time=timeString;if(!time)time='00:00';if(typeof this.function!=='function'){return console.log('You have to use function as argument in "new Schedule(arg)"');}else if(inter.slice(-2)!=='ms'&&!Number.isInteger(+inter.slice(0,-1))||inter.slice(-2)==='ms'&&!Number.isInteger(+inter.slice(0,-2))){return console.log('first argument has to be string ["number(ms || s || m || h || d)"]');}else if(time.split(':').length!==2||time.split(':')[0].length!==2||time.split(':')[1].length!==2){return console.log('second argument has to be string, which will present time: "**:**"');}else{var _ret=function(){var execute=_this.function,now=new Date(),tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),time.split(':')[0],time.split(':')[1])-now,interval=void 0;if(tillFirstStart<=0){tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,time.split(':')[0],time.split(':')[1])-now;}if(inter.slice(-2)==='ms'){interval=+inter.slice(0,-2);}else if(inter.slice(-1)==='s'){interval=inter.slice(0,-1)*1000;}else if(inter.slice(-1)==='m'){interval=inter.slice(0,-1)*60000;}else if(inter.slice(-1)==='h'){interval=inter.slice(0,-1)*3600000;}else if(inter.slice(-1)==='d'){interval=inter.slice(0,-1)*86400000;}else{return{v:console.log('first argument has to be string ["number(ms || s || m || h || d)"]')};}if(_this.bind)execute=_this.function.bind(_this.bind);_this.timerIdS=setTimeout(function(){_this.interId=setInterval.apply(undefined,[execute,interval].concat(_toConsumableArray(_this.arguments)));},tillFirstStart);}();if((typeof _ret==='undefined'?'undefined':_typeof(_ret))==="object")return _ret.v;}}},{key:'stop',value:function stop(){this.timerIdR?clearTimeout(this.timerIdR):null;this.timerIdS?clearTimeout(this.timerIdS):null;this.interId?clearInterval(this.interId):null;}},{key:'args',set:function set(args){this.arguments=args;}},{key:'func',set:function set(newFunction){this.function=newFunction;}},{key:'THIS',set:function set(bindWith){this.bind=bindWith;}}],[{key:'runAt',value:function runAt(time,func){if(typeof func!=='function'){return console.log('second argument has to be function');}else if(time.split(':').length!==2||time.split(':')[0].length!==2||time.split(':')[1].length!==2){return console.log('first argument has to be string, which will present time: "**:**"');}else{var now=new Date(),tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),time.split(':')[0],time.split(':')[1])-now;if(tillFirstStart<=0){tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,time.split(':')[0],time.split(':')[1])-now;}for(var _len2=arguments.length,args=Array(_len2>2?_len2-2:0),_key2=2;_key2<_len2;_key2++){args[_key2-2]=arguments[_key2];}setTimeout.apply(undefined,[func,tillFirstStart].concat(args));}}},{key:'scheduleAt',value:function scheduleAt(inter,timeString,func){for(var _len3=arguments.length,args=Array(_len3>3?_len3-3:0),_key3=3;_key3<_len3;_key3++){args[_key3-3]=arguments[_key3];}var time=timeString;if(!time)time='00:00';if(typeof func!=='function'){return console.log('third argument has to be function');}else if(inter.slice(-2)!=='ms'&&!Number.isInteger(+inter.slice(0,-1))||inter.slice(-2)==='ms'&&!Number.isInteger(+inter.slice(0,-2))){return console.log('first argument has to be string ["number(ms || s || m || h || d)"]');}else if(time.split(':').length!==2||time.split(':')[0].length!==2||time.split(':')[1].length!==2){return console.log('second argument has to be string, which will present time: "**:**"');}else{var _ret2=function(){var now=new Date(),tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate(),time.split(':')[0],time.split(':')[1])-now,interval=void 0;if(tillFirstStart<=0){tillFirstStart=new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,time.split(':')[0],time.split(':')[1])-now;}if(inter.slice(-2)==='ms'){interval=+inter.slice(0,-2);}else if(inter.slice(-1)==='s'){interval=inter.slice(0,-1)*1000;}else if(inter.slice(-1)==='m'){interval=inter.slice(0,-1)*60000;}else if(inter.slice(-1)==='h'){interval=inter.slice(0,-1)*3600000;}else if(inter.slice(-1)==='d'){interval=inter.slice(0,-1)*86400000;}else{return{v:console.log('first argument has to be string ["number(ms || s || m || h || d)"]')};}setTimeout(function(){return setInterval.apply(undefined,[func,interval].concat(args));},tillFirstStart);}();if((typeof _ret2==='undefined'?'undefined':_typeof(_ret2))==="object")return _ret2.v;}}}]);return Schedule;}();exports.default=Schedule;