-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
200 lines (174 loc) · 5.2 KB
/
tasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//Tasks.js created by Eugene Mankovski
//https://github.com/emankovski/tasksjs.git
//License: MIT
window.allTasks = [];
function MarkTaskAsCompleted(task) {
task.status = "Completed";
}
function generateGUID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function TaskObject(callback, name, dependsOnTasks, args) {
this.callback = callback;
this.dependsOnTasks = dependsOnTasks;
this.status = "Created";
this.result = null;
this.error = null;
this.args = args;
this.taskNumber = -1;
this.debug = false;
this.taskFunction = null;
this.finishedFunction = null;
this.AddTaskFunction = function (f) {
this.taskFunction = f;
}
this.AddFinishedFunction = function (f) {
this.finishedFunction = f;
}
this.AddDependantTask = function (task) {
if (this.dependsOnTasks == null) {
this.dependsOnTasks = [];
}
this.dependsOnTasks.push(task);
}
this.MarkTaskComplete = function () {
this.status = "Completed";
};
//Use guid is name is not provided
if (!name) {
this.name = generateGUID();
} else {
this.name = name;
}
return this;
}
function ClearTaskArrays() {
allTasks = new Array();
}
function TaskExists(name) {
if (allTasks) {
var len = allTasks.length;
for (var ii = 0; ii < len; ii++) {
if (allTasks[ii].name == name) {
return true;
}
}
}
return false;
}
function NewTask(callback, depends) {
var genericTask = new TaskObject(function (name, args, task) {
try {
callback();
}
catch (e) { }
finally { task.status = "Completed"; }
}, "GenericTask", depends, null);
allTasks.push(genericTask);
return genericTask;
}
function GetTask(name) {
if (allTasks) {
var len = allTasks.length;
for (var ii = 0; ii < len; ii++) {
if (allTasks[ii].name == name) {
return allTasks[ii];
}
}
}
return null;
}
function WorkerProc() {
//console.info("Worker alive");
if (allTasks.length > 0) {
var postponedTasks = new Array();
var task = allTasks.pop();
while (task) {
//console.info("Task located: " + task.name);
//Those tasks which are in progress we still consider as active
if (task.status == "InProgress") {
postponedTasks.push(task);
task = allTasks.pop();
continue;
}
//Completed means we can stop listening to them and release
if (task.status == "Completed") {
task = allTasks.pop();
continue;
}
if (task.dependsOnTasks) {
var isPosponedTask = false;
var len = task.dependsOnTasks.length;
for (var i = 0; i < len; i++) {
var dependantTask = task.dependsOnTasks[i];
if (allTasks.indexOf(dependantTask) >= 0 || postponedTasks.indexOf(dependantTask) >= 0) {
isPosponedTask = true;
break;
}
}
if (isPosponedTask) {
//Save this task and continue
postponedTasks.push(task);
task = allTasks.pop();
continue;
}
}
task.status = "InProgress";
try {
if (!task.name) {
console.error("Task must have name!");
} else {
task.callback(task.name, task.args, task);
}
} catch (e) {
task.status = "Completed";
task.error = e;
}
//We need to come back to this task second time and check if it is completed
postponedTasks.push(task);
task = allTasks.pop();
}
if (postponedTasks.length > 0) {
allTasks = allTasks.concat(postponedTasks);
}
} else {
//console.info("No tasks");
}
}
function WorkerTask(func, error_callback, ok_callback) {
var localTask = new TaskObject(
function (name, args, task) {
//
var ReportError = function (message) {
console.error('Worker Task Error was ' + message);
MarkTaskAsCompleted(task);
if (error_callback) {
error_callback(message);
}
}
var ReportOk = function () {
console.info('Worker Task Completed...');
MarkTaskAsCompleted(task);
if (ok_callback) {
ok_callback();
}
}
try {
//Actual execution
func();
} catch (e) {
console.error(e);
} finally {
MarkTaskAsCompleted(task);
}
}, "", null, null);
allTasks.push(localTask);
return localTask;
}
window.setInterval(WorkerProc, 10);