Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules/
.idea/
test.txt
package-lock.json
dist/
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Odigie Oseme U.
Ikpefua Harrison V.
15 changes: 0 additions & 15 deletions lib/Task.js

This file was deleted.

18 changes: 18 additions & 0 deletions lib/Task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Created by osemeodigie on 30/05/2017.
*/
import { TaskRunner } from "./TaskRunner"

// Example Task
interface DoneCallback {
(taskRunner: TaskRunner, time?: number): void;
}

export default function Task($this: TaskRunner, done_callback: DoneCallback): void {
var time: number = Math.random() * 8000;
setTimeout(function() {
console.log('task complete', time);
done_callback($this, time);
}, time);
console.log('task started', time);
}
76 changes: 0 additions & 76 deletions lib/TaskRunner.js

This file was deleted.

69 changes: 69 additions & 0 deletions lib/TaskRunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Created by osemeodigie on 30/05/2017.
*/

export class TaskRunner {
max_task_limit: number;
_currently_running_task: number;
_tasks: Array<(runner: TaskRunner, done: (runner: TaskRunner, time?: number) => void) => void | any>;
_is_task_running: boolean;
currentTask?: (runner: TaskRunner, done: (runner: TaskRunner, time?: number) => void) => void | any;

constructor(throttle_limit?: number) {
this.max_task_limit = (throttle_limit === undefined) ? 2 : throttle_limit;
this._currently_running_task = 0;
this._tasks = [];
this._is_task_running = false;

// Binded the doneTask method to the current instance so this method doesn't lose context when used as a callback
// this.doneTask = this.doneTask.bind(this);
}

private runNextTask(): boolean {
// Check if there are any other pending tasks
if (this._tasks.length === 0) {
console.log("No task found currently on stack");
return false;
}

if (this._currently_running_task >= this.max_task_limit) {
return false;
}

this.currentTask = this._tasks.shift();
if (this.currentTask) {
this._currently_running_task += 1; // Taken a task to run
console.log("\n Scheduling task");
this._is_task_running = true;
this.currentTask(this, this.doneTask); // Execute the task
}
return true;
}

private doneTask($this: TaskRunner, time?: number): void {

// decrement my current task counter...
$this._currently_running_task -= 1;
$this._is_task_running = false;

if ($this._tasks.length > 0) {
$this._is_task_running = true;
$this.runNextTask();
}

if ($this._is_task_running && $this._tasks.length === 0) {
console.log("Finished scheduling all tasks to run...");
}

if (!$this._is_task_running && $this._tasks.length === 0) {
console.log("Finished running all tasks!");
}
}

public addTask(task: (runner: TaskRunner, done: (runner: TaskRunner, time?: number) => void) => void): void {
this._tasks.push(task);
if (this._tasks.length > 0) {
this.runNextTask();
}
}
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"url": "http://osemeodigie.com"
},
"scripts": {
"start": "node task_runner_test.js",
"test": "jest",
"start": "tsc && node dist/task_runner_test.js",
"lint": "jshint **/*.js",
"postmerge": "npm install",
"pretest": "npm run lint"
Expand All @@ -20,6 +21,11 @@
"devDependencies": {
"jshint": "latest",
"should": "^11.0.0",
"supertest": "^2.0.0"
"supertest": "^2.0.0",
"@types/jest": "^29.5.14",
"eslint": "^9.25.1",
"jest": "^29.7.0",
"ts-jest": "^29.3.2",
"typescript": "^5.8.3"
}
}
24 changes: 12 additions & 12 deletions task_runner_test.js → task_runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Created by osemeodigie on 30/05/2017.
*/

var task = require("./lib/Task")
var TaskRunner = require("./lib/TaskRunner")
import task from "./lib/Task"
import { TaskRunner } from "./lib/TaskRunner"

console.log("Started the task runner test file...")

Expand All @@ -21,16 +21,16 @@ taskRunner.addTask(task) // task 7
taskRunner.addTask(task) // task 8
taskRunner.addTask(task) // task 9
taskRunner.addTask(task) // task 10
taskRunner.addTask(task) // task 11
taskRunner.addTask(task) // task 12
taskRunner.addTask(task) // task 13
taskRunner.addTask(task) // task 14
taskRunner.addTask(task) // task 15
taskRunner.addTask(task) // task 16
taskRunner.addTask(task) // task 17
taskRunner.addTask(task) // task 18
taskRunner.addTask(task) // task 19
taskRunner.addTask(task) // task 20
// taskRunner.addTask(task) // task 11
// taskRunner.addTask(task) // task 12
// taskRunner.addTask(task) // task 13
// taskRunner.addTask(task) // task 14
// taskRunner.addTask(task) // task 15
// taskRunner.addTask(task) // task 16
// taskRunner.addTask(task) // task 17
// taskRunner.addTask(task) // task 18
// taskRunner.addTask(task) // task 19
// taskRunner.addTask(task) // task 20


// task started 123.23232
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"],
"include": ["lib/**/*", "task_runner_test.ts"],
}