Last active
July 15, 2016 15:23
-
-
Save colinator/08af004db0786e3c18c06de3c722dd82 to your computer and use it in GitHub Desktop.
Reactive Cocoa custom signal: diffDetect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of writing a custom signal: | |
// Starts false, sends true when the signal departs from | |
// the initial value. Thereafter sends nothing, until the | |
// signal gets back to initial value, whereupon sends false. | |
import ReactiveCocoa | |
extension SignalType where Value: Equatable { | |
func diffDetect(i: Value) -> Signal<Bool, Error> { | |
return Signal { observer in | |
var lastDiff = true | |
return self.observeNext { (v: Self.Value) in | |
let diff = !(v == i) | |
if lastDiff != diff { | |
lastDiff = diff | |
observer.sendNext(diff) | |
} | |
} | |
} | |
} | |
} | |
extension SignalProducerType where Value: Equatable { | |
func diffDetect(i: Value) -> SignalProducer<Bool, Error> { | |
return lift { $0.diffDetect(i) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment