-
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(operator): add isEmpty operator
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 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,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(){ } | ||
}; |
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,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); | ||
}); | ||
}); |
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,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); | ||
} | ||
} |