-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.js
82 lines (74 loc) · 1.48 KB
/
sym.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
var sym = function(){
this.init();
};
sym.prototype.init = function(){
this.order = [];
this.stack = {};
this.ptr = 0
this.callback = null;
}
sym.prototype.push = function(name,fn,args,callback){
if (false == name in this.order){
this.stack[name] = {
"fn":fn, //调用的函数
"args":args, //函数参数
"callback":callback,
"data":{}, //暂存数据(output)
"index": this.order.length //索引
};
this.order.push(name);
}
else{
this.stack[name].fn = fn;
this.stack[name].args = args;
}
};
sym.prototype.next = function(data){
//save data,call callback function
var name = this.order[this.ptr];
this.stack[name].data = data;
try{
if( this.stack[name].callback)
this.stack[name].callback(data);
}
catch(e){
this.callback ? this.callback(e,data) : null;
}
this.ptr += 1;
if(this.ptr < this.order.length){
//call next
name = this.order[this.ptr];
try{
this.run(name);
}
catch(e){
this.callback ? this.callback(e,data) : null;
}
}
else{
this.callback ? this.callback(null,data) : null;
}
};
sym.prototype.done = function(callback){
this.callback = callback;
}
sym.prototype.run = function(name){
var fn = this.stack[name].fn;
var args = this.stack[name].args
try{
fn.apply(this,args);
}
catch(e){
this.callback ? this.callback(e,null) : null;
}
};
sym.prototype.clear = function(){
this.init();
}
var _sym = window.sym;
sym.noConflict = function(){
if(window.sym === sym){
window.sym = _sym;
}
return sym;
}