Skip to content

Commit

Permalink
Release 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
grullo90 committed Dec 22, 2017
1 parent 25799db commit 3cd8554
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 103 deletions.
137 changes: 105 additions & 32 deletions lib/task.js → lib/classes/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const XP = require('expandjs'),
* A class used by XPSchedule to provide scheduling functionality.
*
* @class Task
* @since 1.0.0
* @description A class used by XPSchedule to provide scheduling functionality
* @keywords nodejs, expandjs
* @source https://github.com/expandjs/xp-scheduler/blob/master/lib/task.js
Expand All @@ -25,50 +24,88 @@ module.exports = new XP.Class('Task', {

/**
* @constructs
* @param {Object} [options] The task's options
* @param {Object} options The task's options
* @param {Function} options.handler The function to execute
* @param {Date} [options.endDate] The recurrence's date limit
* @param {string} [options.frequency = "precise"] The recurrence's repetition frequency
* @param {string} [options.id] The recurrence's identifier.
* @param {number} [options.interval] The interval between each recurrence's iteration
* @param {number} [options.iterations = 1] The number of recurrence's iterations
* @param {number} [options.iterations] The number of recurrence's iterations
* @param {number} [options.month] The recurrence's month
* @param {number} [options.monthDay] The recurrence's day of the month
* @param {Date} [options.startDate] The recurrence's start date
* @param {string} [options.startTime] The recurrence's start time
* @param {string} [options.week] The recurrence's week
* @param {string} [options.weekDay] The recurrence's day of the week
* @param {Array} [options.weekDays] The recurrence's days of the week
* @param {Function} [resolver]
*/
initialize(options) {
initialize: {
promise: true,
value(options, resolver) {

// Setting
this.options = options;
this.handler = this.options.handler;
this.endDate = this.options.endDate || null;
this.frequency = this.options.frequency || 'precise';
this.id = this.options.id || XP.uuid();
this.interval = this.options.interval || null;
this.iterations = this.frequency === 'precise' ? 1 : this.options.iterations || null;
this.month = this.options.month || null;
this.monthDay = this.options.monthDay || null;
this.startDate = this.options.startDate || new Date();
this.startTime = this.options.startTime || null;
this.week = this.options.week || null;
this.weekDay = this.options.weekDay || null;
this.weekDays = this.options.weekDays || [];

// Adapting
this.adaptee = new RRule({
freq: RRule[this.frequencies[this.frequency]],
interval: this.interval,
count: this.iterations,
byweekday: this.weekDay ? RRule[this.days[this.weekDay]] : this.weekDays.map(day => RRule[this.days[day]]),
bymonth: this.month,
bymonthday: this.monthDay,
byhour: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[1])),
byminute: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[2])),
bysecond: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[4])),
bysetpos: this.week && this.weeks[this.week],
dtstart: this.startDate,
until: this.endDate
});

// Resolving
resolver(null, this);
}
},

/*********************************************************************/

/**
* Clears the timer id.
*
* @method clearTimeout
*/
clearTimeout() {

// Clearing
if (this.timerId) { clearTimeout(this.timerId); }
},

/**
* Set the timer id.
*
* @method setTimeout
*/
setTimeout() {

// Clearing
this.clearTimeout();

// Setting
this.options = options;
this.endDate = this.options.endDate || null;
this.frequency = this.options.frequency || 'precise';
this.interval = this.options.interval || null;
this.iterations = this.frequency !== 'precise' ? this.options.iterations || null : 1;
this.month = this.options.month || null;
this.monthDay = this.options.monthDay || null;
this.startDate = this.options.startDate || new Date();
this.startTime = this.options.startTime || null;
this.week = this.options.week || null;
this.weekDay = this.options.weekDay || null;
this.weekDays = this.options.weekDays || [];

// Adapting
this.adaptee = new RRule({
freq: RRule[this.frequencies[this.frequency]],
interval: this.interval,
count: this.iterations,
byweekday: this.weekDay ? RRule[this.days[this.weekDay]] : this.weekDays.map(day => RRule[this.days[day]]),
bymonth: this.month,
bymonthday: this.monthDay,
byhour: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[1])),
byminute: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[2])),
bysecond: this.startTime && XP.toDefined(XP.toFinite(this.startTime.match(XP.timeRegex)[4])),
bysetpos: this.week && this.weeks[this.week],
dtstart: this.startDate,
until: this.endDate
});
this.timerId = this.setTimeout(this.handler, this.nextDate - Date.now());
},

/*********************************************************************/
Expand Down Expand Up @@ -133,6 +170,30 @@ module.exports = new XP.Class('Task', {
validate(val) { return !this.frequencies[val] && 'string'; }
},

/**
* The function to execute.
*
* @property handler
* @type Function
*/
handler: {
set(val) { return this.handler || val; },
validate(val) { return !XP.isFunction(val) && 'Function'; }
},

/**
* The recurrence's identifier.
*
* By default, an UUID will be assigned.
*
* @property id
* @type string
*/
id: {
set(val) { return this.id || val; },
validate(val) { return !XP.isString(val, true) && 'string'; }
},

/**
* The interval between each recurrence's iteration.
*
Expand Down Expand Up @@ -210,6 +271,18 @@ module.exports = new XP.Class('Task', {
validate(val) { return !XP.isNull(val) && !XP.isTime(val) && 'string'; }
},

/**
* The timer id of the next recurrence's iteration.
*
* @property timerId
* @type number
* @readonly
*/
timerId: {
set(val) { return val; },
validate(val) { return !XP.isInt(val, true) && 'number'; }
},

/**
* The recurrence's week.
*
Expand Down
Loading

0 comments on commit 3cd8554

Please sign in to comment.