-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtaskPromise.js
More file actions
155 lines (131 loc) · 4.54 KB
/
taskPromise.js
File metadata and controls
155 lines (131 loc) · 4.54 KB
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
(function($){
$.getScript("https://rawgithub.com/KanbanSolutions/Math.uuid.js/master/Math.uuid.js");
var Task = function(start_method, call_args) {
this.id = Math.uuidFast();
this.steps = [];
this.then(start_method, call_args);
this.finished = false;
};
Task.prototype.then = function(step_method, call_args) {
var defered = $.Deferred();
defered.then(this.Step(step_method, call_args));
this.steps.push(defered);
return this;
};
Task.prototype.insert = function(step_method, call_args) {
//console.log("then: " + call_args);
var defered = $.Deferred();
defered.then(this.Step(step_method, call_args));
this.steps.splice(1, 0, defered);
return this;
};
Task.prototype.Step = function(step_method, call_args) {
var self = this;
var wrap = function() {
var args = call_args || [];
//console.log("wrap for " + self.id);
if(!args.length && arguments.length) {
args = [].slice.call(arguments);
}
if(!$.isArray(args)){
args = $.makeArray(args);
}
args.push(self);
var results = step_method.apply(self, args);
if(this.finished) return;
var delay = results === undefined || results.delay === undefined ? 0 : results.delay;
//console.log("[results=" + results + "] [steps.length=" + this.steps.length + "]");
if(results === undefined && self.steps.length === 0){
/*
I don't know why I need to bounce out here, it fails if I don't.
*/
//console.log("exiting on thingy");
return;
}
if(results !== undefined && results.error) {
//console.log("retry needed");
self.retry.call(self, delay, step_method, args);
} else {
//console.log("no retry needed");
if(!results) results = {};
//console.log('promise: ' + call_args);
//console.log('promise: ' + args);
args = results.args || [];
args.unshift(delay);
self.steps.shift();
self.progress.apply(self, args);
}
};
return wrap;
};
Task.prototype.retry = function(delay, step_method, args) {
//console.log("retry");
this.steps.shift();
var defered = $.Deferred();
defered.then(this.Step(step_method, args));
this.steps.unshift(defered);
this.progress(delay);
return this;
};
Task.prototype.progress = function(delay) {
if(!delay){
delay = 0;
}
var args = [].slice.call(arguments);
var self = this;
//Moving from progress
if(!($.task.executing === self.id || $.task.executing === null)) {
setTimeout(function() {
self.progress.apply(self, args);
}, Math.max(delay, 3000));
return;
}
$.task.executing = this.id;
args.shift();
var execute = function() {
//console.log("execute");
if(!self.steps.length) {
self.finish();
return;
}
var defered = self.steps[0];
defered.resolve.apply(null, args);
}
if(delay > 0) {
setTimeout(execute, delay);
} else {
requestAnimationFrame(execute);
}
return this;
};
Task.prototype.start_in = function(delay) {
if(!delay){
delay = 0;
}
var args = [].slice.call(arguments);
var self = this;
args.shift(); //remove the delay from the args
setTimeout(function(){
self.progress.apply(self, args);
}, delay);
return this;
}
Task.prototype.finish = function() {
//console.log("finish");
var self = this;
this.steps = [];
$.task.executing = null;
this.finished = true;
requestAnimationFrame(function(){
delete self;
});
};
$.extend(true, {
task: {
create: function(start_method, call_args) {
return new Task(start_method, call_args);
},
executing: null
}
});
}(jQuery));