-
Notifications
You must be signed in to change notification settings - Fork 12
feat(maybe): add a function to convert maybe to an rxjs observable #128
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
Merged
+249
−98
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| export * from './monads/index' | ||
| export * from './interfaces/index' | ||
| export { | ||
| maybeToObservable | ||
| } from './util/index' |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| import { IMaybe } from '../../interfaces' | ||
| import { EMPTY, Observable, of } from 'rxjs' | ||
| import { take } from 'rxjs/operators' | ||
|
|
||
| /** | ||
| * Convert a Maybe into an observable | ||
| * | ||
| * If the Maybe is empty, the observable will immediately complete without emitting a value, otherwise it will emit | ||
| * the value contained and complete. | ||
| * | ||
| * @requires rxjs@^6.0 | ||
| * @example | ||
| * of(maybe(5)).pipe( | ||
| * flatMap(maybeToObservable) | ||
| * ).subscribe(a => console.log(a)) | ||
| * // prints 5 and completes | ||
| * | ||
| * of(maybe()).pipe( | ||
| * flatMap(maybeToObservable) | ||
| * ).subscribe(a => console.log(a)) | ||
| * // immediately completes with no emitted value | ||
| */ | ||
| export const maybeToObservable = <A>(m: IMaybe<A>): Observable<A> => { | ||
| return m.isNone() | ||
| ? EMPTY | ||
| : of(m.valueOrThrow('isNone returned false for empty IMaybe.')) | ||
| .pipe(take(1)) | ||
| } |
This file contains hidden or 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,38 @@ | ||
| import { assert, integer, property } from 'fast-check' | ||
| import { maybe } from '../../../src/monads' | ||
| import { maybeToObservable } from '../../../src/util' | ||
| import { merge, of } from 'rxjs' | ||
| import { count } from 'rxjs/operators' | ||
|
|
||
| describe('maybeToObservable', () => { | ||
| const numRuns = 100 | ||
| it('emits a value when containing a value', () => { | ||
| expect.assertions(numRuns) | ||
| assert( | ||
| property( | ||
| integer(), | ||
| a => { | ||
| const m = maybe(a) | ||
| const o = maybeToObservable(m) | ||
| o.subscribe(val => expect(val).toBe(a)) | ||
| } | ||
| ), { | ||
| numRuns | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| it('immediately completes if there is no contained value', done => { | ||
|
|
||
| const m = maybe() | ||
| const o = maybeToObservable(m) | ||
| const c = of(1) | ||
|
|
||
| merge(o, c) | ||
| .pipe(count()) | ||
| .subscribe(count => { | ||
| expect(count).toBe(1) | ||
| done() | ||
| }) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of wondering if this is a valid way to provide the global symbol for rxjs operators, or if it should be shortened to
operatorsor something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results in this in the UMD output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the answer to this one. I think the way you have it is good.