Skip to content

Add the ability to specify RunLoop and RunLoopMode in after and every methods #38

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions Sources/SwiftyTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ extension Timer {
/// Create and schedule a timer that will call `block` once after the specified time.

@discardableResult
public class func after(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
public class func after(_ interval: TimeInterval, runLoop: RunLoop = .current, modes: RunLoopMode..., _ block: @escaping () -> Void) -> Timer {
let timer = Timer.new(after: interval, block)
timer.start()
timer.start(runLoop: runLoop, modes: modes)
return timer
}

/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.

@discardableResult
public class func every(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
public class func every(_ interval: TimeInterval, runLoop: RunLoop = .current, modes: RunLoopMode..., _ block: @escaping () -> Void) -> Timer {
let timer = Timer.new(every: interval, block)
timer.start()
timer.start(runLoop: runLoop, modes: modes)
return timer
}

/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.
/// (This variant also passes the timer instance to the block)

@nonobjc @discardableResult
public class func every(_ interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer {
public class func every(_ interval: TimeInterval, runLoop: RunLoop = .current, modes: RunLoopMode..., _ block: @escaping (Timer) -> Void) -> Timer {
let timer = Timer.new(every: interval, block)
timer.start()
timer.start(runLoop: runLoop, modes: modes)
return timer
}

Expand Down Expand Up @@ -111,6 +111,20 @@ extension Timer {
runLoop.add(self, forMode: mode)
}
}

/// Schedule this timer on the run loop
///
/// By default, the timer is scheduled on the current run loop for the default mode.
/// Specify `runLoop` or `modes` to override these defaults.
/// Usefull since the splat operator is not a thing in Swift

public func start(runLoop: RunLoop = .current, modes: [RunLoopMode]) {
let modes = modes.isEmpty ? [.defaultRunLoopMode] : modes

for mode in modes {
runLoop.add(self, forMode: mode)
}
}
}

// MARK: - Time extensions
Expand Down
13 changes: 13 additions & 0 deletions SwiftyTimerTests/SwiftyTimerTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {
guard fires <= 1 else { fatalError("should be invalidated") }
defer { fires += 1 }

if fires == 1 {
timer.invalidate()
self.test10()
}
}
}

func test10() {
var fires = 0
Timer.every(0.1.seconds, modes: .commonModes) { (timer: Timer) in
guard fires <= 1 else { fatalError("should be invalidated") }
defer { fires += 1 }

if fires == 1 {
timer.invalidate()
self.done()
Expand Down