@@ -43,9 +43,7 @@ public final class JobBuilder {
43
43
/// If override = true the previous job will be canceled and the new job will be scheduled
44
44
public func singleInstance( forId: String , override: Bool = false , includeExecutingJob: Bool = true ) -> Self {
45
45
assertNotEmptyString ( forId)
46
- info. uuid = forId
47
- info. override = override
48
- info. includeExecutingJob = includeExecutingJob
46
+ info. constraints. append ( UniqueUUIDConstraint ( uuid: forId, override: override, includeExecutingJob: includeExecutingJob) )
49
47
return self
50
48
}
51
49
@@ -62,14 +60,14 @@ public final class JobBuilder {
62
60
/// Otherwise it will wait for the remaining time
63
61
public func delay( time: TimeInterval ) -> Self {
64
62
assert ( time >= 0 )
65
- info. delay = time
63
+ info. constraints . append ( DelayConstraint ( delay: time) )
66
64
return self
67
65
}
68
66
69
67
/// If the job hasn't run after the date, It will be removed
70
68
/// will call onRemove(SwiftQueueError.deadline)
71
69
public func deadline( date: Date ) -> Self {
72
- info. deadline = date
70
+ info. constraints . append ( DeadlineConstraint ( deadline: date) )
73
71
return self
74
72
}
75
73
@@ -80,47 +78,52 @@ public final class JobBuilder {
80
78
public func periodic( limit: Limit = . unlimited, interval: TimeInterval = 0 ) -> Self {
81
79
assert ( limit. validate)
82
80
assert ( interval >= 0 )
83
- info. maxRun = limit
84
- info. interval = interval
85
- info. executor = . foreground
81
+ info. constraints. append ( RepeatConstraint ( maxRun: limit, interval: interval, executor: . foreground) )
86
82
return self
87
83
}
88
84
89
85
@available ( iOS 13 . 0 , tvOS 13 . 0 , macOS 10 . 15 , * )
90
86
public func periodic( limit: Limit = . unlimited, interval: TimeInterval = 0 , executor: Executor = . foreground) -> Self {
91
87
assert ( limit. validate)
92
88
assert ( interval >= 0 )
93
- info. maxRun = limit
94
- info. interval = interval
95
- info. executor = executor
89
+ info. constraints. append ( RepeatConstraint ( maxRun: limit, interval: interval, executor: executor) )
96
90
return self
97
91
}
98
92
99
93
/// Connectivity constraint.
100
94
public func internet( atLeast: NetworkType ) -> Self {
101
95
assert ( atLeast != . any)
102
- info. requireNetwork = atLeast
96
+ info. constraints . append ( NetworkConstraint ( networkType : atLeast) )
103
97
return self
104
98
}
105
99
100
+ private var requirePersist = false
101
+
106
102
/// Job should be persisted.
107
103
public func persist( ) -> Self {
108
- info . isPersisted = true
104
+ requirePersist = true
109
105
return self
110
106
}
111
107
112
108
/// Limit number of retry. Overall for the lifecycle of the SwiftQueueManager.
113
109
/// For a periodic job, the retry count will not be reset at each period.
114
110
public func retry( limit: Limit ) -> Self {
115
111
assert ( limit. validate)
116
- info. retries = limit
112
+ info. constraints . append ( JobRetryConstraint ( limit : limit) )
117
113
return self
118
114
}
119
115
120
116
/// Custom tag to mark the job
121
117
public func addTag( tag: String ) -> Self {
122
- assertNotEmptyString ( tag)
123
- info. tags. insert ( tag)
118
+ if let constraint: TagConstraint = getConstraint ( info. constraints) {
119
+ constraint. insert ( tag: tag)
120
+ return self
121
+ }
122
+
123
+ var set = Set < String > ( )
124
+ set. insert ( tag)
125
+
126
+ info. constraints. append ( TagConstraint ( tags: set) )
124
127
return self
125
128
}
126
129
@@ -142,15 +145,15 @@ public final class JobBuilder {
142
145
return self
143
146
}
144
147
145
- /// Call if the job can only run when the device is charging
148
+ /// Call if job can only run when the device is charging
146
149
public func requireCharging( ) -> Self {
147
- info. requireCharging = true
150
+ info. constraints . append ( BatteryChargingConstraint ( ) )
148
151
return self
149
152
}
150
153
151
154
/// Maximum time in second that the job is allowed to run
152
155
public func timeout( value: TimeInterval ) -> Self {
153
- info. timeout = value
156
+ info. constraints . append ( TimeoutConstraint ( timeout: value) )
154
157
return self
155
158
}
156
159
@@ -166,11 +169,16 @@ public final class JobBuilder {
166
169
167
170
/// Add job to the JobQueue
168
171
public func schedule( manager: SwiftQueueManager ) {
169
- if info. isPersisted {
170
- // Check if we will be able to serialize args
172
+ if requirePersist {
173
+ let constraint : UniqueUUIDConstraint ? = getConstraint ( info)
174
+ if constraint == nil {
175
+ info. constraints. append ( UniqueUUIDConstraint ( uuid: UUID ( ) . uuidString, override: false , includeExecutingJob: false ) )
176
+ }
171
177
assert ( JSONSerialization . isValidJSONObject ( info. params) )
178
+ info. constraints. append ( PersisterConstraint ( serializer: manager. params. serializer, persister: manager. params. persister) )
172
179
}
173
180
174
181
manager. enqueue ( info: info)
175
182
}
183
+
176
184
}
0 commit comments