Skip to content

Commit

Permalink
feat(inspect): added inspect operator
Browse files Browse the repository at this point in the history
changed name of current `sample` operator to `inspect`. This is because it
deviated from the behavior expected in other implementations of Rx sample.

BREAKING CHANGE: RxJS 5 `sample` behavior is now `inspect`
  • Loading branch information
benlesh committed Dec 14, 2015
1 parent ec69fe7 commit f9944ae
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
40 changes: 20 additions & 20 deletions spec/operators/sample-spec.js → spec/operators/inspect-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.sample', function () {
it('should get samples when the notifier emits', function () {
describe('Observable.prototype.inspect', function () {
it('should get inspections when the notifier emits', function () {
var e1 = hot('----a-^--b----c----d----e----f----| ');
var e1subs = '^ ! ';
var e2 = hot( '-----x----------x----------x----------|');
var e2subs = '^ ! ';
var expected = '-----b----------d----------f| ';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should sample nothing if source has not nexted yet', function () {
it('should inspect nothing if source has not nexted yet', function () {
var e1 = hot('----a-^-------b----|');
var e1subs = '^ !';
var e2 = hot( '-----x-------|');
var e2subs = '^ !';
var expected = '-------------|';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -34,7 +34,7 @@ describe('Observable.prototype.sample', function () {
var e2subs = '^ ! ';
var expected = '------a---------------------------';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -46,7 +46,7 @@ describe('Observable.prototype.sample', function () {
var e2subs = '^ ! ';
var expected = '------a--------------------------|';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -59,7 +59,7 @@ describe('Observable.prototype.sample', function () {
var e2subs = '^ ! ';
var expected = '-----b--------- ';

expectObservable(e1.sample(e2), unsub).toBe(expected);
expectObservable(e1.inspect(e2), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -74,22 +74,22 @@ describe('Observable.prototype.sample', function () {

var result = e1
.mergeMap(function (x) { return Observable.of(x); })
.sample(e2)
.inspect(e2)
.mergeMap(function (x) { return Observable.of(x); });

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should sample multiple times according to the notifier', function () {
it('should inspect multiple times according to the notifier', function () {
var e1 = hot('----a----b----c----d----e----f----| ');
var e1subs = '^ ! ';
var e2 = hot('------x-x------xx-x---x-------------|');
var e2subs = '^ ! ';
var expected = '------a-a------cc-c---d-----------| ';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -101,7 +101,7 @@ describe('Observable.prototype.sample', function () {
var e2subs = '^ ! ';
var expected = '-----b----------d-# ';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -113,7 +113,7 @@ describe('Observable.prototype.sample', function () {
var e1subs = '(^!)';
var e2subs = '(^!)';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -125,7 +125,7 @@ describe('Observable.prototype.sample', function () {
var e1subs = '(^!)';
var e2subs = '(^!)';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -137,7 +137,7 @@ describe('Observable.prototype.sample', function () {
var e1subs = '^ !';
var e2subs = '^ !';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand All @@ -149,31 +149,31 @@ describe('Observable.prototype.sample', function () {
var e2subs = '^ !';
var expected = '-';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should sample only until source completes', function () {
it('should inspect only until source completes', function () {
var e1 = hot('----a----b----c----d-|');
var e1subs = '^ !';
var e2 = hot('-----------x----------x------------|');
var e2subs = '^ !';
var expected = '-----------b---------|';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should complete sampling if sample observable completes', function () {
it('should complete sampling if inspect observable completes', function () {
var e1 = hot('----a----b----c----d-|');
var e1subs = '^ !';
var e2 = hot('|');
var e2subs = '(^!)';
var expected = '---------------------|';

expectObservable(e1.sample(e2)).toBe(expected);
expectObservable(e1.inspect(e2)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
Expand Down
2 changes: 1 addition & 1 deletion src/CoreOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface CoreOperators<T> {
elementSelector?: (value: T) => R,
durationSelector?: (group: GroupedObservable<R>) => Observable<any>) => Observable<GroupedObservable<R>>;
ignoreElements?: () => Observable<T>;
inspect?: (notifier: Observable<any>) => Observable<T>;
last?: <R>(predicate?: (value: T, index: number) => boolean,
resultSelector?: (value: T, index: number) => R,
defaultValue?: any) => Observable<T> | Observable<R>;
Expand All @@ -62,7 +63,6 @@ export interface CoreOperators<T> {
repeat?: (count?: number) => Observable<T>;
retry?: (count?: number) => Observable<T>;
retryWhen?: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
sample?: (notifier: Observable<any>) => Observable<T>;
sampleTime?: (delay: number, scheduler?: Scheduler) => Observable<T>;
scan?: <R>(project: (acc: R, x: T) => R, acc?: R) => Observable<R>;
share?: () => Observable<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export class Observable<T> implements CoreOperators<T> {
elementSelector?: (value: T) => R,
durationSelector?: (group: GroupedObservable<R>) => Observable<any>) => Observable<GroupedObservable<R>>;
ignoreElements: () => Observable<T>;
inspect: (notifier: Observable<any>) => Observable<T>;
last: <R>(predicate?: (value: T, index: number) => boolean,
resultSelector?: (value: T, index: number) => R,
thisArg?: any, defaultValue?: any) => Observable<T> | Observable<R>;
Expand All @@ -244,7 +245,6 @@ export class Observable<T> implements CoreOperators<T> {
repeat: (count?: number) => Observable<T>;
retry: (count?: number) => Observable<T>;
retryWhen: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
sample: (notifier: Observable<any>) => Observable<T>;
sampleTime: (delay: number, scheduler?: Scheduler) => Observable<T>;
scan: <R>(accumulator: (acc: R, x: T) => R, seed?: T | R) => Observable<R>;
share: () => Observable<T>;
Expand Down
2 changes: 1 addition & 1 deletion src/Rx.KitchenSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import './add/operator/finally';
import './add/operator/first';
import './add/operator/groupBy';
import './add/operator/ignoreElements';
import './add/operator/inspect';
import './add/operator/extended/isEmpty';
import './add/operator/every';
import './add/operator/last';
Expand All @@ -98,7 +99,6 @@ import './add/operator/reduce';
import './add/operator/repeat';
import './add/operator/retry';
import './add/operator/retryWhen';
import './add/operator/sample';
import './add/operator/sampleTime';
import './add/operator/scan';
import './add/operator/share';
Expand Down
2 changes: 1 addition & 1 deletion src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import './add/operator/finally';
import './add/operator/first';
import './add/operator/groupBy';
import './add/operator/ignoreElements';
import './add/operator/inspect';
import './add/operator/every';
import './add/operator/last';
import './add/operator/map';
Expand All @@ -74,7 +75,6 @@ import './add/operator/reduce';
import './add/operator/repeat';
import './add/operator/retry';
import './add/operator/retryWhen';
import './add/operator/sample';
import './add/operator/sampleTime';
import './add/operator/scan';
import './add/operator/share';
Expand Down
5 changes: 5 additions & 0 deletions src/add/operator/inspect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Observable} from '../../Observable';
import {inspect} from '../../operator/inspect';
Observable.prototype.inspect = inspect;

export var _void: void;
5 changes: 0 additions & 5 deletions src/add/operator/sample.ts

This file was deleted.

12 changes: 6 additions & 6 deletions src/operator/sample.ts → src/operator/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';

export function sample<T>(notifier: Observable<any>): Observable<T> {
return this.lift(new SampleOperator(notifier));
export function inspect<T>(notifier: Observable<any>): Observable<T> {
return this.lift(new InspectOperator(notifier));
}

class SampleOperator<T, R> implements Operator<T, R> {
class InspectOperator<T, R> implements Operator<T, R> {
constructor(private notifier: Observable<any>) {
}

call(subscriber: Subscriber<R>) {
return new SampleSubscriber(subscriber, this.notifier);
return new InspectSubscriber(subscriber, this.notifier);
}
}

class SampleSubscriber<T> extends Subscriber<T> {
class InspectSubscriber<T> extends Subscriber<T> {
private lastValue: T;
private hasValue: boolean = false;

Expand All @@ -37,7 +37,7 @@ class SampleSubscriber<T> extends Subscriber<T> {
}

class SampleNotificationSubscriber<T> extends Subscriber<T> {
constructor(private parent: SampleSubscriber<T>) {
constructor(private parent: InspectSubscriber<T>) {
super(null);
}

Expand Down

0 comments on commit f9944ae

Please sign in to comment.