macOS Linux Apache 2

Causality

A very simple event bus for Swift. Events may have associated data and are fully typed.

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)