File tree 2 files changed +37
-4
lines changed 2 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -78,10 +78,14 @@ const botCallback = function botCallback() {
78
78
* is function, then callback for bot message is added
79
79
*/
80
80
if ( args . length && typeof args [ 0 ] === 'function' ) {
81
- this . middlewares . push ( args [ 0 ] ) ;
81
+ const copy = {
82
+ middlewares : this . middlewares . slice ( )
83
+ } ;
82
84
83
- const callback = botCallback . bind ( this ) ;
84
- callback . use = use . bind ( this ) ;
85
+ copy . middlewares . push ( args [ 0 ] ) ;
86
+
87
+ const callback = botCallback . bind ( copy ) ;
88
+ callback . use = use . bind ( copy ) ;
85
89
86
90
return callback ;
87
91
}
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ describe('basic middleware usage', () => {
84
84
expect ( v . join ( '' ) ) . to . equal ( 'ABCD' ) ;
85
85
} ) ;
86
86
87
- it ( 'should properly throw errors and also pass it to middleware error handler' , function * ( ) {
87
+ it ( 'should properly throw errors and also pass it to middleware error handler' , function * ( ) {
88
88
let wasThereError = false ;
89
89
let middlewareErrorHandlerWorked = false ;
90
90
@@ -111,4 +111,33 @@ describe('basic middleware usage', () => {
111
111
expect ( middlewareErrorHandlerWorked ) . to . equal ( true ) ;
112
112
}
113
113
} ) ;
114
+
115
+ it ( 'should properly pass copied context and not affect previous middlewares' , function * ( ) {
116
+ const values = [ ] ;
117
+
118
+ const def = use ( ( ) => {
119
+ values . push ( 'Z' ) ;
120
+ } ) ;
121
+
122
+ const a = def . use ( ( ) => {
123
+ values . push ( 'A' ) ;
124
+ } ) ;
125
+
126
+ const b = def . use ( ( ) => {
127
+ values . push ( 'B' ) ;
128
+ } ) ;
129
+
130
+ const c = b . use ( ( ) => {
131
+ values . push ( 'C' ) ;
132
+ } ) ;
133
+
134
+ yield a ( botArgumentsMock ) ;
135
+ yield b ( botArgumentsMock ) ;
136
+
137
+ expect ( values . join ( '' ) ) . to . equal ( 'ZAZB' ) ;
138
+
139
+ yield c ( botArgumentsMock ) ;
140
+
141
+ expect ( values . join ( '' ) ) . to . equal ( 'ZAZBZBC' ) ;
142
+ } ) ;
114
143
} ) ;
You can’t perform that action at this time.
0 commit comments