-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (107 loc) · 2.76 KB
/
index.js
File metadata and controls
127 lines (107 loc) · 2.76 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
/* .#.
@@@@@
@@@@@
@
.....@@@
.@@@@@@@
@@@@@@@
@@@@@@@:@@@..@@@@@@@ @@@ @@@ @@@@@@@@ @@@@@@@ @@@@
.@@@@@@@@ @@@@@@@@ @@@ @@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@
'@@@@@@@@@@@@@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
:@@@@@@@@@: @@@@@@@@ @@@ @@@ @@@@@@@@@ @@@@@@@@@ @@@ @@@
`@@@@: @@@ @@@ @@@ @@@#@@@ @@@@@@@@ @@@@@@@@@@
@@@ @@@@@@@@@ @@@@@@@@' @@@ @@@ @@@ @@@ @@@
+##` @@@@@@@@ @@@@@ @@@ @@@ @@@ @@@ @@@
Supra
NodeJS
Developer
Friendly
Framework.
Queue Class File
*/
module.exports = Supra.Class.extend({
/**
* [init description]
* @param {[type]} params [description]
* @return {[type]} [description]
*/
init:function(params){
this.autoExec = (params.autoExec) ? params.autoExec : false;
this.callback = (params.callback) ? params.callback : function(){console.log('Queue Finish')};
this.context = (params.context) ? params.context : this;
this.processes = []
this.date = new Date();
this.completedProcesses = 0;
this.waiting = false;
},
/**
* [enqueue description]
* @param {[type]} process [description]
* @param {[type]} sync [description]
* @return {[type]} [description]
*/
enqueue:function(process,sync){
this.processes.push({
process : process,
sync : sync
})
return this
},
/**
* [run description]
* @param {[type]} context [description]
* @return {[type]} [description]
*/
run : function(context){
if (this.processes.length == 0){
this.callback.call(this.context)
}else{
this.runContext = context
this.loop(0,context)
}
return this
},
/**
* [resume description]
* @param {[type]} index [description]
* @return {[type]} [description]
*/
resume:function(index){
this.loop(index,this.runContext)
},
/**
* [finish description]
* @return {[type]} [description]
*/
finish : function(){
this.completedProcesses+=1;
if (this.completedProcesses == this.processes.length){
this.callback.call(this.context)
}else{
if (this.waiting.waiting == true || this.waiting.processes == this.completedProcesses){
this.processes[this.waiting.processes].sync = false;
this.resume(this.waiting.processes)
this.waiting.waiting = false;
}
}
},
/**
* [loop description]
* @param {[type]} index [description]
* @param {[type]} context [description]
* @return {[type]} [description]
*/
loop :function(index,context){
for (var i = index; i <= this.processes.length-1; i++) {
if (this.processes[i]['sync']){
this.waiting = {
waiting : true,
processes : i
}
break;
}else{
this.processes[i]['process'].call(context)
}
};
}
})