Browse Source

Initial commit is final commit

XCBOSA - ITX 3 years ago
parent
commit
3cc9faa8ca
1 changed files with 61 additions and 2 deletions
  1. 61 2
      Sources/XCNotificationCenter/XCNotificationCenter.swift

+ 61 - 2
Sources/XCNotificationCenter/XCNotificationCenter.swift

@@ -1,6 +1,65 @@
-public struct XCNotificationCenter {
-    public private(set) var text = "Hello, World!"
+import Darwin
+import UIKit
 
+public class XCNotificationCenter {
+    
+    public static var shared = XCNotificationCenter()
+    
+    private var receivers: [String : [Receiver]]
+    private var locker: pthread_rwlock_t
+    
+    public class Receiver {
+        var function: ((Any?) -> Void)?
+        var notificationName: String
+        weak var recordedWeakObject: AnyObject?
+        
+        public init(receiver function: ((Any?) -> Void)?, recordedWeakObject: AnyObject?, forNotificationNamed name: String) {
+            self.function = function
+            self.notificationName = name
+            self.recordedWeakObject = recordedWeakObject
+        }
+    }
+    
     public init() {
+        self.receivers = [String : [Receiver]]()
+        self.locker = pthread_rwlock_t()
+        pthread_rwlock_init(&self.locker, nil)
+    }
+    
+    public func addObserver(_ weakObj: AnyObject?, forName name: String, executionQueue queue: DispatchQueue = .main, andAction action: ((Any?) -> Void)?) {
+        let method = {
+            val in
+            queue.async {
+                action?(val)
+            }
+        }
+        let receiver = Receiver(receiver: method, recordedWeakObject: weakObj, forNotificationNamed: name)
+        pthread_rwlock_wrlock(&self.locker)
+        var t: [Receiver]! = receivers[name]
+        if t == nil {
+            t = [Receiver]()
+            receivers[name] = t
+        }
+        t.append(receiver)
+        pthread_rwlock_unlock(&self.locker)
+    }
+    
+    public func post(notificationNamed name: String, attachedObject object: Any?) {
+        var ree = [Receiver]()
+        pthread_rwlock_rdlock(&self.locker)
+        if var arr = receivers[name] {
+            arr.removeAll(where: { $0.recordedWeakObject == nil })
+            ree.append(contentsOf: arr)
+        }
+        for it in ree {
+            it.function?(object)
+        }
+        pthread_rwlock_unlock(&self.locker)
+        
+    }
+    
+    deinit {
+        pthread_rwlock_destroy(&self.locker)
     }
+    
 }