Last active
September 12, 2017 00:49
-
-
Save hisui/e242ec8f8feca5b5ee54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please update. It would be helpful