-
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.
feat(Observable): add pairwise operator
bring in pairwise operator from RxJS4
- Loading branch information
Showing
6 changed files
with
155 additions
and
1 deletion.
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
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,18 @@ | ||
var RxOld = require('rx'); | ||
var RxNew = require('../../../../index'); | ||
|
||
module.exports = function (suite) { | ||
var oldPairwiseWithImmediateScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.immediate).pairwise(); | ||
var newPairwiseWithImmediateScheduler = RxNew.Observable.range(0, 25).pairwise(); | ||
|
||
function _next(x) { } | ||
function _error(e) { } | ||
function _complete() { } | ||
return suite | ||
.add('old pairwise with immediate scheduler', function () { | ||
oldPairwiseWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
}) | ||
.add('new pairwise with immediate scheduler', function () { | ||
newPairwiseWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
}); | ||
}; |
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,88 @@ | ||
/* globals describe, it, expect, expectObservable, expectSubscriptions, cold, hot */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.pairwise()', function () { | ||
it('should pairwise things', function () { | ||
var e1 = hot('--a--^--b--c--d--e--f--g--|'); | ||
var e1subs = '^ !'; | ||
var expected = '------v--w--x--y--z--|'; | ||
|
||
var values = { | ||
v: ['b', 'c'], | ||
w: ['c', 'd'], | ||
x: ['d', 'e'], | ||
y: ['e', 'f'], | ||
z: ['f', 'g'] | ||
}; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected, values); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
|
||
it('should not emit on single-element streams', function () { | ||
var e1 = hot('-----^--b----|'); | ||
var e1subs = '^ !'; | ||
var expected = '--------|'; | ||
|
||
var values = { | ||
}; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected, values); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
|
||
it('should handle mid-stream throw', function () { | ||
var e1 = hot('--a--^--b--c--d--e--#'); | ||
var e1subs = '^ !'; | ||
var expected = '------v--w--x--#'; | ||
|
||
var values = { | ||
v: ['b', 'c'], | ||
w: ['c', 'd'], | ||
x: ['d', 'e'] | ||
}; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected, values); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
|
||
it('should handle empty', function () { | ||
var e1 = cold('|'); | ||
var e1subs = '(^!)'; | ||
var expected = '|'; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
|
||
it('should handle never', function () { | ||
var e1 = cold('-'); | ||
var e1subs = '^'; | ||
var expected = '-'; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
|
||
it('should handle throw', function () { | ||
var e1 = cold('#'); | ||
var e1subs = '(^!)'; | ||
var expected = '#'; | ||
|
||
var source = e1.pairwise(); | ||
|
||
expectObservable(source).toBe(expected); | ||
expectSubscriptions(e1.subscriptions).toBe(e1subs); | ||
}); | ||
}); |
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,7 @@ | ||
import {Observable} from '../../Observable'; | ||
import {pairwise} from '../../operator/pairwise'; | ||
import {KitchenSinkOperators} from '../../Rx.KitchenSink'; | ||
const observableProto = (<KitchenSinkOperators<any>>Observable.prototype); | ||
observableProto.pairwise = pairwise; | ||
|
||
export var _void: void; |
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,38 @@ | ||
import {Operator} from '../Operator'; | ||
import {Observable} from '../Observable'; | ||
import {Subscriber} from '../Subscriber'; | ||
|
||
/** | ||
* Returns a new observable that triggers on the second and following inputs. | ||
* An input that triggers an event will return an pair of [(N - 1)th, Nth]. | ||
* The (N-1)th is stored in the internal state until Nth input occurs. | ||
* @returns {Observable<R>} an observable of pairs of values. | ||
*/ | ||
export function pairwise<T>(): Observable<T> { | ||
return this.lift(new PairwiseOperator()); | ||
} | ||
|
||
class PairwiseOperator<T, R> implements Operator<T, R> { | ||
call(subscriber: Subscriber<T>): Subscriber<T> { | ||
return new PairwiseSubscriber(subscriber); | ||
} | ||
} | ||
|
||
class PairwiseSubscriber<T> extends Subscriber<T> { | ||
private prev: T; | ||
private hasPrev: boolean = false; | ||
|
||
constructor(destination: Subscriber<T>) { | ||
super(destination); | ||
} | ||
|
||
_next(value: T): void { | ||
if (this.hasPrev) { | ||
this.destination.next([this.prev, value]); | ||
} else { | ||
this.hasPrev = true; | ||
} | ||
|
||
this.prev = value; | ||
} | ||
} |