-
Notifications
You must be signed in to change notification settings - Fork 3k
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(operator): add single operator #322
Conversation
e11e344
to
c671bd1
Compare
Actually, now that I'm digging into this operator I rarely use and researching the C# semantic it was born from, it looks like current RxJS and this impl are doing something weird with it. Enumerable.Single() asserts that there is a single element in the enumerable. likewise Observable.Single() does the same. The current RxJS version is like an alternate version of We could actually benefit from a @mattpodwysocki, what are your thoughts? |
Personally I agree with @Blesh. As already mentioned currently |
@kwonoj @mattpodwysocki ... oh... I think I've conflated some things here. The behavior of So: Observable.of(1).single(); // 1
Observable.of(1,2).single(); // error more than one element
Observable.of(1,2).single(x => x > 1); // 2
Observable.of(1,2,3).single(x => x > 1); // error more than one element
Observable.of(1,2,3,1).single(x => x === 1); // error more than one element I'll have to look over the tests again to see if this is actually the case. |
In other words, I think we all agree and I just said something confusing above. :p |
@mattpodwysocki Thanks for pointing out and clarification. |
c671bd1
to
de12ec0
Compare
Updated behavior as well as test specs, should be same as existing RxJS does.
|
de12ec0
to
e4bfb96
Compare
it('Should return undefined from predicate if observable does not emit', function() { | ||
var e1 = hot('--a--^--|'); | ||
var expected = '---(x|)'; | ||
|
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.
Previous comment seems hidden by updated commit, leaving note again for reference.
This test case illustrates case like example in document
Rx.Observable.empty().single()
where source does not emit anything also predicate is empty to specified match any element. In this case current RxJS behaves same as cases in line 91 which source emits but there isn't any matching element by predicate, returns undefined
.
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.
That seems broken. @mattpodwysocki why does this not error that the sequence was empty?
@kwonoj checking in... what's the status of this PR? are you ready for me to pull this down and test it? Do you need assistance? Thanks again for your hard work. |
This PR have one remaining question to be clarified why Other than those, I believe rest of changes are ready. |
After having discussed this with @benjchristensen, @jhusain and @trxcllnt, I think the concensus is that is should error. This is because If the desired behavior for the user is to emit someObservable.single().catch(err => err === Rx.EmptyError ? Observable.of(undefined) : Observable.throw(err)) TL;DR: have it throw an EmptyError (which already exists in the lib) |
Make sense to me. I'll update PR soon with changes. Appreciate for clarification! |
We can then merge and close this PR. If @mattpodwysocki or @headinthebox provides the reasoning behind the nexting of undefined from the original libraries, we can always change it back. But everyone I discussed this with didn't know why it does what it does. |
e4bfb96
to
308927a
Compare
Updated behavior as well as test case to raise error if source observable does not emit. |
Continue effort to add operator exists in current RxJS.