-
Notifications
You must be signed in to change notification settings - Fork 3
/
SwiftNotes.swift
49 lines (41 loc) · 1.87 KB
/
SwiftNotes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// SwiftNotes.swift
//
// Created by Matt Greenfield on 25/11/17.
// Copyright © 2017 Big Paua.
//
import Foundation
/** SENDER AGNOSTIC WRAPPERS **/
// run the closure on the sender's thread
@discardableResult public func when(_ event: Notification.Name,
do closure: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol {
return NotificationCenter.default.addObserver(forName: event, object: nil, queue: nil) { note in
closure(note)
}
}
// run the closure on an explicit queue
@discardableResult public func when(_ event: Notification.Name, doOn queue: OperationQueue,
do closure: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol {
return NotificationCenter.default.addObserver(forName: event, object: nil, queue: queue) { note in
closure(note)
}
}
/** SENDER EXPLICIT WRAPPERS **/
// run the closure on the sender's thread
@discardableResult public func when(_ sender: Any, does event: Notification.Name,
do closure: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol {
return NotificationCenter.default.addObserver(forName: event, object: sender, queue: nil) { note in
closure(note)
}
}
// run the closure on an explicit queue
@discardableResult public func when(_ sender: Any, does event: Notification.Name, doOn queue: OperationQueue,
do closure: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol {
return NotificationCenter.default.addObserver(forName: event, object: sender, queue: queue) { note in
closure(note)
}
}
// post an event
public func trigger(_ event: Notification.Name, on object: Any? = nil, userInfo: [AnyHashable : Any]? = nil) {
NotificationCenter.default.post(Notification(name: event, object: object, userInfo: userInfo))
}