Swiftpack.co -  grype/SwiftAnnouncements as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
grype/SwiftAnnouncements
Thread-safe and block-based alternative to NSNotifications
.package(url: "https://github.com/grype/SwiftAnnouncements.git", from: "1.0.0")

Announcements

CI

Announcements is an alternative implementation of the Observer pattern, similar to NSNotificationCenter but with a few key differences:

  • Subscription is block based
  • Any type of object can be announced
  • Thread-safe and thread-agnostic - announcements are delivered in the same thread they are announced
  • Handling of announcements does not require a run loop

Example:

// Any type can be made Announceable
extension String : Announceable { }

let announcer = Announcer()

// Observers subscribe to announcements by type
let subscription = announcer.when(String.self) { (aString, anAnnouncer) in
    print("World says: \(aString)")
}

// Notify observers by making an announcement
announcer.announce("Drink Water!")

// Stop observing by removing the subscription object
announcer.remove(subscription: subscription)

// Alternatively, use an arbitrary subscriber object for managing subscriptions:
announcer.when(String.self, subscriber:self) { (aString, anAnnouncer) in
    print("Received announcement: \(aString)")
}
announcer.ubsubscribe(self)

Threading

Behind the scenes an Announcer keeps a Registry of all Subscriptions. Access to that registry is governed by a Read-Write lock, making operations thread-safe such that no changes to the registry will take place until all announcements have been processed, and no announcements will be processed while registry contents are being modified. Handling of announcements can happen concurrently. See Tests for details...

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API