-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds EmptyError error type - adds ability to group values in marble tests - adds last operator closes #304 closes #306
- Loading branch information
Showing
7 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* globals describe, it, expect, expectObservable, hot, cold */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
|
||
describe('Observable.prototype.last()', function(){ | ||
it('should take the last value of an observable', function(){ | ||
var e1 = hot('--a--^--b--c--|'); | ||
var expected = '---------(c|)'; | ||
expectObservable(e1.last()).toBe(expected) | ||
}); | ||
|
||
it('should error on empty', function() { | ||
var e1 = hot('--a--^----|'); | ||
var expected = '-----#'; | ||
expectObservable(e1.last()).toBe(expected, null, new Rx.EmptyError()); | ||
}); | ||
|
||
it('should go on forever on never', function() { | ||
var e2 = hot('--^---'); | ||
var expected = '----'; | ||
expectObservable(e2.last()).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Observable from '../Observable'; | ||
import Operator from '../Operator'; | ||
import Subscriber from '../Subscriber'; | ||
import Observer from '../Observer'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
import bindCallback from '../util/bindCallback'; | ||
import EmptyError from '../util/EmptyError'; | ||
|
||
export default function last<T>(predicate?: (value: T, index: number, source:Observable<T>) => boolean, thisArg?: any, defaultValue?: any) : Observable<T> { | ||
return this.lift(new LastOperator(predicate, thisArg, defaultValue, this)); | ||
} | ||
|
||
class LastOperator<T, R> implements Operator<T, R> { | ||
constructor(private predicate?: (value: T, index: number, source:Observable<T>) => boolean, private thisArg?: any, private defaultValue?: any, private source?: Observable<T>) { | ||
|
||
} | ||
|
||
call(observer: Subscriber<R>): Subscriber<T> { | ||
return new LastSubscriber(observer, this.predicate, this.thisArg, this.defaultValue, this.source); | ||
} | ||
} | ||
|
||
class LastSubscriber<T> extends Subscriber<T> { | ||
private lastValue: T; | ||
private hasValue: boolean = false; | ||
private predicate: Function; | ||
private index: number = 0; | ||
|
||
constructor(destination: Observer<T>, predicate?: (value: T, index: number, source: Observable<T>) => boolean, | ||
private thisArg?: any, private defaultValue?: any, private source?: Observable<T>) { | ||
super(destination); | ||
if(typeof defaultValue !== 'undefined') { | ||
this.lastValue = defaultValue; | ||
this.hasValue = true; | ||
} | ||
if(typeof predicate === 'function') { | ||
this.predicate = bindCallback(predicate, thisArg, 3); | ||
} | ||
} | ||
|
||
_next(value: T) { | ||
const predicate = this.predicate; | ||
if(predicate) { | ||
let result = tryCatch(predicate)(value, this.index++, this.source); | ||
if(result === errorObject) { | ||
this.destination.error(result.e); | ||
} else if (result) { | ||
this.lastValue = result; | ||
this.hasValue = true; | ||
} | ||
} else { | ||
this.lastValue = value; | ||
this.hasValue = true; | ||
} | ||
} | ||
|
||
_complete() { | ||
const destination = this.destination; | ||
if(this.hasValue) { | ||
destination.next(this.lastValue); | ||
destination.complete(); | ||
} else { | ||
destination.error(new EmptyError); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default class EmptyError implements Error { | ||
name = 'EmptyError' | ||
message = 'no elements in sequence'; | ||
} |