-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzen.js
More file actions
158 lines (147 loc) · 3.86 KB
/
zen.js
File metadata and controls
158 lines (147 loc) · 3.86 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
156
157
158
/**
* Zen
*/
// environment
var env = process.env.NODE_ENV || 'development';
/**
* Log error using console.error.
*/
function logerror(err){
if (env !== 'test') console.error("Error:",err.stack || err.toString());
}
/**
* Default error handler
*/
var errorHandler = function error(/*args,*/ /*err*/) {
var err=(arguments.length >0)?arguments[arguments.length-1]:null;
if (typeof err == "undefined" || !err)
return logerror("This is the end, with no result given");
logerror(err);
return;
};
/**
* Default result handler
*/
var resultHandler = function result(/*args,*/ /*res*/) {
var res=(arguments.length >0)?arguments[arguments.length-1]:null;
if (res)
console.log(res);
return;
};
/**
* Zen uses a 'setup' pattern, returning a callable engine function
*/
module.exports= function (/*handlers*/) {
/**
* Default handler delegates to the error handler
*/
var defaultHandler = function(/*args*/) {
var args= Array.prototype.slice.call(arguments);
try {
args[args.length-1]=undefined;//err
return engine.errorHandler.apply(this,args);
} catch (ex) {
args[args.length-1]=ex;//err
return errorHandler.apply(this,args);
}
};
var handlers=Array.prototype.slice.call(arguments);
handlers.push(defaultHandler);
var _engineRequests=[];
var _enginePaused=false;
var _engineStopped=false;
var firstM=handlers[0];
// The real Zen Engine
var engine= function (/*handleArgs*/) {
var handleArgs=Array.prototype.slice.call(arguments);
var handleArgsLength=handleArgs.length;
if (_enginePaused || _engineStopped){
if (_enginePaused) {_engineRequests.push(handleArgs);return;}
handleArgs.push(undefined); return defaultHandler.apply(this,handleArgs);
}
var i=1;
var self=this;
try {
//handler optimization
var handle;
var ha0,ha1,ha2,ha3;
switch (handleArgsLength) {
// faster
case 0:
handle= function(_handler){
return _handler.call(self,next);
};
break;
case 1:
ha0=handleArgs[0];
handle= function(_handler){
return _handler.call(self,ha0,next);
};
break;
case 2:
ha0=handleArgs[0];ha1=handleArgs[1];
handle= function(_handler){
return _handler.call(self,ha0,ha1, next);
};
break;
case 3:
ha0=handleArgs[0];ha1=handleArgs[1];ha2=handleArgs[2];
handle= function(_handler){
return _handler.call(self,ha0,ha1,ha2,next);
};
break;
case 4:
ha0=handleArgs[0];ha1=handleArgs[1];ha2=handleArgs[2];ha3=handleArgs[3];
handle= function(_handler){
return _handler.call(self,ha0,ha1,ha2,ha3, next);
};
break;
// slower
default:
handle= function(_handler){
return _handler.apply(self, handleArgs);
}
break;
}
function next (err,res) {
if(res || err){
if (res) {
handleArgs[handleArgsLength]=res;
return engine.resultHandler.apply(this,handleArgs);
}
handleArgs[handleArgsLength]=err;
return engine.errorHandler.apply(this,handleArgs);
}
return handle(handlers[i++]);
}
handleArgs.push(next);
return handle(firstM);
} catch (err) {
try {
handleArgs[handleArgsLength]=err;
return engine.errorHandler.apply(this,handleArgs);
} catch (ex) {
handleArgs[handleArgsLength]=ex;
return errorHandler.apply(this,handleArgs);
}
}
}
/*if (handlers.length==1)
engine=defaultHandler; //default */
engine.errorHandler = errorHandler;
engine.resultHandler = resultHandler;
/*EXTRA FEATURE*/
engine.pause=function (){_enginePaused=true;};
engine.stop =function (){_engineStopped=true;};
engine.resume=function(){
_enginePaused=_engineStopped=false;
var requests= Array.prototype.slice.call(_engineRequests);
_engineRequests=[];
var request=requests.shift();
while(typeof request !=='undefined'){
engine.apply(this,request);
request=requests.shift();
}
};
return engine;
};