@@ -35,8 +35,6 @@ 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 >
40
38
</dl >
41
39
42
40
## Functions
@@ -102,46 +100,107 @@ Function's name start or end with "_" will not be able to apply middleware.
102
100
<a name =" new_MiddlewareManager_new " ></a >
103
101
104
102
### new MiddlewareManager(target, ...middlewareObjects)
105
- ** Returns** : <code >object</code > - this
106
103
107
104
| Param | Type | Description |
108
105
| --- | --- | --- |
109
106
| target | <code >object</code > | The target object. |
110
107
| ...middlewareObjects | <code >object</code > | Middleware objects. |
111
108
112
- <a name =" MiddlewareManager+use " ></a >
109
+ ** Example**
110
+ ``` js
111
+ ## Basic
113
112
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.
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
+ }
117
120
118
- ** Kind** : instance method of <code >[ MiddlewareManager] ( #MiddlewareManager ) </code >
119
- ** Returns** : <code >object</code > - this
121
+ speak (word ) {
122
+ this .word = word;
123
+ }
124
+ }
120
125
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. |
126
+ Then we define a middleware function to print log.
125
127
126
- <a name =" MiddlewareManager " ></a >
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
+ }
127
135
128
- ## MiddlewareManager
129
- ** Kind** : global class
136
+ Now we apply the log function as a middleware to a Person instance.
130
137
131
- * [ MiddlewareManager] ( #MiddlewareManager )
132
- * [ new MiddlewareManager(target, ...middlewareObjects)] ( #new_MiddlewareManager_new )
133
- * [ .use(methodName, ...middlewares)] ( #MiddlewareManager+use ) ⇒ <code >object</code >
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);
134
143
135
- < a name = " new_MiddlewareManager_new " ></ a >
144
+ Whenever a Person instance call it's walk method, we'll see logs from the looger middleware.
136
145
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. |
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.
144
149
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
+ ```
145
204
<a name =" MiddlewareManager+use " ></a >
146
205
147
206
### middlewareManager.use(methodName, ...middlewares) ⇒ <code >object</code >
0 commit comments