0

I am trying to bind a delegate this is what was done

/** Delegate Proxy **/
func castOrThrow<T>(_ resultType: T.Type, _ object:Any) throws -> T {
    guard let returnValue = object as? T else {
        throw RxCocoaError.castingError(object: object, targetType: resultType)
    }
    return returnValue
}
@objc
public protocol TestEventGeneratorDelegate:class {
    @objc optional func asyncEventResult(int:Int)
}
open class TestEventGenerator {
    public var delegate: TestEventGeneratorDelegate?
    func asyncCall() {
        DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in
            guard let self = self else  { return }
            self.delegate?.asyncEventResult!(int: 0)
        }
    }
}
extension TestEventGenerator: HasDelegate {
    public typealias Delegate = TestEventGeneratorDelegate
}
open class RxTestDelegateProxy : DelegateProxy<TestEventGenerator,TestEventGeneratorDelegate>,
                                DelegateProxyType,
                                TestEventGeneratorDelegate {
    public weak private(set) var testGenerator: TestEventGenerator?
    public init(testEventGenerator: ParentObject) {
        self.testGenerator = testEventGenerator
        super.init(parentObject: testEventGenerator, delegateProxy: RxTestDelegateProxy.self)
    }
    public static func registerKnownImplementations() {
        self.register { RxTestDelegateProxy(testEventGenerator: $0) }
    }
}
extension Reactive where Base: TestEventGenerator {
    public var delegate: DelegateProxy<TestEventGenerator, TestEventGeneratorDelegate> {
        return RxTestDelegateProxy.proxy(for: base)
    }
    public var asyncEventResult: Observable<Int> {
        let source =  delegate.methodInvoked(#selector(TestEventGeneratorDelegate.asyncEventResult(int:)))
        .map { (a) in
            return try castOrThrow(Int.self,a[0])
        }
        return source
    }
}
/**  Delegate Proxy **/

Then when I use it

let testEventGenerator = TestEventGenerator()
textEventGenerator.rx.asyncEventResult.subscribe.... // subscribe here no rx found? 
testEventGenerator.asyncCall() // call and wait for the observable to return to the delegate 

It doesn't compile and says there is no such rx

I have to bind it because the person who wrote the API didn't use a callback to return the value but rather a delegate.

This is example of how he wrote the code and how I want to wrap it.

Is there a way to bind a delegate that requires a kick off from asyncCall()?

so I can chain it using flatmapLatest in a promiseLike way?

Thanks let me know!

I followed these tutorial: How to convert Delegate to Observable RxSwift? and https://blog.ippon.fr/2018/11/13/rxswift-how-to-make-your-favorite-delegate-based-apis-reactive/

George Host
  • 980
  • 1
  • 12
  • 26

1 Answers1

0

have conform to TestEventGenerator:ReactiveCompatible and that will fix it.

George Host
  • 980
  • 1
  • 12
  • 26