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

[3.x] Synchronized Subject by Composition instead of Delegation #7476

Closed
brainbytes42 opened this issue Oct 13, 2022 · 1 comment
Closed

Comments

@brainbytes42
Copy link

Version: 3.1.5
Related (old) issue: #1748 (discussing Subject.toSerialized() for v 1.x)

In my use-case, I have several Subjects; some model state (BehaviourSubject) and some events (PublishSubject). Both, new states and new events, may result from different threads. According to the docs, it is advised to call Subject.toSerialized() then, which returns a SerializedSubject (as Subject), wrapping the original subject.

This isn't ideal for me out of three reasons:

  1. I loose the original subject's signature, for example BehaviousSubject.getValue().
  2. Having only Subjects, the distinction between Replaying one, all or none of the previous events, will be clear only from the docs or some other context, but not from the subject's type itself.
  3. The need to call toSerialized() isn't obvious; it's in the docs, but it would be better (and more convenient) to have it in the Subject's signature, for example BehaviourSubject.createSerialized() → BehaviourSubject. This would make the distinction clear as soon as the IDE of choice provides the auto-completion for available methods.

For the implementation I'd suggest to move away from the SerializedSubject, but compose it's synchronization inside the respective original subjects, if created as serialized subject. For the API this would be transparent, as currently only Subject is returned and returning the original subject's type from toSerialized()would not break this.

@akarnokd
Copy link
Member

Hi.

  1. getValue() is thread-safe thus if you keep the BehaviorSubject next to the SerializedSubject, you can still get to it.
  2. This can't be reasonably encoded in types. Again, if you need this information, keep it around the subject.
  3. This is a general issue of things that have to be learned once. No clever type signatures or API can help if those have to be learned about too. People don't read docs and don't tend to scroll through a lot of methods.

but compose it's synchronization inside the respective original subjects

No. This has been discussed before. If you are emitting from the same thread, the serialization is an overhead for no good reason.

@akarnokd akarnokd closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
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

2 participants