oocoder/pensi-scheduler

View on GitHub
release/pensi-scheduler.min.js

Summary

Maintainability
A
0 mins
Test Coverage
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.scheduler=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++){s(r[o])}return s}({1:[function(require,module,exports){module.exports=function(){var TICKS_ONE_DAY=24*60*60*1e3;return{create:function create(config){config=config||{};return createScheduler({period:config.period||TICKS_ONE_DAY})}}}();function createScheduler(config){if(config===undefined)throw new Error("config cannot be undefined");var events=require("events");var tasks=[];var tasksQueue=getEnumerator();var period=config.period;var isRunning=false;var inst=new events.EventEmitter;var clearId;function updateTaskQueue(){tasksQueue=getEnumerator(tasks.sort(function(a,b){if(calcDelayTicks(a.start,period)>calcDelayTicks(b.start,period))return 1;if(calcDelayTicks(a.start,period)<calcDelayTicks(b.start,period))return-1;return 0}));return tasksQueue.next()}function scheduleTask(){if(!isRunning)return;var task=tasksQueue.next();if(task===undefined)task=updateTaskQueue();var delay=calcDelayTicks(task.start,period);clearId=setTimeout(function(){if(isRunning){inst.emit("interval",task);scheduleTask()}},delay)}function calcDelayTicks(startDate,period){if(!isDate(startDate))throw new Error("startDate must be a date");if(!Number.isInteger(period))throw new Error("period must be an integer");var diff=startDate.getTime()-(new Date).getTime();return(diff<0?period-Math.abs(diff)%period:diff)%(period+1)}function isDate(date){return date instanceof Date&&!isNaN(date.valueOf())}function getEnumerator(arrayObj,flags){arrayObj=arrayObj||[];var ccValue=flags?flags.isCyclical||0:0;var idx=-1;var length=arrayObj.length;return{next:function next(){idx=++idx>=length?ccValue:idx;return arrayObj[idx]}}}function parseTime(time){var rs=/(\d+):(\d+):*(\d*)*/.exec(time);if(rs!==null){var dt=new Date;time=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate(),parseInt(rs[1]),parseInt(rs[2]),rs[3]!==undefined?parseInt(rs[3]):dt.getSeconds())}return time}inst.addTask=function(name,time,metaInfo){if(typeof time==="string")time=parseTime(time);time=time||new Date;tasks.push({name:name,start:time,meta:metaInfo})};inst.start=function(){isRunning=true;scheduleTask()};inst.stop=function(){if(isRunning){isRunning=false;clearTimeout(clearId);tasksQueue=undefined}};return inst}},{events:2}],2:[function(require,module,exports){function EventEmitter(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}module.exports=EventEmitter;EventEmitter.EventEmitter=EventEmitter;EventEmitter.prototype._events=undefined;EventEmitter.prototype._maxListeners=undefined;EventEmitter.defaultMaxListeners=10;EventEmitter.prototype.setMaxListeners=function(n){if(!isNumber(n)||n<0||isNaN(n))throw TypeError("n must be a positive number");this._maxListeners=n;return this};EventEmitter.prototype.emit=function(type){var er,handler,len,args,i,listeners;if(!this._events)this._events={};if(type==="error"){if(!this._events.error||isObject(this._events.error)&&!this._events.error.length){er=arguments[1];if(er instanceof Error){throw er}throw TypeError('Uncaught, unspecified "error" event.')}}handler=this._events[type];if(isUndefined(handler))return false;if(isFunction(handler)){switch(arguments.length){case 1:handler.call(this);break;case 2:handler.call(this,arguments[1]);break;case 3:handler.call(this,arguments[1],arguments[2]);break;default:args=Array.prototype.slice.call(arguments,1);handler.apply(this,args)}}else if(isObject(handler)){args=Array.prototype.slice.call(arguments,1);listeners=handler.slice();len=listeners.length;for(i=0;i<len;i++){listeners[i].apply(this,args)}}return true};EventEmitter.prototype.addListener=function(type,listener){var m;if(!isFunction(listener))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",type,isFunction(listener.listener)?listener.listener:listener);if(!this._events[type])this._events[type]=listener;else if(isObject(this._events[type]))this._events[type].push(listener);else this._events[type]=[this._events[type],listener];if(isObject(this._events[type])&&!this._events[type].warned){if(!isUndefined(this._maxListeners)){m=this._maxListeners}else{m=EventEmitter.defaultMaxListeners}if(m&&m>0&&this._events[type].length>m){this._events[type].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[type].length);if(typeof console.trace==="function"){console.trace()}}}return this};EventEmitter.prototype.on=EventEmitter.prototype.addListener;EventEmitter.prototype.once=function(type,listener){if(!isFunction(listener))throw TypeError("listener must be a function");var fired=false;function g(){this.removeListener(type,g);if(!fired){fired=true;listener.apply(this,arguments)}}g.listener=listener;this.on(type,g);return this};EventEmitter.prototype.removeListener=function(type,listener){var list,position,length,i;if(!isFunction(listener))throw TypeError("listener must be a function");if(!this._events||!this._events[type])return this;list=this._events[type];length=list.length;position=-1;if(list===listener||isFunction(list.listener)&&list.listener===listener){delete this._events[type];if(this._events.removeListener)this.emit("removeListener",type,listener)}else if(isObject(list)){for(i=length;i-- >0;){if(list[i]===listener||list[i].listener&&list[i].listener===listener){position=i;break}}if(position<0)return this;if(list.length===1){list.length=0;delete this._events[type]}else{list.splice(position,1)}if(this._events.removeListener)this.emit("removeListener",type,listener)}return this};EventEmitter.prototype.removeAllListeners=function(type){var key,listeners;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[type])delete this._events[type];return this}if(arguments.length===0){for(key in this._events){if(key==="removeListener")continue;this.removeAllListeners(key)}this.removeAllListeners("removeListener");this._events={};return this}listeners=this._events[type];if(isFunction(listeners)){this.removeListener(type,listeners)}else if(listeners){while(listeners.length){this.removeListener(type,listeners[listeners.length-1])}}delete this._events[type];return this};EventEmitter.prototype.listeners=function(type){var ret;if(!this._events||!this._events[type])ret=[];else if(isFunction(this._events[type]))ret=[this._events[type]];else ret=this._events[type].slice();return ret};EventEmitter.prototype.listenerCount=function(type){if(this._events){var evlistener=this._events[type];if(isFunction(evlistener))return 1;else if(evlistener)return evlistener.length}return 0};EventEmitter.listenerCount=function(emitter,type){return emitter.listenerCount(type)};function isFunction(arg){return typeof arg==="function"}function isNumber(arg){return typeof arg==="number"}function isObject(arg){return typeof arg==="object"&&arg!==null}function isUndefined(arg){return arg===void 0}},{}]},{},[1])(1)});