Skip to content

Instantly share code, notes, and snippets.

@hisui
Last active September 12, 2017 00:49
Show Gist options
  • Save hisui/e242ec8f8feca5b5ee54 to your computer and use it in GitHub Desktop.
Save hisui/e242ec8f8feca5b5ee54 to your computer and use it in GitHub Desktop.
import RxSwift
let Completed = NSError(domain: "Completed", code: 0, userInfo: nil)
class CountdownTimer {
private let publisher = PublishSubject<Int>()
private let signal: Observable<Int>
init(_ count: Int) {
signal =
interleave([publisher,
interval(1
, MainScheduler.sharedInstance)
//>- publish
>- take(count+1)
>- map { count - Int($0) }])
}
var observable: Observable<Int> { return signal }
func sendCompleted() {
publisher.on(.Completed)
}
}
func interleave<E>(a: [Observable<E>]) -> Observable<E> {
return from(a.map { concat([$0, failWith(Completed)]) })
>- merge
>- catch {
$0 == Completed ? empty(): failWith($0)
}
}
@kmuddapu
Copy link

Can you please update. It would be helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment