forked from AlloyTeam/PhyTouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloy_flow.js
133 lines (119 loc) · 3.6 KB
/
alloy_flow.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
/* AlloyFlow 0.1.0
* By AlloyTeam
* https://github.com/AlloyTeam/AlloyFlow
*/
var AlloyFlow = function (option) {
this.start_time = null;
this.workflow = option.workflow;
this.wf_len = this.workflow.length;
this.timer = null;
this.interval = option.interval || 15;
this.onStart = option.onStart || function () { };
this.onProgress = option.onProgress || function () { };
this.onEnd = option.onEnd || function () { };
if (typeof this.workflow[0] === "function") {
this.stopSeries = false;
this.isSeries = true;
this.workIndex = 0;
} else {
var i=0,
j,
startLen;
for (; i < this.wf_len; i++) {
var start = this.workflow[i].start;
if (typeof start !== "number") {
j=0;
startLen=start.length;
for (; j < startLen; j++) {
this.workflow.splice(i, 0, { work: this.workflow[i].work, start: start[j] });
}
i += startLen;
this.workflow.splice(i--, 1);
this.wf_len--;
this.wf_len += startLen;
}
}
}
};
AlloyFlow.prototype = {
start: function () {
this.onStart();
if (this.isSeries) {
this.stopSeries = false;
this.workIndex = 0;
this._startSeries();
return;
}
var self = this;
this._forEach(this.workflow,function (item) {
item.done = false;
});
this.start_time = new Date();
clearInterval(this.timer);
this.timer = setInterval(function () {
var doneCount = 0;
self._forEach(self.workflow, function (w) {
var currentTime = new Date() - self.start_time ;
if (currentTime >= w.start) {
if (!w.done) {
w.done = true;
w.work.call(self,currentTime);
self.onProgress();
} else {
doneCount++;
if (doneCount === self.wf_len) {
clearInterval(self.timer);
self.onEnd();
}
}
}
});
}, this.interval);
},
_startSeries: function () {
this.workflow[this.workIndex].call(this);
},
_forEach: function (arr,callback) {
if (Array.prototype.forEach) return arr.forEach(callback);
var i = 0,
len = arr.length;
for (; i < len; i++) {
callback(arr[i]);
}
},
stop: function () {
if (this.isSeries) {
this.stopSeries = true;
} else {
clearInterval(this.timer);
}
},
next: function (msg , delay) {
if(arguments.length === 2){
var self = this;
setTimeout(function(){
self._nextTask(msg);
},delay);
}else{
this._nextTask(msg);
}
},
callback: function (){
console.log(arguments);
this._nextTask.apply(this,arguments);
},
_nextTask:function(){
if (this.stopSeries) return;
this.workIndex++;
this.workflow[this.workIndex].apply(this,arguments);
this.onProgress();
if (this.workIndex === this.wf_len - 1) {
this.workIndex = 0;
this.stopSeries = true;
this.onEnd();
}
}
};
if (typeof module !== 'undefined' && typeof exports === 'object') {
module.exports = AlloyFlow;
}