Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how would i create a Timer signal #25

Open
jpmhouston opened this issue Jun 29, 2017 · 2 comments
Open

how would i create a Timer signal #25

jpmhouston opened this issue Jun 29, 2017 · 2 comments

Comments

@jpmhouston
Copy link

jpmhouston commented Jun 29, 2017

Any tips for creating a timer signal? Here's what I tried throwing together based on examples and a gesture recognizer signal I made that does work, but it doesn't fire.

public extension Signal {
    // in case of a Timer, we dont start with an object on which we bootstrap a signal
    // so add this class method on Signal itself for creating a timer signal (can we make this a convenience init instead?)
    public class func repeatingTimer(withInterval interval: TimeInterval) -> Signal<Timer> {
        let signal = Signal<Timer>()
        let observer = IntervalTimerOperator(interval: interval)
        
        observer.callback = { [weak signal] timer in
            signal?.send(timer)
        }
        
        signal.disposableSource = observer
        
        return signal
    }
}

final class IntervalTimerOperator: NSObject, Disposable {
    private var timer: Timer!
    private var isDisposed = false
    
    internal var callback: ((_ timer: Timer) -> Void)?
    
    init(interval: TimeInterval, repeats: Bool = true) {
        super.init()
        self.timer = Timer(timeInterval: interval, target: self, selector: #selector(fire(_:)), userInfo: nil, repeats: repeats)
    }
    
    func fire(_: Timer) {
        callback?(timer)
    }
    
    func dispose() {
        guard !isDisposed else { return }
        timer.invalidate()
        isDisposed = true
    }
}
@jpmhouston
Copy link
Author

oops, needed to use Timer.scheduleTimer(..). now it works. using needs to be like:

Signal<Timer>.repeatingTimer(withInterval: interval).next { [weak self] _ in
    self?.checkTheThing()
}.disposeWith(disposalBag)

does anyone have a better suggestion for creating besides using an extension on Signal? perhaps extension on Timer itself?

@yankodimitrov
Copy link
Owner

Hi,

I think an extension on the Timer type will be cleaner:

extension Timer {
    
    public func observe() -> Signal<Void> {
       
        let signal = Signal<Void>()
        let observer = IntervalTimerOperator(interval: interval)
        
        observer.callback = { [weak signal] _ in
            signal?.send()
        }
        
        signal.disposableSource = observer
        
        return signal
    }
}

Then you can use it like this:

let timer = Timer(interval: interval)

timer.observe().next { [weak self] _ in
    self?.checkTheThing()
}.disposeWith(disposalBag)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants