Giter Site home page Giter Site logo

pubsubswift's Introduction

PubSubSwift

CI Status Version License Platform

A type-safe implementation Swift of PubSub/event hub/event bus that can be used as a replacement for the NotificationCenter.

Usage

The easiest way is to just use the singleton instance, but you can always instantiate your own using PubSub():

let pubSub = PubSub.shared
// or
let pubSub = PubSub()

Events can be defined as needed:

enum Reachability: EventType {
    case notReachable
    case reachableOnWifi
    case reachableOnCellular
}

struct ActivationCode: EventType {
    let code: String
}

And can be posted as such:

pubSub.publish(ActivationCode(code: "123456"))

Subscribing to an event with an observer


pubSub.subscribe(self) { (event: Reachability) in
    // handle reachability change
}

// remove the Reachability subscription
pubSub.unsubscribe(self, eventType: Reachability.self)

// remove all of our subscriptions
pubSub.unsubscribe(self)

// you can also obtain a receipt when subscribing
let receipt = pubSub.subscribe(self) { (event: Reachability) in
    // handle reachability change
}

// remove the subscription based on the receipt:
pubSub.unsubscribe(with: receipt)

Subscribing to an event using a receipt

Due to the fact that pubSub.subscribe(_ observer: AnyObject) requires that the observer is a reference type, we need a different method to keep our subscription alive when we want to use it with value types. Therefore, we can make use of receipts by adding them as a member to the structs you want to become as observers.

Receipts can also be used to explicitly remove subscriptions when they are no longer needed.

struct Foo {
    private var receipt: Receipt

    init() {
        self.receipt = pubSub.subscribe { (event: Reachability) in
            // we will keep receiving events until the receipt is deallocated 
            // or it is used to cancel the subscription
        }
    }
}

In this case, the observation will be removed when this Foo instance is deallocated, which in turn deallocates receipt.

Subscribing on a specific queue

You can specify at subscription time the queue you want to receive your event on:

pubSub.subscribe(queue: .main) { (event: Reachability) in 
    // ...
}

If the receiver is subscribed on the main queue and the message is posted from the main queue, the subscriber will receive it synchronously, otherwise it will be dispatched asynchronously. If no queue is specified, the queue where the message is posted from will be used to deliver it synchronously.

Memory management

Subscribers will never be retained, but you must be careful not to cause retain cycles by strongly referencing self from a subscription block of a PubSub instance owned by the subscriber:

// Warning! Do not do this:
class Foo {
    var pubSub: PubSub = .shared
    
    func bar() {
        pubSub.subscribe(self) { (event: Reachability) in
            // this would cause a memory leak
            self.updateReachability(event)  
        }
    }
}

Requirements

None, there are no dependencies.

Installation

PubSub is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'PubSubSwift'

Author

Alin Radut

Credit where credit's due: this is a reimplementation of https://github.com/mishimay/EventHub.

License

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

pubsubswift's People

Contributors

alinradut avatar

Stargazers

 avatar

Watchers

 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.