Skip to content

adding {Flowable, Observable}::justOnce operator - #499

Merged
lehecka merged 3 commits into
rsocket:masterfrom
lehecka:justOnce
Jun 7, 2017
Merged

adding {Flowable, Observable}::justOnce operator#499
lehecka merged 3 commits into
rsocket:masterfrom
lehecka:justOnce

Conversation

@lehecka

Copy link
Copy Markdown
Contributor

this is a version of the just operator which works with movable types. The aim is to enable using just operator with the restriction that you can subscribe to the flowable/observable only once.

template <typename T>
static Reference<Observable<T>> justOnce(T value) {
auto lambda = [value = std::move(value), used = false](Reference<Observer<T>> observer) mutable {
CHECK(!used) << "justOnce value was already used";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should resolve with an exception in the other case, crashing doesn't seem correct.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I was going back and forth about this. On one hand I agree with you that crashing is not good. On the other hand this is violation of the API and the check is just checking that you are using it correctly. Debugging in the case of CHECK is much easier to debug.
I can also add DCHECK and exception instead of CHECK.
Anybody else want's to weigh in?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't crash here. If someone tries to resubscribe to a JustOnce operator, then they should probably get sent an onError.

Comment threadyarpl/test/FlowableTest.cpp Outdated
auto value = std::make_unique<int>(123456);

auto flowable = Flowables::justOnce(std::move(value));
EXPECT_EQ(std::size_t{1}, Refcounted::objects());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't this getting removed?

@phoadphoad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

TEST(Observable, SingleMovable) {
auto value = std::make_unique<int>(123456);

auto observable = Observables::justOnce(std::move(value));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: one more case to check if it fires the exception correctly

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Will cover in the next diff. Thank you for your feedback!

@lehecka
lehecka merged commit dbc5c87 into rsocket:masterJun 7, 2017
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@lehecka@lexs@alexmalyshev@phoad