File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change
1
+ import { expect } from 'chai' ;
1
2
import * as Rx from '../../dist/cjs/Rx' ;
2
3
import marbleTestingSignature = require( '../helpers/marble-testing' ) ; // tslint:disable-line:no-require-imports
3
4
@@ -151,4 +152,21 @@ describe('Observable.prototype.single', () => {
151
152
expectObservable ( e1 . single ( predicate ) ) . toBe ( expected , { z : undefined } ) ;
152
153
expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
153
154
} ) ;
155
+
156
+ it ( 'should call predicate with indices starting at 0' , ( ) => {
157
+ const e1 = hot ( '--a--b--c--|' ) ;
158
+ const e1subs = '^ !' ;
159
+ const expected = '-----------(b|)' ;
160
+
161
+ let indices = [ ] ;
162
+ const predicate = function ( value , index ) {
163
+ indices . push ( index ) ;
164
+ return value === 'b' ;
165
+ } ;
166
+
167
+ expectObservable ( e1 . single ( predicate ) . do ( null , null , ( ) => {
168
+ expect ( indices ) . to . deep . equal ( [ 0 , 1 , 2 ] ) ;
169
+ } ) ) . toBe ( expected ) ;
170
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
171
+ } ) ;
154
172
} ) ;
Original file line number Diff line number Diff line change @@ -61,19 +61,18 @@ class SingleSubscriber<T> extends Subscriber<T> {
61
61
}
62
62
63
63
protected _next ( value : T ) : void {
64
- const predicate = this . predicate ;
65
- this . index ++ ;
66
- if ( predicate ) {
67
- this . tryNext ( value ) ;
64
+ const index = this . index ++ ;
65
+
66
+ if ( this . predicate ) {
67
+ this . tryNext ( value , index ) ;
68
68
} else {
69
69
this . applySingleValue ( value ) ;
70
70
}
71
71
}
72
72
73
- private tryNext ( value : T ) : void {
73
+ private tryNext ( value : T , index : number ) : void {
74
74
try {
75
- const result = this . predicate ( value , this . index , this . source ) ;
76
- if ( result ) {
75
+ if ( this . predicate ( value , index , this . source ) ) {
77
76
this . applySingleValue ( value ) ;
78
77
}
79
78
} catch ( err ) {
You can’t perform that action at this time.
0 commit comments