Skip to content

Commit

Permalink
Added ability to run tasks.
Browse files Browse the repository at this point in the history
* [FEATURE] Added ability to add tasks that can run one or more times on an interval with an optional delay.
* [DOCS] Added documentation about tasks.
* [TEST] Added global to mocha setup to prevent an irrelevant error from displaying.
  • Loading branch information
robertcorponoi committed Jan 23, 2020
1 parent 212595c commit 8adf628
Show file tree
Hide file tree
Showing 19 changed files with 1,158 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.3.0 / 2020-01-23
==================
* [FEATURE] Added ability to add tasks that can run one or more times on an interval with an optional delay.
* [DOCS] Added documentation about tasks.
* [TEST] Added global to mocha setup to prevent an irrelevant error from displaying.

1.2.0 / 2020-01-14
==================
* [FEATURE] Added `time` as a getter.
Expand Down
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,52 @@ if (someConditionThatEndsTheAnimation) {
}
```

## **Tasks**

Tasks are functions that can be assigned to run one or more times on an interval.

**Note:** Tasks are not guaranteed to run at the exact time you wish them to. For example, if you have a task that runs every second, you cannot expect it to run at 1000ms, 2000ms, 3000ms on the dot because the timing is decided by requestAnimationFrame. The task will run at the closest possible time to the expected time.

### **addTask**

| param | type | description | default |
|--------------------|----------|-------------------------------------------------------------------------------------------|----------|
| name | string | The name of the task to add. | |
| fn | Function | The function to call when this task is run. | |
| options | Object | | |
| options.interval | number | Specifies the time in between runs of this task. | 1000 |
| options.delay | number | An initial delay before running this task for the first time. | 0 |
| options.timesToRun | number | Specify this to have the task be destroyed after being run the specified amount of times. | Infinity |

**example:**

```js
const task = () => { return 'hello world!'; }

// Running a task every 1 second:
deltaframe.tasks.addTask('test', task, { interval: 1000 });

// Running a task every 1 second but waiting 2.5 seconds before the first run.
deltaframe.tasks.addTask('test', task, { interval: 1000, delay: 2500 });

// Running a task every 1 second but only twice after which it gets removed automatically.
deltaframe.tasks.addTask('test', task, { interval: 1000, timesToRun: 2 });
```

### **removeTask**

| param | type | description | default |
|--------------------|----------|-------------------------------------------------------------------------------------------|----------|
| name | string | The name of the task to remove. | |

```js
const task = () => { return 'hello world!'; }

deltaframe.tasks.addTask('test', task, { interval: 1000 });

