-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAsync.swift
163 lines (119 loc) · 4.08 KB
/
SimpleAsync.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
152
153
154
155
156
157
158
159
160
161
162
//
// SwiftAsync.swift
// SwiftNotif
//
// Created by Seyyed Parsa Neshaei on 8/6/17.
// Copyright © 2017 Seyyed Parsa Neshaei. All rights reserved.
//
import Foundation
public class SimpleAsync {
/**
Sets the quality of service.
- Main Values:
- `.default`
- `.userInteractive` - All UI related tasks need to be assigned User Interactive.
- `.utility` - Best for networking and file I/O.
- `.background` - Best for tasks that don't depend on their fast execution.
*/
public static let qualityOfService: DispatchQoS.QoSClass = .default
/**
Runs the specified code in async mode.
- Parameters:
- code: The block which will run in async mode.
*/
public static func async(_ code: @escaping (() -> Void)) {
DispatchQueue.global(qos: self.qualityOfService).async(execute: code)
}
/**
Runs the specified code in sync mode.
- Parameters:
- code: The block which will run in sync mode.
*/
public static func sync(_ code: @escaping (() -> Void)) {
DispatchQueue.global(qos: self.qualityOfService).sync(execute: code)
}
/**
Used to run a task after a specified delay. You can call the normal `async` and `main` methods on the return value of this method.
- Parameters:
- seconds: Specifies the delay for running tasks.
*/
public static func after(seconds: Double) -> SimpleAsyncJob {
return SimpleAsyncJob(delayInSeconds: seconds)
}
/**
Runs the specified code in the main thread.
- Parameters:
- code: The block which will run in the main thread.
*/
public static func main(_ code: @escaping (() -> Void)) {
DispatchQueue.main.async(execute: code)
}
}
/**
The helper class for running tasks. This should not be used directly.
*/
public class SimpleAsyncJob {
private var delayInSeconds = 0.0
init(delayInSeconds: Double) {
self.delayInSeconds = delayInSeconds
}
init() {
}
/**
Used to run a task after a specified delay. You can call the normal `async` and `main` methods on the return value of this method.
- Parameters:
- seconds: Specifies the delay for running tasks.
*/
public func after(seconds: Double) -> SimpleAsyncJob {
self.delayInSeconds += seconds
return self
}
/**
Runs the specified code in async mode.
- Parameters:
- code: The block which will run in async mode.
*/
public func async(_ code: @escaping (() -> Void)) {
DispatchQueue.global(qos: SimpleAsync.qualityOfService).asyncAfter(deadline: .now() + self.delayInSeconds, execute: code)
}
/**
Runs the specified code in the main thread.
- Parameters:
- code: The block which will run in the main thread.
*/
public func main(_ code: @escaping (() -> Void)) {
DispatchQueue.main.asyncAfter(deadline: .now() + self.delayInSeconds, execute: code)
}
}
/**
Runs the specified code in async mode.
- Parameters:
- code: The block which will run in async mode.
*/
public func async(_ code: @escaping (() -> Void)) {
DispatchQueue.global(qos: SimpleAsync.qualityOfService).async(execute: code)
}
/**
Runs the specified code in sync mode.
- Parameters:
- code: The block which will run in sync mode.
*/
public func sync(_ code: @escaping (() -> Void)) {
DispatchQueue.global(qos: SimpleAsync.qualityOfService).sync(execute: code)
}
/**
Used to run a task after a specified delay. You can call the normal `async` and `main` methods on the return value of this method.
- Parameters:
- seconds: Specifies the delay for running tasks.
*/
public func after(seconds: Double) -> SimpleAsyncJob {
return SimpleAsyncJob(delayInSeconds: seconds)
}
/**
Runs the specified code in the main thread.
- Parameters:
- code: The block which will run in the main thread.
*/
public func main(_ code: @escaping (() -> Void)) {
DispatchQueue.main.async(execute: code)
}