SignalProducer简单模型

仿照RAS实现SignalProducer

SignalProducer的作用:

  1. 与Signal相比,Signal在调用send的时候,指定观察者动作,SignalProducer在构造时候,就指定了观察者动作;

SignalProducer的实现:

  1. 在执行start方法时,构造了Signal,将观察者对象添加到Signal中,指定SignalProducer的startHandler,并传入内部的obs控制对象
image.png

附代码:

struct aSignalProducer {
    let core: aSignalProducerCore
    
    init(_ startHandler: @escaping (aSignal.aObserver) -> Void) {
        self.init(aSignalCore {
            let fakeDisposable = aDisposable({ print("fake Disposable") })
            let (signal, obs) = aSignal.pipe()
            let observerDidSetup = { startHandler(obs) }
            
            return aSignalCore.Instance(signal: signal, observerDidSetup: observerDidSetup, interruptedHandle: fakeDisposable)
        })
    }
    
    init(value: Value) {
        self.init(aGeneratorCore { observer, _ in
            observer.send(value)
        })
    }
    
    init(_ core: aSignalProducerCore) {
        self.core = core
    }
    
    @discardableResult
    func start(_ observer: aSignal.aObserver) -> aDisposable {
        return core.start { _ in observer }
    }
}

internal class aSignalProducerCore {
    struct Instance {
        let signal: aSignal
        let observerDidSetup: () -> Void
        let interruptedHandle: aDisposable
    }
    
    func makeInstance() -> Instance {
        fatalError()
    }
    
    func start(_ generator: (_ disposable: aDisposable) -> aSignal.aObserver) -> aDisposable {
        fatalError()
    }
}

private class aSignalCore: aSignalProducerCore {
    let _make: () -> Instance
    
    init(_ action: @escaping () -> Instance) {
        self._make = action
    }
    
    override func makeInstance() -> aSignalProducerCore.Instance {
        return self._make()
    }
    
    override func start(_ generator: (aDisposable) -> aSignal.aObserver) -> aDisposable {
        let instance = makeInstance()
        instance.signal.observe(generator(instance.interruptedHandle))
        instance.observerDidSetup()
        
        return instance.interruptedHandle
    }
}

private class aGeneratorCore: aSignalProducerCore {
    let _generator: (aSignal.aObserver, aDisposable) -> Void
    
    init(_ generator: @escaping (aSignal.aObserver, aDisposable) -> Void) {
        self._generator = generator
    }
    
    override func makeInstance() -> aSignalProducerCore.Instance {
        let fakeDisposable = aDisposable({})
        let (signal, obs) = aSignal.pipe()
    
        
        return Instance(signal: signal, observerDidSetup: { self._generator(obs, fakeDisposable) }, interruptedHandle: fakeDisposable)
    }
    
    override func start(_ generator: (aDisposable) -> aSignal.aObserver) -> aDisposable {
        let instance = makeInstance()
        instance.signal.observe(generator(instance.interruptedHandle))
        instance.observerDidSetup()
        
        return instance.interruptedHandle
    }
}

        /**
         * aSignalProducer 使用方式
         */
        
        let asps = aSignalProducer { observer in
            observer.send("22222")
        }
        
        let asubscriber = aSignal.aObserver { event in
            if case let .value(v) = event {
                print(v)
            } else if case .interupt = event {
                print("interrupted")
            }
        }
        
        asps.start(asubscriber)

你可能感兴趣的:(SignalProducer简单模型)