Causality
A very simple event bus for Swift. Events may have associated data and are fully typed. All publish/subscribe methods are thread-safe.
Installation
Swift Package Manager
Add the Causality package to the dependencies within your application’s Package.swift file. Substitute “x.y.z” with the latest Causality release.
.package(url: "https://github.com/dannys42/Causality.git", from: "x.y.z")
Add Causality to your target’s dependencies:
.target(name: "example", dependencies: ["Causality"]),
Usage
Just an event (no data)
The simplest event to manage has no associated data.
Declare Events
This declares an event called aTriggerEvent that has no associated data.
struct MyEvents {
static let aTriggerEvent = Causality.Event<NoSimpleEventMessage>(name: "A Trigger")
}
Subscribe to events
To subscribe to this event:
let subscription = Causality.bus.subscribe(MyEvents.aTriggerEvent) { _ in
print("Event happened")
}
Publish events
To publish/post an event of this type:
Causality.bus.publish(MyEvents.aTriggerEvent)
An event with associated data
Events can include data of any type (referred to as a “message”). The event label is fully type specified with the message. So a subscriber will have a fully typed message available to its handler.
Define the Message
A message can be a standard Swift type like Int, String, etc. Or it can be a struct or class. In this example, we’ll declare a struct:
struct InterestingMessage: Causality.Message {
let string: String
let number: Int
}
Declare the event
struct MyEvents {
static let interestingEvent = Causality.Event<InterestingMessage>(name: "An interesting Event")
}
Subscribing to the event
let subscription = Causality.bus.subscribe(MyEvents.interestingEvent) { message in
print("A message from interestingEvent: \(message)")
}
Publish events
To publish/post an event of this type:
Causality.bus.publish(MyEvents.interestingEvent,
message: InterestingMessage(string: "Hello", number: 42))
To unsubsrcibe from an event
Causality.bus.unsubscribe(subscriptionId)
API Documentation
For more information visit our API reference.
Related Projects
License
This library is licensed under Apache 2.0. The full license text is available in LICENSE.
Reference