Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Made Noted more swift3y
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Mygind authored and Jakob Mygind committed Sep 13, 2016
1 parent 13fbee6 commit d02bc48
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions Noted/Classes/Noted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class Noted {

public static let defaultInstance = Noted()


private let notedQueue = DispatchQueue(label: "com.nodes.noted", attributes: .concurrent)

private var _observers = NSHashTable<NoteObserverInfo>(options: .weakMemory)
Expand All @@ -51,24 +50,23 @@ public class Noted {
return _observers.allObjects.map {$0.receiver}
}


private init() {}

public func addObserver(_ observer: NoteObserver, filter: NoteFilter = PassthroughNoteFilter()) {
public func add(observer: NoteObserver, filter: NoteFilter = PassthroughNoteFilter()) {
notedQueue.async(group: nil, qos: .default, flags: .barrier) {
self._observers.add(NoteObserverInfo(receiver: observer, filter: filter))
}
}

public func removeObserver(_ observer: NoteObserver) {
public func remove(observer: NoteObserver) {
notedQueue.async(group: nil, qos: .default, flags: .barrier) {
if let foundEntry = (self._observers.allObjects).first(where: {$0.receiver === observer}) {
self._observers.remove(foundEntry)
}
}
}

public func postNote(_ note: NoteType) {
public func post(note: NoteType) {
notedQueue.async {
for receiver in self._observers.allObjects {
if !receiver.filter.shouldFilter(note:note) {
Expand Down
4 changes: 2 additions & 2 deletions NotedTests/ThreadSafeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class ThreadSafeTests: XCTestCase {
receiverStore.append(controller)
let queue = DispatchQueue(label:"com.noted.queue.\(index)")
queue.async {
Noted.defaultInstance.addObserver(controller)
Noted.defaultInstance.postNote(TestNotification.TestUpdated)
Noted.defaultInstance.add(observer: controller)
Noted.defaultInstance.post(note: TestNotification.TestUpdated)

}

Expand Down

0 comments on commit d02bc48

Please sign in to comment.