项目作者: foxfriends

项目描述 :
Protected (strongly-typed) shipments (notifications)
高级语言: Swift
项目地址: git://github.com/foxfriends/convoy.git
创建时间: 2019-11-28T05:39:47Z
项目社区:https://github.com/foxfriends/convoy

开源协议:

下载


Convoy

Protected (strongly-typed) shipments (notifications). Convoy is a system that works very
much like the standard NotificationCenter, but in a strongly-typed manner.

Usage

  1. // First, define a Convoy, and its contents.
  2. enum MyConvoy: Convoy {
  3. typealias Contents = String
  4. }
  5. // Then, set up a receiver (to receive your Convoys)
  6. var subscription: ConvoyHandle?
  7. func receiveMyConvoy() {
  8. // Unlike NotificationCenter, we use the type of our Convoy to identify our events.
  9. //
  10. // `ConvoyDispatcher.receive` returns a handle which we should clean up later. This is
  11. // similar to the object returned from `NotificationCenter.addObserver`, which you must
  12. // later pass to `NotificationCenter.removeObserver`.
  13. subscription = ConvoyDispatcher.default.receive(MyConvoy.self) { (contents: String) in
  14. // all we receive here is the payload, so let's just print it out!
  15. print(contents)
  16. }
  17. }
  18. // We can then dispatch Convoys to the ConvoyDispatcher, and they will be sent to all
  19. // the receivers
  20. func sendMyConvoy(contents: String) {
  21. ConvoyDispatcher.default.dispatch(MyConvoy.self, contents: contents)
  22. }
  23. // Finally, later on, we have to clean up our Receivers, or else we'll have memory leaks:
  24. func removeReceiver() {
  25. subscription.remove()
  26. }

Installation

Only Swift Package Manager is supported:

  1. Package(
  2. dependencies: [
  3. .package(url: "https://github.com/foxfriends/convoy.git", from: "1.0.0")
  4. ],
  5. targets: [
  6. .target(name: "MyProject", dependencies: ["Convoy"])
  7. ]
  8. )

RxSwift

Convoy has support for RxSwift style receivers as well, compatible with RxSwift version 5 and above.

  1. Package(
  2. dependencies: [
  3. .package(url: "https://github.com/foxfriends/convoy.git", from: "1.0.0")
  4. ],
  5. targets: [
  6. .target(name: "MyProject", dependencies: ["Convoy", "RxConvoy"])
  7. ]
  8. )

Usage

  1. let disposable = ConvoyDispatcher.default.rx.receive(MyConvoy.self)
  2. .subscribe(onNext: { (contents: String) in
  3. print(contents)
  4. })