ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - mcmatan/TopicEventBus: Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.
Publish–subscribe design pattern implementation framework, with an ability to publish events by topic. - mcmatan/TopicEventBus
Visit Site

GitHub - mcmatan/TopicEventBus: Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.

GitHub - mcmatan/TopicEventBus: Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.

TopicEventBus

Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alternative)

On Medium: https://medium.com/@matancohen_22770/why-you-should-stop-using-notificationcenter-and-start-using-topiceventbus-c4c7ab312643

Language

About:

TopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.

There are many other libraries out there, aiming to solve this issue, but none of them support publishing events by topic, in a type-safe way, with no magic strings.

Also, TopicEventBus holds weak referenced for Its observers, so you don't have to be afraid of memory leaks.

What is a topic?

The topic is for example, when you would like to publish "ConversationUpdateEvent" yet have the ability to publish that event only to listeners with conversation id "1234" or to all listeners.

Specifying the conversation Id is specifying a topic for that event.

Show me the code! and what's in it for me.

Let's build a chat app!

In this app, we are going to have multiple conversations screens, each one of them would like to know only about changes to Its conversation.

The first step, create an event for conversation update:

class ConversationChangedEvent: TopicEvent {
    let newTitle: String
    init(conversationId: String, newTitle: String) {
        self.newTitle = newTitle
        super.init()
        self.key = conversationId
    }
}

Every event must inherit from "TopicEvent," and in case it has a topic (Our example it will be the conversation id) set "key" property to the correct value.

Now inside ConversationVC, subscribe to this event:

Notice you only need to specify the return value you are expecting, for TopicEventBus to figure out the event you are subscribing for

class ConversationVC: UIViewController {
    let topicEventBus: TopicEventBus
    let conversationId = "1234"
    
    init(topicEventBus: TopicEventBus) {
        self.topicEventBus = topicEventBus
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        _ = self.topicEventBus.subscribe(topic: conversationId, callback: { (conversationChangedEvent: ConversationChangedEvent) in
            // This will run every time "ConversationChangedEvent" with id 1234 will be fired.
        })
    }
}

This is how you fire an event:


class FiringService {
    let topicEventBus: TopicEventBus
    init(topicEventBus: TopicEventBus) {
        self.topicEventBus = topicEventBus
    }
    
    func conversationTitleChanged() {
        self.topicEventBus.fire(event: ConversationChangedEvent.init(conversationId: "1234",
                                                                                            newTitle: "First update"))
    }
}

You did it! You fired your first event with topic 🤗 🎉

API

If you subscribe to event and specify no topic, you will receive all events from that type, no matter their key:

_ = self.topicEventBus.subscribe { (conversationChangedEvent: ConversationChangedEvent) in
  // All events from this type: ConversationChangedEvent will trigger this block
}

Unsubscribe by calling "stop" on the returned object after subscribing:

let listener = self.topicEventBus.subscribe(topic: conversationId, callback: { (conversationChangedEvent: ConversationChangedEvent) in
//
})
listener.stop()        

On app log out, terminate TopicEventBus by just calling:

self.topicEventBus.terminate()

Example project

Please notice example project contains tests to support all edge cases, plus example code from the above snippets.

Installation

Cocoapods

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

pod 'TopicEventBus'

Manually

  1. Download and drop /TopicEventBus folder in your project.
  2. Congratulations!

Author

Matan made this with ❤️.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory