-
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(isEmpty): add higher-order lettable version of isEmpty
- Loading branch information
Showing
3 changed files
with
44 additions
and
35 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
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,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); | ||
} | ||
} |