Skip to content

Commit a4fe17e

Browse files
authored
Merge pull request #6 from p-x9/feature/post-with-option
support `CFNotificationCenterPostNotificationWithOptions`
2 parents 3614702 + 61040a3 commit a4fe17e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sources/SCFNotification/SCFNotificationCenter.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public class SCFNotificationCenter {
5858
userInfo: userInfo,
5959
deliverImmediately: deliverImmediately)
6060
}
61+
62+
public func postNotification(name: CFNotificationName,
63+
object: AnyObject? = nil,
64+
userInfo: CFDictionary,
65+
options: Set<Option>) {
66+
Self.postNotification(center: center,
67+
name: name,
68+
object: object,
69+
userInfo: userInfo,
70+
options: options)
71+
}
6172
}
6273

6374

@@ -131,6 +142,23 @@ extension SCFNotificationCenter {
131142

132143
CFNotificationCenterPostNotification(center.cfNotificationCenter, name, objectPtr, userInfo, deliverImmediately)
133144
}
145+
146+
public static func postNotification(center: CenterType,
147+
name: CFNotificationName,
148+
object: AnyObject? = nil,
149+
userInfo: CFDictionary,
150+
options: Set<Option>) {
151+
var objectPtr: UnsafeRawPointer?
152+
if let object {
153+
objectPtr = unsafeBitCast(object, to: UnsafeRawPointer.self)
154+
}
155+
156+
let options: CFOptionFlags = options.reduce(into: 0) { partialResult, option in
157+
partialResult = partialResult | option.flag
158+
}
159+
160+
CFNotificationCenterPostNotificationWithOptions(center.cfNotificationCenter, name, objectPtr, userInfo, options)
161+
}
134162
}
135163

136164
extension SCFNotificationCenter {
@@ -155,3 +183,17 @@ extension SCFNotificationCenter {
155183
}
156184
}
157185
}
186+
187+
extension SCFNotificationCenter {
188+
public enum Option: CaseIterable {
189+
case deliverImmediately
190+
case postToAllSessions
191+
192+
var flag: CFOptionFlags {
193+
switch self {
194+
case .deliverImmediately: return kCFNotificationDeliverImmediately
195+
case .postToAllSessions: return kCFNotificationPostToAllSessions
196+
}
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)