Skip to content

Commit

Permalink
feat(isEmpty): add higher-order lettable version of isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 21, 2017
1 parent 9267b30 commit aad1833
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
38 changes: 3 additions & 35 deletions src/operator/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';

import { Observable } from '../Observable';
import { isEmpty as higherOrder } from '../operators';

/**
* If the source Observable is empty it returns an Observable that emits true, otherwise it emits false.
Expand All @@ -12,37 +12,5 @@ import { Observable } from '../Observable';
* @owner Observable
*/
export function isEmpty<T>(this: Observable<T>): Observable<boolean> {
return this.lift(new IsEmptyOperator());
}

class IsEmptyOperator implements Operator<any, boolean> {
call (observer: Subscriber<boolean>, source: any): any {
return source.subscribe(new IsEmptySubscriber(observer));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class IsEmptySubscriber extends Subscriber<any> {
constructor(destination: Subscriber<boolean>) {
super(destination);
}

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

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

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

protected _complete() {
this.notifyComplete(true);
}
return higherOrder()(this);
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { findIndex } from './findIndex';
export { first } from './first';
export { groupBy } from './groupBy';
export { ignoreElements } from './ignoreElements';
export { isEmpty } from './isEmpty';
export { map } from './map';
export { materialize } from './materialize';
export { max } from './max';
Expand Down
40 changes: 40 additions & 0 deletions src/operators/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { OperatorFunction } from '../interfaces';

export function isEmpty<T>(): OperatorFunction<T, boolean> {
return (source: Observable<T>) => source.lift(new IsEmptyOperator());
}

class IsEmptyOperator implements Operator<any, boolean> {
call (observer: Subscriber<boolean>, source: any): any {
return source.subscribe(new IsEmptySubscriber(observer));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class IsEmptySubscriber extends Subscriber<any> {
constructor(destination: Subscriber<boolean>) {
super(destination);
}

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

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

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

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

0 comments on commit aad1833

Please sign in to comment.