Skip to content

Commit

Permalink
Update Task.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
onury committed Jan 8, 2019
1 parent e30fe83 commit 50de4a0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DEFAULT_TASK_OPTIONS: ITaskOptions = Object.freeze({
/**
* Represents the class that holds the configurations and the callback function
* required to run a task.
* @class
*/
class Task {

Expand Down Expand Up @@ -58,6 +59,7 @@ class Task {

/**
* Gets the unique ID of the task.
* @name Task#id
* @type {string}
* @readonly
*/
Expand All @@ -69,6 +71,7 @@ class Task {
* Specifies whether this task is currently enabled. This essentially gives
* you a manual control over execution. The task will always bypass the
* callback while this is set to `false`.
* @name Task#enabled
* @type {boolean}
*/
get enabled(): boolean {
Expand All @@ -81,6 +84,7 @@ class Task {
/**
* Gets or sets the number of ticks to allow before running the task for
* the first time.
* @name Task#tickDelay
* @type {number}
*/
get tickDelay(): number {
Expand All @@ -95,6 +99,7 @@ class Task {
* is "ticks" (not milliseconds). For instance, if the timer interval is
* `1000` milliseconds, and we add a task with `5` tick intervals. The task
* will run on every `5` <b>seconds</b>.
* @name Task#tickInterval
* @type {number}
*/
get tickInterval(): number {
Expand All @@ -107,6 +112,7 @@ class Task {
/**
* Gets or sets the total number of times the task should be run. `0` or
* `null` means unlimited (until the timer has stopped).
* @name Task#totalRuns
* @type {number}
*/
get totalRuns(): number {
Expand All @@ -120,6 +126,7 @@ class Task {
* Specifies whether to wrap callback in a `setImmediate()` call before
* executing. This can be useful if the task is not doing any I/O or using
* any JS timers but synchronously blocking the event loop.
* @name Task#immediate
* @type {boolean}
*/
get immediate(): boolean {
Expand All @@ -131,6 +138,7 @@ class Task {

/**
* Gets the number of times, this task has been run.
* @name Task#currentRuns
* @type {number}
* @readonly
*/
Expand All @@ -143,6 +151,7 @@ class Task {
* `#time.started` indicates the first execution time of a task.
* `#time.stopped` indicates the last execution time of a task. (`0` if still running.)
* `#time.elapsed` indicates the total lifetime of a task.
* @name Task#time
* @type {ITimeInfo}
* @readonly
*/
Expand All @@ -158,6 +167,7 @@ class Task {

/**
* Gets the callback function to be executed on each run.
* @name Task#callback
* @type {TaskCallback}
* @readonly
*/
Expand All @@ -169,6 +179,7 @@ class Task {
* Gets or sets whether to remove the task (to free up memory) when task
* has completed its executions (runs). For this to take affect, the task
* should have `totalRuns` and/or `stopDate` configured.
* @name Task#removeOnCompleted
* @type {boolean}
*/
get removeOnCompleted(): boolean {
Expand All @@ -183,6 +194,7 @@ class Task {
* `stopDate` is reached. Note that if both `totalRuns` and `stopDate` are
* omitted, this will never return `true`; since the task has no execution
* limit set.
* @name Task#completed
* @type {boolean}
* @readonly
*/
Expand All @@ -195,6 +207,8 @@ class Task {

/**
* Specifies whether the task can run on the current tick of the timer.
* @private
* @name Task#canRunOnTick
* @type {boolean}
* @readonly
*/
Expand All @@ -211,6 +225,7 @@ class Task {
/**
* Resets the current number of runs. This will keep the task running for
* the same amount of `tickIntervals` initially configured.
* @memberof Task
* @chainable
*
* @param {ITaskBaseOptions} [options] If set, this will also re-configure the task.
Expand Down Expand Up @@ -259,9 +274,9 @@ class Task {
/**
* @private
*/
private _emit(type: TaskTimer.EventType, object: Task | Error): void {
private _emit(name: TaskTimer.Event, object: Task | Error): void {
const event: ITaskTimerEvent = {
type,
name,
source: this
};
/* istanbul ignore else */
Expand All @@ -270,7 +285,7 @@ class Task {
} else {
event.data = object;
}
this._timer.emit(type, event);
this._timer.emit(name, event);
}

/**
Expand Down Expand Up @@ -299,13 +314,13 @@ class Task {
this._done();
})
.catch((err: Error) => {
this._emit(TaskTimer.EventType.TASK_ERROR, err);
this._emit(TaskTimer.Event.TASK_ERROR, err);
});
} else {
this._done();
}
} catch (err) {
this._emit(TaskTimer.EventType.TASK_ERROR, err);
this._emit(TaskTimer.Event.TASK_ERROR, err);
}
}

Expand Down

0 comments on commit 50de4a0

Please sign in to comment.