Comments (12)
Instead of DispatchQueue.main
use DispatchQueue.main.ocombine
.
from opencombine.
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.
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.
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.
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.
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 ObservableObject
exists 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.
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.
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.
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.
I think you can just do this:
typealias Published = OpenOmbine.Published
This way you don't have to rewrite all your properties.
from opencombine.
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.
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)
- the `*` in DispatchQueue scheduler's SchedulerTimeType gives wrong value HOT 3
- Ready for production? HOT 3
- Slack Invite is no longer valid HOT 1
- Doesn't compile on macOS Monterey / XCode 13 HOT 2
- [Feature Request] Async Publisher.values HOT 1
- Re-enable concurrency tests on Wasm when swiftwasm supports them
- Re-enable concurrency tests on Windows when Windows issues are fixed
- error HOT 5
- [Bug]The Publishers.ReceiveOn may lead to subscriber never receive the published single when schedule is concurrent queue.
- [Bug]The Publishers.ReceiveOn may lead to subscriber never receive the published signal when scheduler is concurrent queue.
- would be nice to be able to use "@Published" directive in earlier Swift versions that do not know it HOT 2
- Add primary associated type support HOT 1
- Unable to build on Ubuntu 22.04.1 HOT 2
- Zip crash HOT 3
- When BUILD_LIBRARY_FOR_DISTRIBUTION is set to YES, the build will fail. HOT 1
- Xcode 15 crash HOT 1
- EXC_BAD_ACCESS when repeatedly using Future HOT 3
- When Future call Cancel, why parent is copied by take()?
- error build: Undefined symbol: _OBJC_CLASS_$
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opencombine.