FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add the ability to specify RunLoop and RunLoopMode in `after` and `every` methods by madeinqc · Pull Request #38 · radex/SwiftyTimer · GitHub

This repository was archived by the owner on May 1, 2026. It is now read-only.

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .swift  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 20 additions & 6 deletions Sources/SwiftyTimer.swift
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
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
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
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

Back | FazBrowse Home | New Git URL