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

Introduce AnonymousSubject #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bonzini
Copy link
Contributor

@bonzini bonzini commented Jan 15, 2020

RxLua only has a zero-version create() function for Subject, corresponding to the constructor of RxJS for example. However, RxJS and other ReactiveX implementations also have a two-argument create() function, returning a subject that simply juxtaposes an observable and an observer into a single object. Quite confusingly, the object returned by create() is not a Subject, but an instance of a different class AnonymousSubject. This pull request introduces AnonymousSubject as a completely separate implementation to avoid overloading Subject.create.

Observable.create's one-argument version was not properly documented, and
the return type is not described either.  But to document that it returns a
Subscription we should first make it do so.
RxLua only has a zero-version create() function for Subject, corresponding to
the constructor of RxJS for example.  However, RxJS has a two-argument
create() function, returning a subject that simply juxtaposes an observable
and an observer into a single object.  Quite confusingly, the object returned
by create() is not a Subject, but an instance of a different class
AnonymousSubject.  Introduce AnonymousSubject as a completely separate
implementation to avoid overloading Subject.create.
@bonzini
Copy link
Contributor Author

bonzini commented Jun 12, 2020

Hi, could you please take a look at this?

@4O4
Copy link
Contributor

4O4 commented Oct 17, 2020

Personally I'm wondering what's the use case for that? I know the AnonymousSubject class but I've never seen it actually used anywhere in the real code outside of the RxJS internals. Can you provide some examples when is it useful to have something like that?

@bonzini
Copy link
Contributor Author

bonzini commented Oct 19, 2020

You can use AnonymousSubject to define an operator that returns a hot observable. Usually the Observable will be a Subject too, for example a BehaviorSubject.

On the Observer side, the onNext method will "do something" with the value it receives and push to the destination Subject. For example the observer could accept a boolean value for "is the network connection available", try connecting to a server when the network connection becomes available, and push true to a BehaviorSubject if the attempt to connect succeeds. This AnonymousSubject would tell you upon subscription whether you are connected to the server.

@4O4
Copy link
Contributor

4O4 commented Oct 22, 2020

Thanks for the explanation.

I'm maintaining a fork of this library which I called Lua-ReactiveX and I've also included AnonymousSubject there in order to implement proper automatic unsubscription, but it works a little bit differently (because one of the create() parameters is required to be a factory function).

I'm considering however to rename it to something else like ProxySubject and leave it private behind the scenes, and then an actual public implementation of AnonoymousSubject like the one you're offering here could be added there. Would you be interested in preparing a pull request for that in my fork? The fork is going to be actively maintained by me, because I'm actually using it myself and need it to be stable and possibly bug-less :)

@bonzini
Copy link
Contributor Author

bonzini commented Oct 22, 2020

Not sure when but yeah, I can contribute with a pull request.

Sign up for free to 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.

2 participants