-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1602 from mapbox/jerrad/146-navigation-service
Navigation Service, Part 1
- Loading branch information
Showing
44 changed files
with
1,827 additions
and
1,562 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Foundation | ||
import Dispatch | ||
|
||
class CountdownTimer { | ||
enum State { | ||
case armed, disarmed | ||
} | ||
|
||
typealias Payload = DispatchSource.DispatchSourceHandler | ||
static let defaultAccuracy: DispatchTimeInterval = .milliseconds(500) | ||
let countdownInterval: DispatchTimeInterval | ||
private var deadline: DispatchTime { return .now() + countdownInterval } | ||
let accuracy: DispatchTimeInterval | ||
let payload: Payload | ||
let timerQueue = DispatchQueue(label: "com.mapbox.coreNavigation.timer.countdown", qos: DispatchQoS.background, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil) | ||
let executionQueue: DispatchQueue | ||
let timer: DispatchSourceTimer | ||
|
||
private(set) var state: State = .disarmed | ||
|
||
init(countdown: DispatchTimeInterval, payload: @escaping Payload, accuracy: DispatchTimeInterval = defaultAccuracy, executingOn executionQueue: DispatchQueue = .main) { | ||
countdownInterval = countdown | ||
self.executionQueue = executionQueue | ||
self.payload = payload | ||
self.accuracy = accuracy | ||
self.timer = DispatchSource.makeTimerSource(flags: [], queue: timerQueue) | ||
} | ||
|
||
deinit { | ||
timer.setEventHandler {} | ||
timer.cancel() | ||
/* | ||
If the timer is suspended, calling cancel without resuming | ||
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902 | ||
*/ | ||
if state == .disarmed { | ||
timer.resume() | ||
} | ||
} | ||
|
||
private func scheduleTimer() { | ||
timer.schedule(deadline: deadline, repeating: .never, leeway: accuracy) | ||
} | ||
|
||
private func fire() { | ||
executionQueue.async(execute: payload) | ||
} | ||
|
||
func arm() { | ||
guard state == .disarmed, !timer.isCancelled else { return } | ||
state = .armed | ||
scheduleTimer() | ||
timer.setEventHandler(handler: fire) | ||
timer.resume() | ||
} | ||
|
||
func reset() { | ||
guard state == .armed, !timer.isCancelled else { return } | ||
timer.suspend() | ||
scheduleTimer() | ||
timer.resume() | ||
} | ||
|
||
func disarm() { | ||
guard state == .armed, !timer.isCancelled else { return } | ||
state = .disarmed | ||
timer.suspend() | ||
timer.setEventHandler {} | ||
} | ||
} |
Oops, something went wrong.