-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19b54f0
commit 0fed237
Showing
4 changed files
with
189 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,125 @@ | ||
import Foundation | ||
|
||
final class ApertureNotification { | ||
static func notificationName(forEvent event: String, processId: String) -> String { | ||
"aperture.\(processId).\(event)" | ||
} | ||
static func notificationName(forEvent event: String, processId: String) -> String { | ||
"aperture.\(processId).\(event)" | ||
} | ||
|
||
private var notification: Notification | ||
var isAnswered = false | ||
private var notification: Notification | ||
var isAnswered = false | ||
|
||
init(_ notification: Notification) { | ||
self.notification = notification | ||
} | ||
init(_ notification: Notification) { | ||
self.notification = notification | ||
} | ||
|
||
func getField<T>(_ name: String) -> T? { | ||
notification.userInfo?[name] as? T | ||
} | ||
func getField<T>(_ name: String) -> T? { | ||
notification.userInfo?[name] as? T | ||
} | ||
|
||
var data: String? { getField("data") } | ||
var data: String? { getField("data") } | ||
|
||
func answer(_ data: Any? = nil) { | ||
isAnswered = true | ||
func answer(_ data: Any? = nil) { | ||
isAnswered = true | ||
|
||
guard | ||
let responseIdentifier: String = getField("responseIdentifier") | ||
guard | ||
let responseIdentifier: String = getField("responseIdentifier") | ||
else { | ||
return | ||
} | ||
|
||
var payload = [AnyHashable: Any]() | ||
|
||
if let payloadData = data { | ||
payload["data"] = "\(payloadData)" | ||
} | ||
|
||
DistributedNotificationCenter.default().postNotificationName( | ||
.init(responseIdentifier), | ||
object: nil, | ||
userInfo: payload, | ||
deliverImmediately: true | ||
) | ||
} | ||
return | ||
} | ||
|
||
var payload = [AnyHashable: Any]() | ||
|
||
if let payloadData = data { | ||
payload["data"] = "\(payloadData)" | ||
} | ||
|
||
DistributedNotificationCenter.default().postNotificationName( | ||
.init(responseIdentifier), | ||
object: nil, | ||
userInfo: payload, | ||
deliverImmediately: true | ||
) | ||
} | ||
} | ||
|
||
enum ApertureEvents { | ||
static func answerEvent( | ||
processId: String, | ||
event: String, | ||
using handler: @escaping (ApertureNotification) -> Void | ||
) -> NSObjectProtocol { | ||
static func answerEvent( | ||
processId: String, | ||
event: String, | ||
using handler: @escaping (ApertureNotification) -> Void | ||
) -> NSObjectProtocol { | ||
DistributedNotificationCenter.default().addObserver( | ||
forName: .init(ApertureNotification.notificationName(forEvent: event, processId: processId)), | ||
object: nil, | ||
queue: nil | ||
) { notification in | ||
let apertureNotification = ApertureNotification(notification) | ||
handler(apertureNotification) | ||
|
||
if !apertureNotification.isAnswered { | ||
apertureNotification.answer() | ||
} | ||
forName: .init(ApertureNotification.notificationName(forEvent: event, processId: processId)), | ||
object: nil, | ||
queue: nil | ||
) { notification in | ||
let apertureNotification = ApertureNotification(notification) | ||
handler(apertureNotification) | ||
|
||
if !apertureNotification.isAnswered { | ||
apertureNotification.answer() | ||
} | ||
} | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
data: Any?, | ||
using callback: @escaping (ApertureNotification) -> Void | ||
) { | ||
let notificationName = ApertureNotification.notificationName(forEvent: event, processId: processId) | ||
let responseIdentifier = "\(notificationName).response.\(UUID().uuidString)" | ||
|
||
var payload: [AnyHashable: Any] = ["responseIdentifier": responseIdentifier] | ||
|
||
if let payloadData = data { | ||
payload["data"] = "\(payloadData)" | ||
} | ||
|
||
var observer: AnyObject? | ||
|
||
observer = DistributedNotificationCenter.default().addObserver( | ||
forName: .init(responseIdentifier), | ||
object: nil, | ||
queue: nil | ||
) { notification in | ||
DistributedNotificationCenter.default().removeObserver(observer!) | ||
callback(ApertureNotification(notification)) | ||
} | ||
|
||
DistributedNotificationCenter.default().postNotificationName( | ||
.init( | ||
ApertureNotification.notificationName(forEvent: event, processId: processId) | ||
), | ||
object: nil, | ||
userInfo: payload, | ||
deliverImmediately: true | ||
) | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
using callback: @escaping (ApertureNotification) -> Void | ||
) { | ||
sendEvent( | ||
processId: processId, | ||
event: event, | ||
data: nil, | ||
using: callback | ||
) | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
data: Any? = nil | ||
) { | ||
sendEvent( | ||
processId: processId, | ||
event: event, | ||
data: data | ||
) { _ in } | ||
} | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
data: Any?, | ||
using callback: @escaping (ApertureNotification) -> Void | ||
) { | ||
let notificationName = ApertureNotification.notificationName(forEvent: event, processId: processId) | ||
let responseIdentifier = "\(notificationName).response.\(UUID().uuidString)" | ||
|
||
var payload: [AnyHashable: Any] = ["responseIdentifier": responseIdentifier] | ||
|
||
if let payloadData = data { | ||
payload["data"] = "\(payloadData)" | ||
} | ||
|
||
var observer: AnyObject? | ||
|
||
observer = DistributedNotificationCenter.default().addObserver( | ||
forName: .init(responseIdentifier), | ||
object: nil, | ||
queue: nil | ||
) { notification in | ||
DistributedNotificationCenter.default().removeObserver(observer!) | ||
callback(ApertureNotification(notification)) | ||
} | ||
|
||
DistributedNotificationCenter.default().postNotificationName( | ||
.init( | ||
ApertureNotification.notificationName(forEvent: event, processId: processId) | ||
), | ||
object: nil, | ||
userInfo: payload, | ||
deliverImmediately: true | ||
) | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
using callback: @escaping (ApertureNotification) -> Void | ||
) { | ||
sendEvent( | ||
processId: processId, | ||
event: event, | ||
data: nil, | ||
using: callback | ||
) | ||
} | ||
|
||
static func sendEvent( | ||
processId: String, | ||
event: String, | ||
data: Any? = nil | ||
) { | ||
sendEvent( | ||
processId: processId, | ||
event: event, | ||
data: data | ||
) { _ in } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.