This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (172 loc) · 3.61 KB
/
index.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
(function() {
var AsyncMarks = function(options) {
AsyncMarks.func.init.prototype = AsyncMarks.func;
return new AsyncMarks.func.init(options);
}
AsyncMarks.func = AsyncMarks.prototype = {
constructor: AsyncMarks,
init: function(options) {
/**
* init options
*/
options = options || {};
options.timeout = options.timeout || 0;
options.debug = !!options.debug || false;
/**
* vars
*/
this._markStack = {};
this.callback;
/**
* others
*/
if(options.timeout !== 0) {
this.outTimer = setTimeout(function() {
throw new Error("Mark has expired");
}, options.timeout);
}
return this;
},
/**
* add mark
*/
addMark: function(mark) {
var that = this;
mark = mark || Object.keys(that._markStack).length + '_mark';
that._markStack[mark] = false;
return {
complete: function() {
that.completeMark(mark);
}
}
},
/**
* complete mark
*/
completeMark: function(markName) {
if(markName == undefined) {
throw new Error("markName is undefined");
}
this._markStack[markName] = true;
this._compteleCall();
},
/**
* func call with all complete
*/
complete: function(callback) {
this.callback = callback;
if(this.function_list.length == 0) {
this.callback();
}
return callback;
},
/**
* sequence of operation
*/
series: function() {
this._fManager(arguments);
// execute
this._executeSeriesFunc();
},
/**
* _seriesMaster
*/
_seriesMaster: function() {
var that = this;
return {
complete: function() {
that.function_list.splice(0, 1);
that._executeSeriesFunc();
}
}
},
/**
* _executeSeriesFunc
*/
_executeSeriesFunc: function() {
if(this.function_list.length > 0) {
if(!(this.function_list[0] instanceof Function)) {
throw new Error("Argument is not function.");
}
this.function_list[0].call(this._seriesMaster());
}
else {
if(this.callback != undefined) {
this.callback();
}
}
},
/**
* pack
*/
pack: function() {
this._fManager(arguments);
// execute
for(var i=0; i < this.function_list.length; i++) {
this.function_list[i].call(this._packMaster());
}
},
/**
* _packMaster
*/
_packMaster: function() {
var that = this;
return {
complete: function() {
that.function_list.splice(0, 1);
if(that.function_list.length == 0 && that.callback != undefined) {
that.callback();
}
}
}
},
/**
* function manager
*/
_fManager: function(arguments) {
this.function_list = [];
// null
if(arguments.length == 0) {
throw new Error("Arguments is null.");
}
// one function
else if(arguments.length == 1 && arguments[0] instanceof Function) {
this.function_list.push(arguments[0]);
}
// function list
else if(arguments.length == 1 && arguments[0] instanceof Array) {
for(var i=0; i < arguments[0].length; i++) {
this.function_list.push(arguments[0][i]);
}
}
// arguments list
else {
for(var i=0; i < arguments.length; i++) {
this.function_list.push(arguments[i]);
}
}
},
/**
*
*/
_compteleCall: function() {
for (prop in this._markStack) {
if (this._markStack.hasOwnProperty(prop)) {
if(!this._markStack[prop]) {
return false;
}
}
}
if(this.outTimer != undefined) {
clearTimeout(this.outTimer);
}
this.callback();
}
}
if(typeof module !== 'undefined' && module.exports) {
module.exports = AsyncMarks;
}
else {
window['AsyncMarks'] = AsyncMarks;
}
}());