Why doesn't SignalProducer return a Signal?

℡╲_俬逩灬. 提交于 2019-12-05 05:44:44

I think the main reason is some events can be sent immediately when the producer starts.

For example, if you don't like the start series interface, and want to get a signal directly when start:

extension SignalProducer {
    func getSignalFromStart() -> Signal<Value, Error> {
        var signal: Signal<Value, Error>!
        startWithSignal{ innerSignal, _ in
            signal = innerSignal
        }
        return signal
    }
}

Then you can miss some events. Try this:

// When property.producer starts, it will send its current value immediately
let property = MutableProperty(1)

property.producer.getSignalFromStart().observeValues { value in
    print("getSignalFromStart \(value)") // maybe not what you want, only gets 2
}

property.producer.startWithValues { value in
    print("normal start \(value)") // this normally gets 1 and 2
}

property.value = 2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!