Coder Social home page Coder Social logo

Comments (12)

broadwaylamb avatar broadwaylamb commented on June 11, 2024 1

Instead of DispatchQueue.main use DispatchQueue.main.ocombine.

from opencombine.

maximkrouk avatar maximkrouk commented on June 11, 2024

I implemented a prototype this evening, maybe I'll make a draft PR tomorrow after cleaning up the codebase (or maybe @broadwaylamb may create a separate repo for the bridge if needed)

There is a chance, that this feature is not in-scope for the framework/community, but anyway it was fun to implement ๐ŸŒš

from opencombine.

matteogazzato avatar matteogazzato commented on June 11, 2024

Thanks @maximkrouk for your support, I tried to achieve my goal creating a draft custom Publisher and Subscription.
Here there my implementation

extension OpenCombine.Publisher {
    var combinePublisher: Combine.AnyPublisher<Output, Swift.Error> {
        BridegedPublisher(withOpenCombinePublisher: self)
            .eraseToAnyPublisher()
    }
}

public class BridegedPublisher<OpenCombinePublisher: OpenCombine.Publisher>: Combine.Publisher {
    public typealias Output = OpenCombinePublisher.Output
    public typealias Failure = Swift.Error
    
    private let ocPublisher: OpenCombinePublisher
    
    init(withOpenCombinePublisher publisher: OpenCombinePublisher) {
        self.ocPublisher = publisher
    }
    
    public func receive<S: Combine.Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input {
        subscriber.receive(
            subscription: BridgedSubscription(
                withOpenCombinePublisher: ocPublisher,
                combineSubscriber: subscriber
            )
        )
        
    }
}

public class BridgedSubscription<OpenCombinePublisher: OpenCombine.Publisher,
                                 CombineSubscriber: Combine.Subscriber>: Combine.Subscription
where CombineSubscriber.Input == OpenCombinePublisher.Output, CombineSubscriber.Failure == Swift.Error {
    
    private let ocPublisher: OpenCombinePublisher
    private let subject = Combine.PassthroughSubject<OpenCombinePublisher.Output, Swift.Error>()
    private var subscriber: CombineSubscriber?
    
    init(
        withOpenCombinePublisher publisher: OpenCombinePublisher,
        combineSubscriber: CombineSubscriber) {
        self.ocPublisher = publisher
        self.subscriber = combineSubscriber
        subject.receive(subscriber: self.subscriber!)
        
        _ = self.ocPublisher
            .sink { error in
            switch error {
            case .failure(let error):
                self.subject.send(completion: .failure(error))
            case .finished:
                self.subject.send(completion: .finished)
            }
        } receiveValue: { event in
            self.subject.send(event)
        }
    }
    
    public func request(_ demand: Combine.Subscribers.Demand) {
        // Not necessary?
    }
    
    public func cancel() {
        self.subscriber = nil
        
    }
}

Of course, this is only linked to publisher.
Your implementation would be a complete bridge between OpenCombine - Combine and that's great!
I'm following the combine-bridge merge request.

Thanks again ๐Ÿค™๐Ÿป

from opencombine.

maximkrouk avatar maximkrouk commented on June 11, 2024

My implementation is more low-level and relies on Combine private API, so you can't be sure that it won't break, so depending on what does @broadwaylamb thinks about it it might be rewritten to something higher-level, like your suggestion, so thank u for sharing ๐Ÿ™‚

from opencombine.

maximkrouk avatar maximkrouk commented on June 11, 2024

My implementation is more low-level and relies on Combine private API, so you can't be sure that it won't break, so depending on what does @broadwaylamb think about it, it might be rewritten to something higher-level, like your suggestion, so thank u for sharing ๐Ÿ™‚

from opencombine.

designerfuzzi avatar designerfuzzi commented on June 11, 2024

same idea here, so +1.
the problem i am facing is 'ObservableObject' is ambiguous for type lookup in this context in Xcode 11.3.1 which is the last Version that supports iOS 12 and below. In that particular setup ObservableObjectexists in Beta Framework "Combine" but can't be used and linking with OpenCombine produces the above ambiguous relation. Apple's Combine is not linked in my project and i just dont know a simple way to kick the relation in favour of OpenCombine yet. Maybe someone has some hint or link where i can read on about it. Apart from that "Publisher" would be a nice feature and indeed make development easier with support to lower OS <= OSX10.14

from opencombine.

designerfuzzi avatar designerfuzzi commented on June 11, 2024

uuh sometime one more cofรฉ helps..
i had to change

import Combine
class SomeCombineUsingClass : ObservableObject  {
    //...
}
//to
import OpenCombine
class SomeCombineUsingClass : OpenCombine.ObservableObject {
   //...
}

now taking on the missing @Publisher feature

from opencombine.

maximkrouk avatar maximkrouk commented on June 11, 2024

Yep, the issue is not in the bridge, but in Combine&OpenCombine, you have to use Module prefix if both modules are imported, but afaik you won't have to use it if you import OpenCombineBridge and one of the Combine frameworks ๐Ÿค”

OpenCombine is more like a replacement for the Combine, so in general it's not meant to be used with the Combine and names are kept the same ๐Ÿ™‚

Sadly some features are still not available ๐Ÿ˜”

from opencombine.

designerfuzzi avatar designerfuzzi commented on June 11, 2024

that leaves me with a simple question.. how to take on code parts that make use of @Publisher feature..
"just" 8 properties that needs to be rewritten .. which i can wrap in precompiler directives to avoid clashing for builds later on. Sorry if that is a little distracting from matteogazzato's issue

Maybe a blunt thought into the dust .. when @Publisher feature is not available in Swift then my thought is could @propertyWrapper help me out declaring same behaviour?

from opencombine.

broadwaylamb avatar broadwaylamb commented on June 11, 2024

I think you can just do this:

typealias Published = OpenOmbine.Published

This way you don't have to rewrite all your properties.

from opencombine.

designerfuzzi avatar designerfuzzi commented on June 11, 2024

nice one! thank you. but i can not admit done yet.
I linked all 4 OpenCombine libs, doesnt hurt yet, also import OpenCombine & import OpenCombineDispatch
them in code. Outcommented my own propertywrapper, and used your suggestion ^^but! instead i had to write
typealias Published = OpenCombine.Published and voila ! seems to make it work. One step further.

Now thinking.. same has to be done for Scheduler and who knows what will come up after.. but step by step.

someObserveable.$highlighted
    .receive(on: DispatchQueue.main) //<---- Argument type 'DispatchQueue' does not conform to expected type 'Scheduler'
    .sink { [weak self] newValue in
        self?.overlayView.needsDisplay = true
    }
    .store(in: &cancellables)

So i tried additionally
typealias Scheduler = OpenCombine.Scheduler
which as i was bluntly hoping can't fix it the same nice easy way.

from opencombine.

designerfuzzi avatar designerfuzzi commented on June 11, 2024

compile success. Resume..

import OpenCombine
import OpenCombineDispatch
typealias Published = OpenCombine.Published


someObserveable.$highlighted
    .receive(on: DispatchQueue.main.ocombine) 
    .sink { [weak self] newValue in
        self?.overlayView.needsDisplay = true
    }
    .store(in: &cancellables)

made it work. Thank you very much

from opencombine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.