@@ -39,7 +39,7 @@ public final class Footprint: @unchecked Sendable {
39
39
public struct Memory {
40
40
41
41
/// State describes how close to app termination your app is based on memory.
42
- public enum State : Comparable , CaseIterable {
42
+ public enum State : Int , Comparable , CaseIterable {
43
43
44
44
/// Everything is good, no need to worry.
45
45
case normal
@@ -57,7 +57,10 @@ public final class Footprint: @unchecked Sendable {
57
57
/// memory usage behavior.
58
58
/// Please revisit memory best practices and profile your app.
59
59
case terminal
60
-
60
+
61
+ public static func < ( lhs: Self , rhs: Self ) -> Bool {
62
+ lhs. rawValue < rhs. rawValue
63
+ }
61
64
/// Init from String value
62
65
public init ? ( _ value: String ) {
63
66
for c in Self . allCases {
@@ -153,9 +156,7 @@ public final class Footprint: @unchecked Sendable {
153
156
154
157
/// Returns a copy of the current memory structure.
155
158
public var memory : Memory {
156
- _memoryLock. lock ( )
157
- defer { _memoryLock. unlock ( ) }
158
- return _memory
159
+ _memoryLock. withLock { _memory }
159
160
}
160
161
161
162
/// Based on the current memory footprint, tells you if you should be able to allocate
@@ -170,16 +171,12 @@ public final class Footprint: @unchecked Sendable {
170
171
171
172
/// The currently tracked memory state.
172
173
public var state : Memory . State {
173
- _memoryLock. lock ( )
174
- defer { _memoryLock. unlock ( ) }
175
- return _memory. state
174
+ _memoryLock. withLock { _memory. state }
176
175
}
177
176
178
177
/// The currently tracked memory pressure.
179
178
public var pressure : Memory . State {
180
- _memoryLock. lock ( )
181
- defer { _memoryLock. unlock ( ) }
182
- return _memory. pressure
179
+ _memoryLock. withLock { _memory. pressure }
183
180
}
184
181
185
182
private init ( _ provider: MemoryProvider = DefaultMemoryProvider ( ) ) {
@@ -240,12 +237,11 @@ public final class Footprint: @unchecked Sendable {
240
237
return . normal
241
238
}
242
239
243
- internal func observe( _ action: @escaping ( Memory ) -> Void ) {
244
- _memoryLock. lock ( )
245
- defer { _memoryLock. unlock ( ) }
246
- _observers. append ( action)
247
- let mem = _memory
248
-
240
+ public func observe( _ action: @escaping ( Memory ) -> Void ) {
241
+ let mem = _memoryLock. withLock {
242
+ _observers. append ( action)
243
+ return _memory
244
+ }
249
245
DispatchQueue . global ( ) . async {
250
246
action ( mem)
251
247
}
@@ -269,7 +265,7 @@ public final class Footprint: @unchecked Sendable {
269
265
}
270
266
// memory used changes only on ~1MB intevals
271
267
// that's enough precision
272
- if _memory. used - memory. used > 1000 {
268
+ if abs ( _memory. used - memory. used) > 1000000 {
273
269
changeSet. insert ( . footprint)
274
270
}
275
271
guard !changeSet. isEmpty else {
@@ -317,9 +313,7 @@ public final class Footprint: @unchecked Sendable {
317
313
if changeSet. contains ( . footprint) {
318
314
// copy behind the lock
319
315
// deploy outside the lock
320
- _memoryLock. lock ( )
321
- let observers = _observers
322
- _memoryLock. unlock ( )
316
+ let observers = _memoryLock. withLock { _observers }
323
317
observers. forEach { $0 ( memory) }
324
318
}
325
319
}
0 commit comments