Skip to content

Generic and retention cycle free Event that enables instance of any type to notify multiple classes when something of interest occurs.

License

Notifications You must be signed in to change notification settings

khamitimur/AmazingEvent

Repository files navigation

AmazingEvent

AmazingEvent is a generic and retention cycle free Event that enables instance of any type to notify multiple classes when something of interest occurs.

In other words, it's an attempt to bring C# like Event to Swift and exploring what Swift version of Target-Action is like.

Usage

The type that sends an Event is called the publisher and the classes that handle an Event are called subscribers.

Publisher

import AmazingEvent

class Car {
    // It's better to keep an event publisher private if you don't want it to be invoked by some other class.
    private var didStartPublisher = AmazingEventPublisher<Date>()
    
    // On initialization AmazingEventPublisher creates assosiated AmazingEvent.
    // Your job is to make this assosiated event reachable without exposing an event publisher itself.
    private(set) lazy var didStart = didStartPublisher.event
    
    // ...
    
    func start() {
        // Invokes the event by passing `self` as a sender.
        didStartPublisher.invoke(sender: self, parameter: Date())
    }
}

Subscriber

import AmazingEvent

class CarTestBench {
    init(car: Car) {
        car.didStart.subscribe(target: self, handleAction: CarTestBench.carDidStart)
    }
    
    private func carDidStart(sender: AnyObject?, parameter: Date) {
        // ...
    }
}

Requirements

  • macOS 10.14+
  • iOS 11.0+
  • tvOS 11.0+
  • watchOS 4.0+
  • Xcode 11+
  • Swift 5.0+

Installation

CocoaPods

To integrate AmazingEvent into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'AmazingEvent', '~> 2.0'

Swift Package Manager

To integrate AmazingEvent into your Xcode project using Swift Package Manager, specify it as dependency in your Package.swift:

dependencies: [
    .package(url: "https://github.com/khamitimur/AmazingEvent.git", .upToNextMajor(from: "2.0.0"))
]

License

This project is licensed under the MIT License.

Acknowledgments

  • Inspired by article by Colin Eberhardt.

About

Generic and retention cycle free Event that enables instance of any type to notify multiple classes when something of interest occurs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published