-
Notifications
You must be signed in to change notification settings - Fork 61
/
CwlDispatch.swift
151 lines (140 loc) · 6.7 KB
/
CwlDispatch.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// CwlDispatch.swift
// CwlUtils
//
// Created by Matt Gallagher on 2016/07/29.
// Copyright © 2016 Matt Gallagher ( https://www.cocoawithlove.com ). All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
import Foundation
public extension DispatchSource {
// An overload of timer that immediately sets the handler and schedules the timer
class func singleTimer(interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), queue: DispatchQueue, handler: @escaping () -> Void) -> DispatchSourceTimer {
let result = DispatchSource.makeTimerSource(queue: queue)
result.setEventHandler(handler: handler)
#if swift(>=4)
result.schedule(deadline: DispatchTime.now() + interval, leeway: leeway)
#else
result.scheduleOneshot(deadline: DispatchTime.now() + interval, leeway: leeway)
#endif
result.resume()
return result
}
// An overload of timer that always uses the default global queue (because it is intended to enter the appropriate mutex as a separate step) and passes a user-supplied Int to the handler function to allow ignoring callbacks if cancelled or rescheduled before mutex acquisition.
class func singleTimer<T>(parameter: T, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), queue: DispatchQueue = DispatchQueue.global(), handler: @escaping (T) -> Void) -> DispatchSourceTimer {
let result = DispatchSource.makeTimerSource(queue: queue)
result.scheduleOneshot(parameter: parameter, interval: interval, leeway: leeway, handler: handler)
result.resume()
return result
}
// An overload of timer that immediately sets the handler and schedules the timer
class func repeatingTimer(interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), queue: DispatchQueue = DispatchQueue.global(), handler: @escaping () -> Void) -> DispatchSourceTimer {
let result = DispatchSource.makeTimerSource(queue: queue)
result.setEventHandler(handler: handler)
#if swift(>=4)
result.schedule(deadline: DispatchTime.now() + interval, repeating: interval, leeway: leeway)
#else
result.scheduleRepeating(deadline: DispatchTime.now() + interval, interval: interval, leeway: leeway)
#endif
result.resume()
return result
}
// An overload of timer that always uses the default global queue (because it is intended to enter the appropriate mutex as a separate step) and passes a user-supplied Int to the handler function to allow ignoring callbacks if cancelled or rescheduled before mutex acquisition.
class func repeatingTimer<T>(parameter: T, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), queue: DispatchQueue = DispatchQueue.global(), handler: @escaping (T) -> Void) -> DispatchSourceTimer {
let result = DispatchSource.makeTimerSource(queue: queue)
result.scheduleRepeating(parameter: parameter, interval: interval, leeway: leeway, handler: handler)
result.resume()
return result
}
}
public extension DispatchSourceTimer {
// An overload of scheduleOneshot that updates the handler function with a new user-supplied parameter when it changes the expiry deadline
func scheduleOneshot<T>(parameter: T, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), handler: @escaping (T) -> Void) {
suspend()
setEventHandler { handler(parameter) }
#if swift(>=4)
schedule(deadline: DispatchTime.now() + interval, leeway: leeway)
#else
scheduleOneshot(deadline: DispatchTime.now() + interval, leeway: leeway)
#endif
resume()
}
// An overload of scheduleOneshot that updates the handler function with a new user-supplied parameter when it changes the expiry deadline
func scheduleRepeating<T>(parameter: T, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0), handler: @escaping (T) -> Void) {
suspend()
setEventHandler { handler(parameter) }
#if swift(>=4)
schedule(deadline: DispatchTime.now() + interval, repeating: interval, leeway: leeway)
#else
scheduleRepeating(deadline: DispatchTime.now() + interval, interval: interval, leeway: leeway)
#endif
resume()
}
}
public extension DispatchTime {
func since(_ previous: DispatchTime) -> DispatchTimeInterval {
return .nanoseconds(Int(uptimeNanoseconds - previous.uptimeNanoseconds))
}
}
public extension DispatchTimeInterval {
static func interval(_ seconds: TimeInterval) -> DispatchTimeInterval {
if MemoryLayout<Int>.size < 8 {
return .milliseconds(Int(seconds * Double(NSEC_PER_SEC / NSEC_PER_MSEC)))
} else {
return .nanoseconds(Int(seconds * Double(NSEC_PER_SEC)))
}
}
var seconds: Double {
#if swift (>=3.2)
switch self {
case .seconds(let t): return Double(t)
case .milliseconds(let t): return (Double(NSEC_PER_MSEC) / Double(NSEC_PER_SEC)) * Double(t)
case .microseconds(let t): return (Double(NSEC_PER_USEC) / Double(NSEC_PER_SEC)) * Double(t)
case .nanoseconds(let t): return (1.0 / Double(NSEC_PER_SEC)) * Double(t)
case .never: return Double.infinity
#if swift (>=5)
default: fatalError("Unknown case")
#endif
}
#else
switch self {
case .seconds(let t): return Double(t)
case .milliseconds(let t): return (Double(NSEC_PER_MSEC) / Double(NSEC_PER_SEC)) * Double(t)
case .microseconds(let t): return (Double(NSEC_PER_USEC) / Double(NSEC_PER_SEC)) * Double(t)
case .nanoseconds(let t): return (1.0 / Double(NSEC_PER_SEC)) * Double(t)
}
#endif
}
var nanoseconds: Int64 {
#if swift (>=3.2)
switch self {
case .seconds(let t): return Int64(NSEC_PER_SEC) * Int64(t)
case .milliseconds(let t): return Int64(NSEC_PER_MSEC) * Int64(t)
case .microseconds(let t): return Int64(NSEC_PER_USEC) * Int64(t)
case .nanoseconds(let t): return Int64(t)
case .never: return Int64.max
#if swift (>=5)
default: fatalError("Unknown case")
#endif
}
#else
switch self {
case .seconds(let t): return Int64(NSEC_PER_SEC) * Int64(t)
case .milliseconds(let t): return Int64(NSEC_PER_MSEC) * Int64(t)
case .microseconds(let t): return Int64(NSEC_PER_USEC) * Int64(t)
case .nanoseconds(let t): return Int64(t)
}
#endif
}
}