-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.swift
94 lines (71 loc) · 2.96 KB
/
Timer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Timer.swift
//
// Created by Pablo Balduz on 06/08/2019.
// Copyright © 2019 Pablo Balduz. All rights reserved.
//
import Foundation
typealias NanoSeconds = UInt64
typealias TimeUpdater = Foundation.Timer
extension NanoSeconds {
/// A `Double` representing the milliseconds for the given nanoseconds
var milliseconds: Double {
return Double(self) / 1_000_000
}
/// A `Double` representing the seconds for the given nanoseconds
var seconds: Double {
return Double(self) / 1_000_000_000
}
}
/// The `TimerDelegate` provides an interface for responding to `Timer` instance events. This includes whenever a time update happens.
protocol TimerDelegate: class {
/// Triggered when a time update happens according to `TimerUpdateRate` value in `Timer`
///
/// - Parameters:
/// - time: The time elapsed (expressed in nanoseconds) since the last time the `Timer` instance was started
func timeDidUpdate(timeElapsedSinceLastStart time: NanoSeconds)
}
/// The `Timer` class is a custom timer that provides the elapsed time between a start and a stop. This time is expressed in nanoseconds. It also triggers time updates notifications via a `TimerDelegate`.
class Timer {
// MARK: - Public properties
/// A `TimeInterval` to control the time updates triggering rate
static var TimerUpdateRate: TimeInterval = 0.1
/// A `TimerDelegate` to handle events from `Timer`
weak var delegate: TimerDelegate?
/// A `UInt64` value representing the time elapsed since the `Timer` instance was started. It is expressed in nanoseconds
var currentElapsed: NanoSeconds {
let elapsed = stopTime ?? mach_absolute_time() - startTime
return elapsed * NanoSeconds(Timer.b.numer) / NanoSeconds(Timer.b.denom)
}
// MARK: - Private properties
private static var b: mach_timebase_info = mach_timebase_info(numer: 0, denom: 0)
private var startTime: NanoSeconds = 0
private var stopTime: NanoSeconds?
private var updater: TimeUpdater?
// MARK: - Init
/// Initializes `Timer` instance
init() {
mach_timebase_info(&Timer.b)
}
// MARK: - Private methods
@objc private func timerUpdate(timer: TimeUpdater) {
self.delegate?.timeDidUpdate(timeElapsedSinceLastStart: self.currentElapsed)
}
// MARK: - Public methods
/// Starts counting and initializes time updates if a `TimerDelegate` is set
func start() {
stopTime = nil
startTime = mach_absolute_time()
if delegate != nil && updater == nil {
let timer = TimeUpdater(timeInterval: Timer.TimerUpdateRate, repeats: true, block: timerUpdate)
RunLoop.current.add(timer, forMode: .common)
updater = timer
}
}
/// Stops counting and time updates
func stop() {
stopTime = mach_absolute_time()
updater?.invalidate()
updater = nil
}
}