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

PublishSubject -> PassthroughSubject? #22

Open
sshnip opened this issue Oct 28, 2020 · 5 comments
Open

PublishSubject -> PassthroughSubject? #22

sshnip opened this issue Oct 28, 2020 · 5 comments

Comments

@sshnip
Copy link

sshnip commented Oct 28, 2020

It's more like question not an issue. As I can see, in rxswift PublishSubject often used for binding + subscribing, e.g. the main idea of that class is to implement both Observer and Observable interfaces. In Combine this's no true because PassthroughSubject only implementes Pusblisher protocol and cannot be used as a subscriber. What do you think about that?

@freak4pc
Copy link
Member

freak4pc commented Oct 28, 2020

You can use a subject as a subscriber using this specific subscribe overload:
https://developer.apple.com/documentation/combine/publisher/subscribe(_:)-3fk20

@sshnip
Copy link
Author

sshnip commented Nov 3, 2020

@freak4pc Thanks for a good point! I've tried to make a simple implementation of this method and it worked with simple example. Now I'm struggling with another thing.
My desired result is to use parent -> child objects which should be connected using Publishers and Subscribers. Sometimes I need to use some intermediate actions, like PassthroughSubject<CustomType, Never>.map... and subscribe another PassthroughSubject<String, Never> to the result of this mapping. So in the end I couldn't find the proper way to erase result of map operator, there's no eraseToAnySubject() atm. I would like to implement it myself but I don't really know where to start from. Is there anything ready-to-use for my case in current Combine API?
PS: I've actively used such approach using RxSwift but atm I couldn't use it.

Thanks in advance

@freak4pc
Copy link
Member

freak4pc commented Nov 3, 2020

What did you do in RxSwift to achieve this? @sshnip

@sshnip
Copy link
Author

sshnip commented Nov 13, 2020

Hi @freak4pc
Sorry for the delay, here's the sample and I couldn't figure out the good way to do the same in combine. You could ignore BehaviorRelay, it could be any kind of publisher. So I would like to make a subscription in a similar manner, after doing map and without that as well let initalReplay = BehaviorRelay(value: 1)

        let intermediateReplay = ReplaySubject<String?>.create(bufferSize: 1)
        let finalSubject = PublishSubject<String?>()
        let anotherSubj = PublishSubject<Int>()
        initalReplay
            .map({ (value) -> String? in
                return String(describing: value)
            })
            .subscribe(intermediateReplay)
            .disposed(by: disposables)
        intermediateReplay
            .share(replay: 1, scope: .forever)
            .subscribe(finalSubject)
            .disposed(by: disposables)
        initalReplay
            .subscribe(anotherSubj)
            .disposed(by: disposables)
        finalSubject
            .subscribe { (value) in
                print(value)
            }
            .disposed(by: disposables)
        anotherSubj
            .subscribe { (value) in
                print("Another subj received \(String(describing: value))")
            }
            .disposed(by: disposables)
        initalReplay.accept(5)
        initalReplay.accept(6)
        initalReplay.accept(7)

@freak4pc
Copy link
Member

This is quite a lot of code with multiple players. Mind focusing this up a bit more on the portion where this issue relates? I mean the relation with Passthrogh and Publish?

If it's a general Combine question I suggest posting in Slack, more peopel look there. http://slack.combine.community

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

No branches or pull requests

2 participants