Skip to content

Instantly share code, notes, and snippets.

@colinator
Last active July 15, 2016 15:23
Show Gist options
  • Save colinator/08af004db0786e3c18c06de3c722dd82 to your computer and use it in GitHub Desktop.
Save colinator/08af004db0786e3c18c06de3c722dd82 to your computer and use it in GitHub Desktop.
Reactive Cocoa custom signal: diffDetect
// 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