|
| 1 | +// |
| 2 | +// Notice.swift |
| 3 | +// NoticeObserveKit |
| 4 | +// |
| 5 | +// Created by marty-suzuki on 2019/01/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +// MARK: - Notice definition |
| 11 | + |
| 12 | +public enum Notice { |
| 13 | + |
| 14 | + public final class Center { |
| 15 | + |
| 16 | + public static let `default` = Center(center: .default) |
| 17 | + |
| 18 | + let center: NotificationCenter |
| 19 | + |
| 20 | + init(center: NotificationCenter) { |
| 21 | + self.center = center |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public class Names { |
| 26 | + fileprivate init() {} |
| 27 | + } |
| 28 | + |
| 29 | + public final class Name<Value>: Names { |
| 30 | + |
| 31 | + let raw: Notification.Name |
| 32 | + |
| 33 | + public convenience init(name: String) { |
| 34 | + self.init(Notification.Name(name)) |
| 35 | + } |
| 36 | + |
| 37 | + public init(_ name: Notification.Name) { |
| 38 | + self.raw = name |
| 39 | + super.init() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public final class Observer { |
| 44 | + |
| 45 | + private(set) weak var center: NotificationCenter? |
| 46 | + let raw: NSObjectProtocol |
| 47 | + |
| 48 | + init(center: NotificationCenter, raw: NSObjectProtocol) { |
| 49 | + self.center = center |
| 50 | + self.raw = raw |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public class ObserverPool { |
| 55 | + private(set) var observers: [Observer] = [] |
| 56 | + private(set) var mutex: pthread_mutex_t = pthread_mutex_t() |
| 57 | + |
| 58 | + public init() { |
| 59 | + pthread_mutex_init(&mutex, nil) |
| 60 | + } |
| 61 | + |
| 62 | + deinit { |
| 63 | + pthread_mutex_lock(&mutex) |
| 64 | + observers.forEach { |
| 65 | + $0.invalidate() |
| 66 | + } |
| 67 | + observers.removeAll() |
| 68 | + pthread_mutex_unlock(&mutex) |
| 69 | + pthread_mutex_destroy(&mutex) |
| 70 | + } |
| 71 | + |
| 72 | + func adding(_ observer: Observer) { |
| 73 | + pthread_mutex_lock(&mutex) |
| 74 | + observers = Array([observers, [observer]].joined()) |
| 75 | + pthread_mutex_unlock(&mutex) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// MARK: - Notice.Center (NoticeUserInfoDecodable) |
| 81 | + |
| 82 | +extension Notice.Center { |
| 83 | + @available(iOS, unavailable) |
| 84 | + public func post<Value: NoticeUserInfoDecodable>(name: Notice.Name<Value>, with value: Value, from object: Any? = nil) {} |
| 85 | + |
| 86 | + public func observe<Value: NoticeUserInfoDecodable>(name: Notice.Name<Value>, |
| 87 | + object: Any? = nil, |
| 88 | + queue: OperationQueue? = nil, |
| 89 | + using: @escaping (Value) -> Void) -> Notice.Observer { |
| 90 | + let observer = center.addObserver(forName: name.raw, object: object, queue: queue) { notification in |
| 91 | + guard |
| 92 | + let userInfo = notification.userInfo, |
| 93 | + let value = Value(info: userInfo) |
| 94 | + else { |
| 95 | + return |
| 96 | + } |
| 97 | + using(value) |
| 98 | + } |
| 99 | + return Notice.Observer(center: center, raw: observer) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// MARK: - Notice.Center |
| 104 | + |
| 105 | +extension Notice.Center { |
| 106 | + |
| 107 | + enum Const { |
| 108 | + static let infoKey = "noice-info-key" |
| 109 | + } |
| 110 | + |
| 111 | + public func post<Value>(name: Notice.Name<Value>, with value: Value, from object: Any? = nil) { |
| 112 | + let userInfo: [AnyHashable : Any] = [Const.infoKey : value] |
| 113 | + center.post(name: name.raw, object: object, userInfo: userInfo) |
| 114 | + } |
| 115 | + |
| 116 | + public func observe<Value>(name: Notice.Name<Value>, |
| 117 | + object: Any? = nil, |
| 118 | + queue: OperationQueue? = nil, |
| 119 | + using: @escaping (Value) -> Void) -> Notice.Observer { |
| 120 | + let observer = center.addObserver(forName: name.raw, object: object, queue: queue) { notification in |
| 121 | + guard let value = notification.userInfo?[Const.infoKey] as? Value else { |
| 122 | + return |
| 123 | + } |
| 124 | + using(value) |
| 125 | + } |
| 126 | + return Notice.Observer(center: center, raw: observer) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// MARK: - Notice.Observer |
| 131 | + |
| 132 | +extension Notice.Observer { |
| 133 | + public func invalidate() { |
| 134 | + center?.removeObserver(raw) |
| 135 | + } |
| 136 | + |
| 137 | + public func invalidated(by pool: Notice.ObserverPool) { |
| 138 | + pool.adding(self) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// MARK: - NotificationCenter extension |
| 143 | + |
| 144 | +extension Notice { |
| 145 | + public struct Extension { |
| 146 | + let base: Notice.Center |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension NotificationCenter { |
| 151 | + public var nok: Notice.Extension { |
| 152 | + return Notice.Extension(base: Notice.Center(center: self)) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +extension Notice.Extension { |
| 157 | + public func post<Value>(name: Notice.Name<Value>, with value: Value, from object: Any? = nil) { |
| 158 | + base.post(name: name, with: value, from: object) |
| 159 | + } |
| 160 | + |
| 161 | + public func observe<Value>(name: Notice.Name<Value>, |
| 162 | + object: Any? = nil, |
| 163 | + queue: OperationQueue? = nil, |
| 164 | + using: @escaping (Value) -> Void) -> Notice.Observer { |
| 165 | + return base.observe(name: name, |
| 166 | + object: object, |
| 167 | + queue: queue, using: using) |
| 168 | + } |
| 169 | + |
| 170 | + @available(iOS, unavailable) |
| 171 | + public func post<Value: NoticeUserInfoDecodable>(name: Notice.Name<Value>, with value: Value, from object: Any? = nil) {} |
| 172 | + |
| 173 | + public func observe<Value: NoticeUserInfoDecodable>(name: Notice.Name<Value>, |
| 174 | + object: Any? = nil, |
| 175 | + queue: OperationQueue? = nil, |
| 176 | + using: @escaping (Value) -> Void) -> Notice.Observer { |
| 177 | + return base.observe(name: name, |
| 178 | + object: object, |
| 179 | + queue: queue, |
| 180 | + using: using) |
| 181 | + } |
| 182 | +} |
0 commit comments