1
1
import { Observable } from '../Observable' ;
2
2
import { Operator } from '../Operator' ;
3
3
import { Subscriber } from '../Subscriber' ;
4
- import { EmptyError } from '../util/EmptyError' ;
5
- import { OperatorFunction , MonoTypeOperatorFunction } from '../types' ;
6
-
7
- /* tslint:disable:max-line-length */
8
- export function last < T , S extends T > ( predicate : ( value : T , index : number , source : Observable < T > ) => value is S ) : OperatorFunction < T , S > ;
9
- export function last < T , S extends T , R > ( predicate : ( value : T | S , index : number , source : Observable < T > ) => value is S ,
10
- resultSelector : ( value : S , index : number ) => R , defaultValue ?: R ) : OperatorFunction < T , R > ;
11
- export function last < T , S extends T > ( predicate : ( value : T , index : number , source : Observable < T > ) => value is S ,
12
- resultSelector : void ,
13
- defaultValue ?: S ) : OperatorFunction < T , S > ;
14
- export function last < T > ( predicate ?: ( value : T , index : number , source : Observable < T > ) => boolean ) : MonoTypeOperatorFunction < T > ;
15
- export function last < T , R > ( predicate : ( value : T , index : number , source : Observable < T > ) => boolean ,
16
- resultSelector ?: ( value : T , index : number ) => R ,
17
- defaultValue ?: R ) : OperatorFunction < T , R > ;
18
- export function last < T > ( predicate : ( value : T , index : number , source : Observable < T > ) => boolean ,
19
- resultSelector : void ,
20
- defaultValue ?: T ) : MonoTypeOperatorFunction < T > ;
21
- /* tslint:enable:max-line-length */
4
+ import { EmptyError } from '..//util/EmptyError' ;
5
+ import { MonoTypeOperatorFunction } from '../../internal/types' ;
22
6
23
7
/**
24
8
* Returns an Observable that emits only the last item emitted by the source Observable.
@@ -30,28 +14,26 @@ export function last<T>(predicate: (value: T, index: number, source: Observable<
30
14
*
31
15
* @throws {EmptyError } Delivers an EmptyError to the Observer's `error`
32
16
* callback if the Observable completes before any `next` notification was sent.
33
- * @param {function } predicate - The condition any source emitted item has to satisfy.
17
+ * @param {function } [predicate] - The condition any source emitted item has to satisfy.
18
+ * @param {any } [defaultValue] - An optional default value to provide if last
19
+ * predicate isn't met or no values were emitted.
34
20
* @return {Observable } An Observable that emits only the last item satisfying the given condition
35
21
* from the source, or an NoSuchElementException if no such items are emitted.
36
22
* @throws - Throws if no items that match the predicate are emitted by the source Observable.
37
- * @method last
38
- * @owner Observable
39
23
*/
40
- export function last < T , R > ( predicate ?: ( value : T , index : number , source : Observable < T > ) => boolean ,
41
- resultSelector ?: ( ( value : T , index : number ) => R ) | void ,
42
- defaultValue ?: R ) : OperatorFunction < T , T | R > {
43
- return ( source : Observable < T > ) => source . lift ( new LastOperator ( predicate , resultSelector , defaultValue , source ) ) ;
24
+ export function last < T > ( predicate ?: ( value : T , index : number , source : Observable < T > ) => boolean ,
25
+ defaultValue ?: T ) : MonoTypeOperatorFunction < T > {
26
+ return ( source : Observable < T > ) => source . lift ( new LastOperator ( predicate , defaultValue , source ) ) ;
44
27
}
45
28
46
- class LastOperator < T , R > implements Operator < T , R > {
47
- constructor ( private predicate ?: ( value : T , index : number , source : Observable < T > ) => boolean ,
48
- private resultSelector ?: ( ( value : T , index : number ) => R ) | void ,
49
- private defaultValue ?: any ,
50
- private source ?: Observable < T > ) {
29
+ class LastOperator < T > implements Operator < T , T > {
30
+ constructor ( private predicate : ( value : T , index : number , source : Observable < T > ) => boolean ,
31
+ private defaultValue : any ,
32
+ private source : Observable < T > ) {
51
33
}
52
34
53
- call ( observer : Subscriber < R > , source : any ) : any {
54
- return source . subscribe ( new LastSubscriber ( observer , this . predicate , this . resultSelector , this . defaultValue , this . source ) ) ;
35
+ call ( observer : Subscriber < T > , source : any ) : any {
36
+ return source . subscribe ( new LastSubscriber ( observer , this . predicate , this . defaultValue , this . source ) ) ;
55
37
}
56
38
}
57
39
@@ -60,16 +42,15 @@ class LastOperator<T, R> implements Operator<T, R> {
60
42
* @ignore
61
43
* @extends {Ignored }
62
44
*/
63
- class LastSubscriber < T , R > extends Subscriber < T > {
64
- private lastValue : T | R ;
65
- private hasValue : boolean = false ;
66
- private index : number = 0 ;
45
+ class LastSubscriber < T > extends Subscriber < T > {
46
+ private lastValue : T ;
47
+ private hasValue = false ;
48
+ private index = 0 ;
67
49
68
- constructor ( destination : Subscriber < R > ,
69
- private predicate ?: ( value : T , index : number , source : Observable < T > ) => boolean ,
70
- private resultSelector ?: ( ( value : T , index : number ) => R ) | void ,
71
- private defaultValue ?: any ,
72
- private source ?: Observable < T > ) {
50
+ constructor ( destination : Subscriber < T > ,
51
+ private predicate : ( value : T , index : number , source : Observable < T > ) => boolean ,
52
+ private defaultValue : T ,
53
+ private source : Observable < T > ) {
73
54
super ( destination ) ;
74
55
if ( typeof defaultValue !== 'undefined' ) {
75
56
this . lastValue = defaultValue ;
@@ -82,10 +63,6 @@ class LastSubscriber<T, R> extends Subscriber<T> {
82
63
if ( this . predicate ) {
83
64
this . _tryPredicate ( value , index ) ;
84
65
} else {
85
- if ( this . resultSelector ) {
86
- this . _tryResultSelector ( value , index ) ;
87
- return ;
88
- }
89
66
this . lastValue = value ;
90
67
this . hasValue = true ;
91
68
}
@@ -100,27 +77,11 @@ class LastSubscriber<T, R> extends Subscriber<T> {
100
77
return ;
101
78
}
102
79
if ( result ) {
103
- if ( this . resultSelector ) {
104
- this . _tryResultSelector ( value , index ) ;
105
- return ;
106
- }
107
80
this . lastValue = value ;
108
81
this . hasValue = true ;
109
82
}
110
83
}
111
84
112
- private _tryResultSelector ( value : T , index : number ) {
113
- let result : any ;
114
- try {
115
- result = ( < any > this ) . resultSelector ( value , index ) ;
116
- } catch ( err ) {
117
- this . destination . error ( err ) ;
118
- return ;
119
- }
120
- this . lastValue = result ;
121
- this . hasValue = true ;
122
- }
123
-
124
85
protected _complete ( ) : void {
125
86
const destination = this . destination ;
126
87
if ( this . hasValue ) {
0 commit comments