Skip to content

Commit a19a8db

Browse files
authored
Clean up DarwinNotificationName & friends
1 parent 05b5aa0 commit a19a8db

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

SignalServiceKit/Util/DarwinNotificationCenter.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ public enum DarwinNotificationCenter {
2424
/// - Parameter observer: The token returned by ``addObserver(name:queue:block:)``
2525
/// - Returns: `true` iff the given `observer` is valid
2626
public static func isValid(_ observer: ObserverToken) -> Bool {
27-
notify_is_valid_token(observer)
27+
return notify_is_valid_token(observer)
2828
}
2929

3030
/// Post a darwin notification that can be listened for from other processes.
3131
///
3232
/// - Parameter name: The name of the notification to post.
3333
public static func postNotification(name: DarwinNotificationName) {
34-
owsAssertDebug(name.isValid)
35-
name.withCString { cName in
36-
_ = notify_post(cName)
37-
}
34+
_ = notify_post(name.rawValue)
3835
}
3936

4037
/// Add an observer for a darwin notification of the given name.
@@ -45,12 +42,8 @@ public enum DarwinNotificationCenter {
4542
/// removing the observer after receipt.
4643
/// - Returns: An ``ObserverToken`` that can be used to remove this observer.
4744
public static func addObserver(name: DarwinNotificationName, queue: DispatchQueue, block: @escaping (ObserverToken) -> Void) -> ObserverToken {
48-
owsAssertDebug(name.isValid)
49-
5045
var observer = Self.invalidObserverToken
51-
name.withCString { cName in
52-
_ = notify_register_dispatch(cName, &observer, queue, block)
53-
}
46+
_ = notify_register_dispatch(name.rawValue, &observer, queue, block)
5447
return observer
5548
}
5649

@@ -63,8 +56,7 @@ public enum DarwinNotificationCenter {
6356
owsFailDebug("Invalid observer token.")
6457
return
6558
}
66-
67-
notify_cancel(observer)
59+
_ = notify_cancel(observer)
6860
}
6961

7062
/// Retrieves the state for a given observer. This value can be set and read from
@@ -80,7 +72,7 @@ public enum DarwinNotificationCenter {
8072
}
8173

8274
var result: UInt64 = 0
83-
notify_get_state(observer, &result)
75+
_ = notify_get_state(observer, &result)
8476
return result
8577
}
8678
}

SignalServiceKit/Util/DarwinNotificationName.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@ public struct DarwinNotificationName: ExpressibleByStringLiteral {
99
public static let nseDidReceiveNotification: DarwinNotificationName = "org.signal.nseDidReceiveNotification"
1010
public static let mainAppHandledNotification: DarwinNotificationName = "org.signal.mainAppHandledNotification"
1111
public static let mainAppLaunched: DarwinNotificationName = "org.signal.mainAppLaunched"
12-
public static let primaryDBFolderNameDidChange: DarwinNotificationName = "org.signal.primaryDBFolderNameDidChange"
12+
static let primaryDBFolderNameDidChange: DarwinNotificationName = "org.signal.primaryDBFolderNameDidChange"
1313

14-
public static func sdsCrossProcess(for type: AppContextType) -> DarwinNotificationName {
14+
static func sdsCrossProcess(for type: AppContextType) -> DarwinNotificationName {
1515
DarwinNotificationName("org.signal.sdscrossprocess.\(type)")
1616
}
1717

1818
public typealias StringLiteralType = String
1919

20-
private let stringValue: String
20+
public let rawValue: String
2121

22-
public var isValid: Bool { !stringValue.isEmpty }
23-
24-
public init(stringLiteral value: String) {
25-
stringValue = value
22+
public init(stringLiteral name: String) {
23+
owsPrecondition(!name.isEmpty)
24+
self.rawValue = name
2625
}
2726

2827
public init(_ name: String) {
29-
stringValue = name
30-
}
31-
32-
public func withCString<T>(_ body: (UnsafePointer<CChar>) throws -> T) rethrows -> T {
33-
try stringValue.withCString(body)
28+
owsAssertDebug(!name.isEmpty)
29+
self.rawValue = name
3430
}
3531
}

0 commit comments

Comments
 (0)