deltaframe.tasks.removeTask('test');
```

## **Tests**

The tests for Deltaframe are browser based so to run them you will first need to start the local testing server like so:
Expand Down
220 changes: 220 additions & 0 deletions deltaframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,204 @@ function () {
return Options;
}();

var TaskOptions =
/**
* Specifies the time in between runs.
*
* @property {number}
*
* @default 1000
*/

/**
* A delay before running the task for the first time.
*
* @property {number}
*
* @default 0
*/

/**
* Specify this to have the task be destroyed after being run the specified amount of times.
*
* @property {number}
*
* @default Infinity
*/

/**
* @param {Object} options The options passed when creating a new task.
*/
function TaskOptions(options) {
_classCallCheck(this, TaskOptions);

_defineProperty(this, "interval", 1000);

_defineProperty(this, "delay", 0);

_defineProperty(this, "timesToRun", Infinity);

Object.assign(this, options);
};

/**
* Defines a task that can be created and added to the task manager.
*/

var Task =
/*#__PURE__*/
function () {
/**
* The name of this task.
*
* @property {string}
*/

/**
* A reference to the function to call when this task is run.
*
* @property {Function}
*/

/**
* A reference to the options for this task.
*
* @property {TaskOptions}
*/

/**
* The number of times that this task has been run.
*
* @property {number}
*/

/**
* The time this task was last run at.
*
* @property {number}
*/

/**
* @param {string} name The name of this task.
* @param {Function} fn The function to call when this task is run.
* @param {Object} options The options for this task.
*/
function Task(name, fn, options) {
_classCallCheck(this, Task);

_defineProperty(this, "name", void 0);

_defineProperty(this, "fn", void 0);

_defineProperty(this, "options", void 0);

_defineProperty(this, "timesRun", 0);

_defineProperty(this, "lastRunAt", 0);

this.name = name;
this.fn = fn;
this.options = new TaskOptions(options);
}
/**
* Runs the function associated with this task.
*/


_createClass(Task, [{
key: "run",
value: function run() {
this.fn();
this.timesRun++;
}
}]);

return Task;
}();

/**
* The task manager is used to add and manage tasks that are supposed to run at specific times, on repeat, or a
* predetermined number of times.
*/

var TaskManager =
/*#__PURE__*/
function () {
function TaskManager() {
_classCallCheck(this, TaskManager);

_defineProperty(this, "_active", []);
}

_createClass(TaskManager, [{
key: "addTask",

/**
* Adds a task to the task manager.
*
* @param {string} name The name of the task to add.
* @param {string} fn The function to call when this task is run.
* @param {Object} [options]
* @param {number} [options.interval=1000] Specifies the time in between runs.
* @param {number} [options.delay=0] A delay before running the task for the first time.
* @param {number} [options.timesToRun=Infinity] Specify this to have the task be destroyed after being run the specified amount of times.
*/
value: function addTask(name, fn) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var task = new Task(name, fn, options);

this._active.push(task);
}
/**
* Removes a task by its name.
*
* @param {string} name The name of the task to remove.
*/

}, {
key: "removeTask",
value: function removeTask(name) {
this._active = this._active.filter(function (task) {
return task.name !== name;
});
}
/**
* Checks to see if any tasks need to be run and runs them if so.
*
* This will also remove tasks if they are no longer needed.
*
* @param {number} time The current timestamp.
*/

}, {
key: "update",
value: function update(time) {
var _this = this;

this.active.map(function (task) {
if (time > task.options.delay && time - task.lastRunAt >= task.options.interval) {
task.run();
task.lastRunAt = time;
if (task.timesRun > task.options.timesToRun) _this.removeTask(task.name);
}
});
}
}, {
key: "active",

/**
* Returns all of the active tasks.
*
* @returns {Array<Tas>}
*/
get: function get() {
return this._active;
}
}]);

return TaskManager;
}();

var RequestAnimationFrame =
/*#__PURE__*/
function () {
Expand Down Expand Up @@ -404,6 +602,14 @@ function () {
* @property {document.hidden}
*/

/**
* A reference to the task manager.
*
* @private
*
* @property {TaskManager}
*/

/**
* @param {Object} [options] The options to pass to this Deltaframe instance.
* @param {number} [options.minFps=15] The minimum fps value allowed before Deltaframe will restart to try to correct the issue.
Expand Down Expand Up @@ -443,6 +649,8 @@ function () {

_defineProperty(this, "_hidden", void 0);

_defineProperty(this, "_tasks", new TaskManager());

this._options = new Options(options);
this._restartAttempts = 0;
this._running = false;
Expand Down Expand Up @@ -603,6 +811,7 @@ function () {

this._fn(timestamp, this._delta, this._deltaAverage);

if (this._tasks.active.length > 0) this._tasks.update(this.time);
this._prevTime = timestamp;
}
}
Expand Down Expand Up @@ -668,6 +877,17 @@ function () {
get: function get() {
return this._time;
}
/**
* Returns a reference to the task manager.
*
* @returns {TaskManager}
*/

}, {
key: "tasks",
get: function get() {
return this._tasks;
}
}]);

return Deltaframe;
Expand Down
15 changes: 15 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import TaskManager from './tasks/TaskManager';
/**
* Deltaframe is an animation and game loop manager that makes sure your application is punctual and performant.
*/
Expand Down Expand Up @@ -115,6 +116,14 @@ export default class Deltaframe {
* @property {document.hidden}
*/
private _hidden;
/**
* A reference to the task manager.
*
* @private
*
* @property {TaskManager}
*/
private _tasks;
/**
* @param {Object} [options] The options to pass to this Deltaframe instance.
* @param {number} [options.minFps=15] The minimum fps value allowed before Deltaframe will restart to try to correct the issue.
Expand Down Expand Up @@ -154,6 +163,12 @@ export default class Deltaframe {
* @returns {DOMHighResTimeStamp|number}
*/
get time(): (DOMHighResTimeStamp | number);
/**
* Returns a reference to the task manager.
*
* @returns {TaskManager}
*/
get tasks(): TaskManager;
/**
* Start the loop.
*
Expand Down
Loading

0 comments on commit 8adf628

Please sign in to comment.