@@ -33,37 +33,28 @@ public enum NetworkType: Int, Codable {
33
33
case wifi = 2
34
34
}
35
35
36
- internal final class NetworkConstraint : SimpleConstraint , CodableConstraint {
36
+ internal protocol NetworkMonitor {
37
37
38
- /// Require a certain connectivity type
39
- internal let networkType : NetworkType
38
+ func hasCorrectNetworkType( require: NetworkType ) -> Bool
40
39
41
- private var monitor : NWPathMonitor ?
40
+ func startMonitoring ( networkType : NetworkType , operation : SqOperation )
42
41
43
- required init ( networkType: NetworkType ) {
44
- assert ( networkType != . any)
45
- self . networkType = networkType
46
- }
42
+ }
47
43
48
- convenience init ? ( from decoder: Decoder ) throws {
49
- let container = try decoder. container ( keyedBy: NetworkConstraintKey . self)
50
- if container. contains ( . requireNetwork) {
51
- try self . init ( networkType: container. decode ( NetworkType . self, forKey: . requireNetwork) )
52
- } else { return nil }
53
- }
44
+ internal class NWPathMonitorNetworkMonitor : NetworkMonitor {
54
45
55
- override func willSchedule( queue: SqOperationQueue , operation: SqOperation ) throws {
56
- assert ( operation. dispatchQueue != . main)
57
- self . monitor = NWPathMonitor ( )
58
- }
59
-
60
- override func run( operation: SqOperation ) -> Bool {
61
- guard let monitor = monitor else { return true }
46
+ private let monitor = NWPathMonitor ( )
62
47
48
+ func hasCorrectNetworkType( require: NetworkType ) -> Bool {
63
49
if monitor. currentPath. status == . satisfied {
50
+ monitor. pathUpdateHandler = nil
64
51
return true
52
+ } else {
53
+ return false
65
54
}
55
+ }
66
56
57
+ func startMonitoring( networkType: NetworkType , operation: SqOperation ) {
67
58
monitor. pathUpdateHandler = { [ monitor, operation, networkType] path in
68
59
guard path. status == . satisfied else {
69
60
operation. logger. log ( . verbose, jobId: operation. name, message: " Unsatisfied network requirement " )
@@ -81,8 +72,47 @@ internal final class NetworkConstraint: SimpleConstraint, CodableConstraint {
81
72
monitor. pathUpdateHandler = nil
82
73
operation. run ( )
83
74
}
84
-
85
75
monitor. start ( queue: operation. dispatchQueue)
76
+ }
77
+
78
+
79
+ }
80
+
81
+
82
+ internal final class NetworkConstraint : SimpleConstraint , CodableConstraint {
83
+
84
+ /// Require a certain connectivity type
85
+ internal let networkType : NetworkType
86
+
87
+ private let monitor : NetworkMonitor
88
+
89
+ required init ( networkType: NetworkType , monitor: NetworkMonitor ) {
90
+ assert ( networkType != . any)
91
+ self . networkType = networkType
92
+ self . monitor = monitor
93
+ }
94
+
95
+ convenience init ( networkType: NetworkType ) {
96
+ self . init ( networkType: networkType, monitor: NWPathMonitorNetworkMonitor ( ) )
97
+ }
98
+
99
+ convenience init ? ( from decoder: Decoder ) throws {
100
+ let container = try decoder. container ( keyedBy: NetworkConstraintKey . self)
101
+ if container. contains ( . requireNetwork) {
102
+ try self . init ( networkType: container. decode ( NetworkType . self, forKey: . requireNetwork) )
103
+ } else { return nil }
104
+ }
105
+
106
+ override func willSchedule( queue: SqOperationQueue , operation: SqOperation ) throws {
107
+ assert ( operation. dispatchQueue != . main)
108
+ }
109
+
110
+ override func run( operation: SqOperation ) -> Bool {
111
+ if monitor. hasCorrectNetworkType ( require: networkType) {
112
+ return true
113
+ }
114
+
115
+ monitor. startMonitoring ( networkType: networkType, operation: operation)
86
116
return false
87
117
}
88
118
0 commit comments