CombineCommunity / CombineExt

CombineExt provides a collection of operators, publishers and utilities for Combine, that are not provided by Apple themselves, but are common in other Reactive Frameworks and standards.
https://combine.community
MIT License
1.72k stars 151 forks source link

Ability to create a Passthrough and CurrentValue relay from an existing subject #131

Closed rpassis closed 2 years ago

rpassis commented 2 years ago

I find both Passthrough and CurrentValue relays pretty useful however they require that the entire stack adopts that convention because there is no way to currently convert an existing Subject to a Relay.

This is useful when you are for example adding a new scene/feature which takes a subject from a previous screen as a way to communicate some user action.

For simplicity here's an example:

class MainVC:  UIViewController {
    func showNext(doneSubject: PassthroughSubject<Void, Never>) {
        let nextVC = NextVC(doneRelay: ????) // <- there is no way to convert a subject to a relay
        ....
    }
}

Since the backing storage for both Relay types are their corresponding (private) subjects, it would be useful to add initializers to the Relay types that take an existing subject as the backing storage.

Is this something you'd be willing to accept as a contribution? Let me know and I'd be happy to get a PR ready. AFAICT We should be able to overload the existing initializer so no breaking changes involved either.

Let me know what you think! Rog

freak4pc commented 2 years ago

The conversion here doesn't necessarily make sense. "Casting" a subject to a relay like this loses all events except for value (next) events without any fallbacks, which is a bit strange. It seems to me the right thing to do is make the subject a relay from the get-go if that's the contract you're trying to work with.