@@ -29,144 +29,96 @@ module.exports = {
29
29
* @param {Function } the method
30
30
* @param {Function } the error handler callback
31
31
*/
32
- hook : function ( name , fn , err ) {
32
+ hook : function ( name , fn , errorCb ) {
33
33
if ( arguments . length === 1 && typeof name === 'object' ) {
34
34
for ( var k in name ) { // `name` is a hash of hookName->hookFn
35
35
this . hook ( k , name [ k ] ) ;
36
36
}
37
37
return ;
38
38
}
39
39
40
- if ( ! err ) err = fn ;
40
+ if ( ! errorCb ) errorCb = fn ;
41
41
42
42
var proto = this . prototype || this
43
43
, pres = proto . _pres = proto . _pres || { }
44
44
, posts = proto . _posts = proto . _posts || { } ;
45
45
pres [ name ] = pres [ name ] || [ ] ;
46
46
posts [ name ] = posts [ name ] || [ ] ;
47
47
48
- function noop ( ) { }
49
-
50
- // NEW CODE
51
48
proto [ name ] = function ( ) {
52
49
var self = this
50
+ , hookArgs // arguments eventually passed to the hook - are mutable
53
51
, pres = this . _pres [ name ]
54
52
, posts = this . _posts [ name ]
55
- , numAsyncPres = 0
56
- , hookArgs = [ ] . slice . call ( arguments )
57
- , preChain = pres . map ( function ( pre , i ) {
58
- var wrapper = function ( ) {
59
- if ( arguments [ 0 ] instanceof Error )
60
- return err ( arguments [ 0 ] ) ;
61
- if ( numAsyncPres ) {
62
- // arguments[1] === asyncComplete
63
- if ( arguments . length )
64
- hookArgs = [ ] . slice . call ( arguments , 2 ) ;
65
- pre . apply ( self ,
66
- [ preChain [ i + 1 ] || allPresInvoked ,
67
- asyncComplete
68
- ] . concat ( hookArgs )
69
- ) ;
70
- } else {
71
- if ( arguments . length )
72
- hookArgs = [ ] . slice . call ( arguments ) ;
73
- pre . apply ( self ,
74
- [ preChain [ i + 1 ] || allPresDone ] . concat ( hookArgs ) ) ;
75
- }
76
- } ; // end wrapper = function () {...
77
- if ( wrapper . isAsync = pre . isAsync )
78
- numAsyncPres ++ ;
79
- return wrapper ;
80
- } ) ; // end posts.map(...)
81
- function allPresInvoked ( ) {
82
- if ( arguments [ 0 ] instanceof Error )
83
- err ( arguments [ 0 ] ) ;
84
- }
85
-
86
- function allPresDone ( ) {
87
- if ( arguments [ 0 ] instanceof Error )
88
- return err ( arguments [ 0 ] ) ;
89
- if ( arguments . length )
90
- hookArgs = [ ] . slice . call ( arguments ) ;
91
- fn . apply ( self , hookArgs ) ;
92
- var postChain = posts . map ( function ( post , i ) {
93
- var wrapper = function ( ) {
53
+ , _total = pres . length
54
+ , _current = - 1
55
+ , _asyncsLeft = proto [ name ] . numAsyncPres
56
+ , _next = function ( ) {
94
57
if ( arguments [ 0 ] instanceof Error )
95
- return err ( arguments [ 0 ] ) ;
96
- if ( arguments . length )
97
- hookArgs = [ ] . slice . call ( arguments ) ;
98
- post . apply ( self ,
99
- [ postChain [ i + 1 ] || noop ] . concat ( hookArgs ) ) ;
100
- } ; // end wrapper = function () {...
101
- return wrapper ;
102
- } ) ; // end posts.map(...)
103
- if ( postChain . length ) postChain [ 0 ] ( ) ;
104
- }
105
-
106
- if ( numAsyncPres ) {
107
- complete = numAsyncPres ;
108
- function asyncComplete ( ) {
109
- if ( arguments [ 0 ] instanceof Error )
110
- return err ( arguments [ 0 ] ) ;
111
- -- complete || allPresDone . call ( this ) ;
58
+ return errorCb ( arguments [ 0 ] ) ;
59
+ var _args = Array . prototype . slice . call ( arguments )
60
+ , currPre
61
+ , preArgs ;
62
+ if ( _args . length ) hookArgs = _args ;
63
+ if ( ++ _current < _total ) {
64
+ currPre = pres [ _current ]
65
+ if ( currPre . isAsync && currPre . length < 2 )
66
+ throw new Error ( "Your pre must have next and done arguments -- e.g., function (next, done, ...)" ) ;
67
+ if ( currPre . length < 1 )
68
+ throw new Error ( "Your pre must have a next argument -- e.g., function (next, ...)" ) ;
69
+ preArgs = ( currPre . isAsync
70
+ ? [ _next , _asyncsDone ]
71
+ : [ _next ] ) . concat ( hookArgs ) ;
72
+ return currPre . apply ( self , preArgs ) ;
73
+ } else if ( ! proto [ name ] . numAsyncPres ) {
74
+ return _done . apply ( self , hookArgs ) ;
75
+ }
76
+ }
77
+ , _done = function ( ) {
78
+ var args_ = Array . prototype . slice . call ( arguments )
79
+ , ret , total_ , current_ , next_ , done_ , postArgs ;
80
+ if ( _current === _total ) {
81
+ ret = fn . apply ( self , args_ ) ;
82
+ total_ = posts . length ;
83
+ current_ = - 1 ;
84
+ next_ = function ( ) {
85
+ if ( arguments [ 0 ] instanceof Error )
86
+ return errorCb ( arguments [ 0 ] ) ;
87
+ var args_ = Array . prototype . slice . call ( arguments , 1 )
88
+ , currPost
89
+ , postArgs ;
90
+ if ( args_ . length ) hookArgs = args_ ;
91
+ if ( ++ current_ < total_ ) {
92
+ currPost = posts [ current_ ]
93
+ if ( currPost . length < 1 )
94
+ throw new Error ( "Your post must have a next argument -- e.g., function (next, ...)" ) ;
95
+ postArgs = [ next_ ] . concat ( hookArgs ) ;
96
+ return currPost . apply ( self , postArgs ) ;
97
+ }
98
+ } ;
99
+ if ( total_ ) return next_ ( ) ;
100
+ return ret ;
101
+ }
102
+ } ;
103
+ if ( _asyncsLeft ) {
104
+ function _asyncsDone ( ) {
105
+ -- _asyncsLeft || _done . apply ( self , hookArgs ) ;
112
106
}
113
107
}
114
- ( preChain [ 0 ] || allPresDone ) ( ) ;
108
+ return _next . apply ( this , arguments ) ;
115
109
} ;
110
+
111
+ proto [ name ] . numAsyncPres = 0 ;
116
112
117
- // proto[name] = function () {
118
- // var self = this
119
- // , hookArgs // arguments eventually passed to the hook - are mutable
120
- // , pres = this._pres[name]
121
- // , posts = this._posts[name]
122
- // , _total = pres.length
123
- // , _current = -1
124
- // , _next = function () {
125
- // var _args = Array.prototype.slice.call(arguments)
126
- // , currPre
127
- // , preArgs;
128
- // if (_args.length) hookArgs = _args;
129
- // if (++_current < _total) {
130
- // currPre = pres[_current]
131
- // if (currPre.length < 2) throw new Error("Your pre must have next and done arguments -- e.g., function (next, done, ...)");
132
- // preArgs = [_next, _done].concat( (currPre.length === 3) ? [hookArgs] : hookArgs);
133
- // return currPre.apply(self, preArgs);
134
- // } else return _done.apply(self, [null].concat(hookArgs));
135
- // }
136
- // , _done = function () {
137
- // var err = arguments[0]
138
- // , args_ = Array.prototype.slice.call(arguments, 1)
139
- // , ret, total_, current_, next_, done_, postArgs;
140
- // if (_current === _total) {
141
- // ret = fn.apply(self, args_);
142
- // total_ = posts.length;
143
- // current_ = -1;
144
- // next_ = function () {
145
- // var args_ = Array.prototype.slice.call(arguments)
146
- // , currPost
147
- // , postArgs;
148
- // if (args_.length) hookArgs = args_;
149
- // if (++current_ < total_) {
150
- // currPost = posts[current_]
151
- // if (currPost.length < 2) throw new Error("Your post must have next and done arguments -- e.g., function (next, done, ...)");
152
- // postArgs = [next_, done_].concat( (currPost.length ===3) ? [hookArgs] : hookArgs);
153
- // return posts[current_].apply(self, postArgs);
154
- // }
155
- // else return done_();
156
- // };
157
- // done_ = function () { return ret; };
158
- // if (total_) return next_();
159
- // return ret;
160
- // }
161
- // };
162
- // return _next.apply(this, arguments);
163
- // };
164
113
return this ;
165
114
} ,
115
+
166
116
pre : function ( name , fn , isAsync ) {
167
117
var proto = this . prototype
168
118
, pres = proto . _pres = proto . _pres || { } ;
169
- fn . isAsync = isAsync ;
119
+ if ( fn . isAsync = isAsync ) {
120
+ this . prototype [ name ] . numAsyncPres ++ ;
121
+ }
170
122
( pres [ name ] = pres [ name ] || [ ] ) . push ( fn ) ;
171
123
return this ;
172
124
} ,
0 commit comments