目录
目录README.md

Announcements

CI

An event dispatch mechanism for broadcasting of information to registered observers. It is similar to NSNotificationCenter, but with a few notable differences:

  • Any type of value can be announced
  • Subscription is block-based
  • Delivery is thread-agnostic - announcements are delivered in the same thread they are made
  • Processing announcements does not require a run loop

Example:

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

let announcer = Announcer()

// Subscribe to observe values of an Announceable type
let subscription = announcer.when(String.self) { (aString, anAnnouncer) in
    print("World says: \(aString)")
}

// Notify observers by announcing an Announceable value
announcer.announce("Drink Water!")

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

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

// Unsubscribe when no longer interested
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…

关于
62.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号