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

Subject.toSerialized() #1748

Closed
benjchristensen opened this issue Oct 10, 2014 · 9 comments
Closed

Subject.toSerialized() #1748

benjchristensen opened this issue Oct 10, 2014 · 9 comments
Milestone

Comments

@benjchristensen
Copy link
Member

Should we add an instance method such as:

class Subject<T, R> {
   public SerializedSubject<T, R> toSerialized()
}

Currently the way to get a SerializedSubject is via the constructor:

new SerializedSubject<T, R>(subject);

Related to addition of SerializedSubject in #1744

@headinthebox
Copy link
Contributor

It is sad that Java has no extensions methods.
Howevere it is a bit weird since (Imassume) serialized subject is also a subject, so we can call xs.toSerialized().....toSerialized()

I vote to keep things as is for now.

@benjchristensen
Copy link
Member Author

Moving to 1.0 as this is easy to add.

@samuelgruetter
Copy link
Contributor

I agree with @headinthebox that xs.toSerialized().....toSerialized() looks a bit weird at first sight, but if the semantics of toSerialized are "make sure that it's a serialized subject" instead of "wrap the subject in a serialized subject", it makes sense. That is, the default implementation in Subject would be to return a new SerializedSubject, and SerializedSubject would override it and just return this. This would also be extensible, because users could add a MyOwnSerializedSubject, which does not extend SerializedSubject, but only Subject, and also returns this in the toSerialized method.

@benjchristensen
Copy link
Member Author

we can call xs.toSerialized().....toSerialized()

I can do that on an Observable as well.... observable.serialize().serialize().serialize()

if the semantics of toSerialized are "make sure that it's a serialized subject"

Yes it can check that is in an instance of the already wrapped Subject and not wrap again.

@benjchristensen
Copy link
Member Author

The signature could be:

public SerializedSubject<T, R> toSerialized();

This way it is clear what is being returned.

@zsxwing
Copy link
Member

zsxwing commented Oct 28, 2014

public SerializedSubject<T, R> toSerialized();

If using this one, the case mentioned by @samuelgruetter cannot work.

This would also be extensible, because users could add a MyOwnSerializedSubject, which does not extend SerializedSubject, but only Subject, and also returns this in the toSerialized method.

@samuelgruetter
Copy link
Contributor

We could make an abstract SerializedSubject class which is just for labelling, and rx would provide a default implementation.

@headinthebox
Copy link
Contributor

Sounds good.

@benjchristensen benjchristensen modified the milestones: 1.0.x, 1.0 Nov 8, 2014
benjchristensen added a commit to benjchristensen/RxJava that referenced this issue Nov 8, 2014
Adds toSerialized as discussed in ReactiveX#1748
benjchristensen added a commit to benjchristensen/RxJava that referenced this issue Nov 8, 2014
Adds toSerialized as discussed in ReactiveX#1748
@benjchristensen
Copy link
Member Author

I added toSerialized. I did not make SerializedSubject an interface because it follows the same pattern as SerializedObserver and SerializedSubscriber. The "serialized" part IS the implementation of the Subject.

Even if we were to make SerializedSubject into an interface we'd also need to make SerializedObserver and SerializedSubscriber into interfaces.

Also, there are really only 3 ways to approach this that I'm aware of:

  • Using blocking synchronization. We are trying to avoid that. We used to have SynchronizedObserver and replaced it with SerializedObserver.
  • Using queues and thread-stealing to push through. This is the model SerializedObserver and SerializedSubject use.
  • Using an MPSC queue with a separate thread to drain (standard producer/consumer). We don't want to add extra concurrency so are avoiding that. If an extra thread is wanted then observeOn is the answer.

Thus, there is really only one approach we want for this, and that is the SerializedObserver approach, hence SerializedSubject, which is an implementation of Subject that adds the Serialized capability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants