This PR contains all the work related to setting up this project as required to implement the [Assignment](https://repo.rock-n-code.com/rock-n-code/deep-linking-assignment/wiki/Assignment) on top, as intended. To summarise this work: - [x] created a new **Xcode** project; - [x] cloned the `Wikipedia` app and inserted it into the **Xcode** project; - [x] created the `Locations` app and also, its `Libraries` package; - [x] created the `Shared` package to share dependencies between the apps; - [x] added a `Makefile` file and implemented some **environment** and **help** commands. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#1
54 lines
1.8 KiB
Swift
54 lines
1.8 KiB
Swift
public class RepeatingTimer {
|
|
private let source: DispatchSourceTimer
|
|
private let semaphore: DispatchSemaphore = DispatchSemaphore(value: 1)
|
|
private var isSet: Bool = true
|
|
|
|
public init(_ intervalInSeconds: TimeInterval, afterDelay delayInSeconds: TimeInterval? = nil, leeway leewayInSeconds: TimeInterval? = nil, on queue: DispatchQueue = DispatchQueue.global(qos: .background), _ handler: @escaping () -> Void) {
|
|
|
|
let interval = DispatchTimeInterval.nanoseconds(Int(intervalInSeconds * TimeInterval(NSEC_PER_SEC)))
|
|
|
|
let delay: DispatchTimeInterval
|
|
if let delayInSeconds = delayInSeconds {
|
|
delay = DispatchTimeInterval.nanoseconds(Int(delayInSeconds * TimeInterval(NSEC_PER_SEC)))
|
|
} else {
|
|
delay = interval
|
|
}
|
|
|
|
let leeway: DispatchTimeInterval
|
|
if let leewayInSeconds = leewayInSeconds {
|
|
leeway = DispatchTimeInterval.nanoseconds(Int(leewayInSeconds * TimeInterval(NSEC_PER_SEC)))
|
|
} else {
|
|
leeway = DispatchTimeInterval.nanoseconds(Int(0.1 * intervalInSeconds * TimeInterval(NSEC_PER_SEC)))
|
|
}
|
|
|
|
self.source = DispatchSource.makeTimerSource(queue: queue)
|
|
self.source.schedule(deadline: DispatchTime.now() + delay, repeating: interval, leeway: leeway)
|
|
self.source.setEventHandler(handler: handler)
|
|
self.source.resume()
|
|
}
|
|
|
|
public func resume() {
|
|
semaphore.wait()
|
|
defer {
|
|
semaphore.signal()
|
|
}
|
|
guard !isSet else {
|
|
return
|
|
}
|
|
isSet = true
|
|
self.source.resume()
|
|
}
|
|
|
|
public func pause() {
|
|
semaphore.wait()
|
|
defer {
|
|
semaphore.signal()
|
|
}
|
|
guard isSet else {
|
|
return
|
|
}
|
|
isSet = false
|
|
source.suspend()
|
|
}
|
|
}
|