Skip to content

Commit

Permalink
feat(operator): add isEmpty operator
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored and benlesh committed Sep 23, 2015
1 parent 4515fff commit 80f72c5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
20 changes: 20 additions & 0 deletions perf/micro/immediate-scheduler/operators/isempty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var RxOld = require("rx");
var RxNew = require("../../../../index");

module.exports = function (suite) {

var oldIsEmptyNoArgs = RxOld.Observable.of(25, RxOld.Scheduler.immediate).isEmpty();
var newIsEmptyNoArgs = RxNew.Observable.of(25).isEmpty();

return suite
.add('old isEmpty with immediate scheduler', function () {
oldIsEmptyNoArgs.subscribe(_next, _error, _complete);
})
.add('new isEmpty with immediate scheduler', function () {
newIsEmptyNoArgs.subscribe(_next, _error, _complete);
});

function _next(x) { }
function _error(e){ }
function _complete(){ }
};
31 changes: 31 additions & 0 deletions spec/operators/isEmpty-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* globals describe, it, expect, expectObservable, hot */
var Rx = require('../../dist/cjs/Rx');

describe('Observable.prototype.isEmpty()', function(){
it('should return true if source is empty', function () {
var source = hot('-----|');
var expected = '-----(x|)';

expectObservable(source.isEmpty()).toBe(expected, { x: true });
});

it('should return false if source emits element', function () {
var source = hot('--a--^--b--|');
var expected = '---(x|)';

expectObservable(source.isEmpty()).toBe(expected, { x: false });
});

it('should raise error if source raise error', function () {
var source = hot('--#');
var expected = '--#';

expectObservable(source.isEmpty()).toBe(expected);
});

it('should not completes if source never emits', function () {
var expected = '-';

expectObservable(Rx.Observable.never().isEmpty()).toBe(expected);
});
});
1 change: 1 addition & 0 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export default class Observable<T> {
startWith: <T>(x: T) => Observable<T>;
debounce: <R>(dueTime: number, scheduler?: Scheduler) => Observable<R>;

isEmpty: () => Observable<boolean>;
elementAt: (index: number, defaultValue?: any) => Observable<T>;
last: (predicate?: (value: T, index:number) => boolean, thisArg?: any, defaultValue?: any) => Observable<T>;
single: (predicate?: (value: T, index:number) => boolean, thisArg?: any) => Observable<T>;
Expand Down
3 changes: 3 additions & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ import sampleTime from './operators/sampleTime';
observableProto.sample = sample;
observableProto.sampleTime = sampleTime;

import isEmpty from './operators/isEmpty';
import last from './operators/last';
import single from './operators/single';

observableProto.isEmpty = isEmpty;
observableProto.last = last;
observableProto.single = single

Expand Down
35 changes: 35 additions & 0 deletions src/operators/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Operator from '../Operator';
import Observable from '../Observable';
import Subscriber from '../Subscriber';

export default function isEmpty() {
return this.lift(new IsEmptyOperator());
}

class IsEmptyOperator<T, R> implements Operator<T, R> {
call (observer: Subscriber<boolean>): Subscriber<T> {
return new IsEmptySubscriber<T>(observer);
}
}

class IsEmptySubscriber<T> extends Subscriber<T> {

constructor(destination: Subscriber<boolean>) {
super(destination);
}

private notifyComplete(isEmpty: boolean): void {
const destination = this.destination;

destination.next(isEmpty);
destination.complete();
}

_next(value: T) {
this.notifyComplete(false);
}

_complete() {
this.notifyComplete(true);
}
}

0 comments on commit 80f72c5

Please sign in to comment.