Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(throwIfEmpty): adds throwIfEmpty operator #3368

Merged
merged 2 commits into from
Mar 8, 2018

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Mar 2, 2018

This is a new, simple, operator that will emit an error if the source observable completes without emitting a value. This primitive operator can be used to compose other operators such as first and last, and is a good compliment for defaultIfEmpty.

This is a step toward refactoring a few other operators to be smaller.

@benlesh
Copy link
Member Author

benlesh commented Mar 2, 2018

Having this operator basically means that first would become:

first() => pipe(take(1), throwIfEmpty())
first(predicate) => pipe(filter(predicate), take(1), throwIfEmpty())
first(undefined, defaultValue) => pipe(take(1), defaultIfEmpty(defaultValue))
first(predicate, defaultValue) => pipe(filter(predicate), take(1), defaultIfEmpty(defaultValue))

last would be similar, only with takeLast

This is a new, simple, operator that will emit an error if the source observable completes without emitting a value. This primitive operator can be used to compose other operators such as `first` and `last`, and is a good compliment for `defaultIfEmpty`.
@rxjs-bot
Copy link

rxjs-bot commented Mar 2, 2018

Messages
📖

CJS: 1314.7KB, global: 698.0KB (gzipped: 114.3KB), min: 134.7KB (gzipped: 29.7KB)

Generated by 🚫 dangerJS

@coveralls
Copy link

coveralls commented Mar 2, 2018

Coverage Status

Coverage decreased (-0.04%) to 97.501% when pulling 5749019 on benlesh:add-throwIfEmpty into 72d9745 on ReactiveX:master.

Copy link
Collaborator

@jasonaden jasonaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor change.

*
* @example
*
* const click$ = fromEvent(button, 'clicks');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

click

*/
export const throwIfEmpty =
<T>(errorFactory: (() => any) = defaultErrorFactory) => tap<T>({
hasValue: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL this works

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants