@@ -35,6 +35,8 @@ the target function will be left hanging.</p>
35
35
}
36
36
</code ></pre ><p >Function' ; s name start or end with " ; _" ; will not be able to apply middleware.</p >
37
37
</dd >
38
+ <dt ><a href =" #MiddlewareManager " >MiddlewareManager</a ></dt >
39
+ <dd ></dd >
38
40
</dl >
39
41
40
42
## Functions
@@ -100,107 +102,46 @@ Function's name start or end with "_" will not be able to apply middleware.
100
102
<a name =" new_MiddlewareManager_new " ></a >
101
103
102
104
### new MiddlewareManager(target, ...middlewareObjects)
105
+ ** Returns** : <code >object</code > - this
103
106
104
107
| Param | Type | Description |
105
108
| --- | --- | --- |
106
109
| target | <code >object</code > | The target object. |
107
110
| ...middlewareObjects | <code >object</code > | Middleware objects. |
108
111
109
- ** Example**
110
- ``` js
111
- ## Basic
112
+ <a name =" MiddlewareManager+use " ></a >
112
113
113
- We define a Person class .
114
- // the target object
115
- class Person {
116
- // the target function
117
- walk (step ) {
118
- this .step = step;
119
- }
114
+ ### middlewareManager.use(methodName, ...middlewares) ⇒ <code >object</code >
115
+ Apply (register) middleware functions to the target function or apply (register) middleware objects.
116
+ If the first argument is a middleware object, the rest arguments must be middleware objects.
120
117
121
- speak (word ) {
122
- this .word = word;
123
- }
124
- }
118
+ ** Kind** : instance method of <code >[ MiddlewareManager] ( #MiddlewareManager ) </code >
119
+ ** Returns** : <code >object</code > - this
125
120
126
- Then we define a middleware function to print log.
121
+ | Param | Type | Description |
122
+ | --- | --- | --- |
123
+ | methodName | <code >string</code > | ; <code >object</code > | String for target function name, object for a middleware object. |
124
+ | ...middlewares | <code >function</code > | ; <code >object</code > | The middleware chain to be applied. |
127
125
128
- // middleware for walk function
129
- const logger = target => next => (... args ) => {
130
- console .log (` walk start, steps: ${ args[0 ]} .` );
131
- const result = next (... args);
132
- console .log (` walk end.` );
133
- return result;
134
- }
126
+ <a name =" MiddlewareManager " ></a >
135
127
136
- Now we apply the log function as a middleware to a Person instance.
128
+ ## MiddlewareManager
129
+ ** Kind** : global class
137
130
138
- // apply middleware to target object
139
- const p = new Person ();
140
- const middlewareManager = new MiddlewareManager(p);
141
- middlewareManager.use('walk', walk);
142
- p.walk(3);
131
+ * [ MiddlewareManager] ( #MiddlewareManager )
132
+ * [ new MiddlewareManager(target, ...middlewareObjects)] ( #new_MiddlewareManager_new )
133
+ * [ .use(methodName, ...middlewares)] ( #MiddlewareManager+use ) ⇒ <code >object</code >
143
134
144
- Whenever a Person instance call it's walk method, we'll see logs from the looger middleware.
135
+ < a name = " new_MiddlewareManager_new " ></ a >
145
136
146
- ## Middleware object
147
- We can also apply a middleware object to a target object.
148
- Middleware object is an object that contains function's name as same as the target object's function name.
137
+ ### new MiddlewareManager(target, ...middlewareObjects)
138
+ ** Returns** : <code >object</code > - this
139
+
140
+ | Param | Type | Description |
141
+ | --- | --- | --- |
142
+ | target | <code >object</code > | The target object. |
143
+ | ...middlewareObjects | <code >object</code > | Middleware objects. |
149
144
150
- const PersonMiddleware {
151
- walk : target => next => step => {
152
- console .log (` walk start, steps: step.` );
153
- const result = next (step);
154
- console .log (` walk end.` );
155
- return result;
156
- },
157
- speak : target => next => word => {
158
- word = ' this is a middleware trying to say: ' + word;
159
- return next (word);
160
- }
161
- }
162
-
163
- // apply middleware to target object
164
- const p = new Person ();
165
- const middlewareManager = new MiddlewareManager (p);
166
- middlewareManager .use (PersonMiddleware);
167
- p .walk (3 );
168
- p .speak (' hi' );
169
-
170
- ## middlewareMethods
171
- Or we can use ` middlewareMethods` to define function names for middleware target within a class.
172
-
173
- class PersonMiddleware {
174
- constructor () {
175
- // Define function names for middleware target.
176
- this .middlewareMethods = [' walk' , ' speak' ];
177
- }
178
- log (text ) {
179
- console .log (' Middleware log: ' + text);
180
- }
181
- walk (target ) {
182
- return next => step => {
183
- this .log (` walk start, steps: step.` );
184
- const result = next (step);
185
- this .log (` walk end.` );
186
- return result;
187
- }
188
- }
189
- speak (target ) {
190
- return next => word => {
191
- this .log (' this is a middleware trying to say: ' + word);
192
- return next (word);
193
- }
194
- }
195
- }
196
-
197
- // apply middleware to target object
198
- const p = new Person ();
199
- const middlewareManager = new MiddlewareManager (p);
200
- middlewareManager .use (new PersonMiddleware ())
201
- p .walk (3 );
202
- p .speak (' hi' );
203
- ```
204
145
<a name =" MiddlewareManager+use " ></a >
205
146
206
147
### middlewareManager.use(methodName, ...middlewares) ⇒ <code >object</code >
0 commit comments