Git Product home page Git Product logo

swen's Introduction

Swen - An Event Bus written in Swift

Build Status codecov Badge w/ Version GitHub license

  • Typesafe events
  • Pass custom objects
  • Threadsafe
  • Sticky events
  • Fast and small

Installation

Swen is available through CocoaPods, Carthage and Swift Package Manager.

CocoaPods

To install it using CocoaPods, simply add the following line to your Podfile:

pod "Swen"

Carthage

To install it via Carthage, add the following line to your Cartfile and follow the instructions to adding frameworks to an application:

github "e-Sixt/Swen"

Swift Package Manager

To install it using the Swift Package Manager, either directly add it to your project using Xcode 11, or specify it as dependency in the Package.swift file:

// ...
dependencies: [
    .package(url: "https://github.com/e-Sixt/Swen.git", from: "2.0.0"),
],
//...

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 8.1+
  • Swift 3.0+

Usage

import Swen

struct TestEvent: Event {
    let name: String
}

class TestViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //register for incoming events
        Swen<TestEvent>.register(self) { event in
            print(event.name)
        }
        //post event
        Swen.post(TestEvent(name: "Sixt"))
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        //unregister from events
        Swen<TestEvent>.unregister(self)
    }
}

Register on different threads

// Registers on the main thread
Swen<Event>.register(_ observer: AnyObject, handler: (_ event: Event) -> Void)

// Registers on background queue
Swen<Event>.registerOnBackground(_ observer: AnyObject, handler: (_ event: Event) -> Void)

// Registers the closure on a specific queue
Swen<Event>.register(_ observer: AnyObject, onQueue queue: OperationQueue, handler: (_ event: Event) -> Void)

Sticky events

Sometimes events carry information that is not only important for that one moment but needs to be kept around longer. For these cases sticky events come in handy. They behave the same as normal events with two further additions. First you can query them by:

struct TestStickyEvent: StickyEvent {
    let name: String
}

print(Swen<TestStickyEvent>.sticky()?.name)

Important: The return sticky is optional, because the event may not be posted yet!

The second additions is that if you register for a sticky event and one was already posted before. It will immediately trigger the registered closure

class TestViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //post event
        Swen.post(TestStickyEvent(name: "Stick it"))

        //register for incoming events
        Swen<TestStickyEvent>.register(self) { event in
            print(event.name)
        }
    }
}

System Events

To use System Events, write wrapper for interest NSNotifications:

public struct SystemEvents {

    struct ApplicationWillEnterForeground: Event {
    }

    static func register(storage: SwenStorage = .defaultStorage) {

        let center = NotificationCenter.default

        center.addObserver(forName: .UIApplicationWillEnterForeground, object: nil, queue: nil) { _ in

            Swen.post(ApplicationWillEnterForeground(), in: storage)
        }
    }
}

Dependency Injection

To encapsulate test cases from production code and from each other, we suggest to use Dependency Injection and Storage mechanism:

class TestViewController: UIViewController {

    var swenStorage = SwenStorage()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //register for incoming events in custom storage
        Swen<TestEvent>.register(self, in: swenStorage) { event in
            print(event.name)
        }

        //register for incoming events in default storage
        Swen<TestEvent>.register(self) { event in
            print(event.name)
        }

        //post event in custom storage
        Swen.post(TestEvent(name: "Sixt, custom storage"), in: swenStorage)

        //post event in default storage
        Swen.post(TestEvent(name: "Sixt, default storage"))
    }
}

Performance

One of the other main benefits of using Swen is the significant performance increase over NSNotificationCenter

Performance Test NSNotificationCenter SwiftBus
Perform 10ˆ6 Events to 1 receiver 6.54s 2.61s
Perform 10ˆ3 Events to 10ˆ3 receivers 14.42s 11.15s

Author

Contributors:

License

Swen is available under the MIT license. See the LICENSE file for more info.

alt text

swen's People

Contributors

batschz avatar bhabani-sixt avatar dependabot[bot] avatar dmitrypoznukhov avatar ffittschen avatar franzbusch avatar sebastianborggrewe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.