Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.

Overview

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

TopicEventBus Icon

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 ❤️ .

You might also like...
Testing the new ShazamKit framework announced at WWDC 2021
Testing the new ShazamKit framework announced at WWDC 2021

ShazamKit Demo Using the ShazamKit framework announced at WWDC 2021 to perform song recognition. Demo Tweet Usage Open the project and change the Bund

Facebook's Pop Framework, By Examples
Facebook's Pop Framework, By Examples

Facebook's Pop Framework, By Examples --- This project is a tutorial (Check tutorial here) for how to use Pop framework by Facebook. Its very easy and

DaisyChain is a micro framework which makes UIView animations chaining dead simple.

DaisyChain DaisyChain is a micro framework which makes UIView animations chaining dead simple. It uses the exact same interface you are familiars with

Simple, extensible interpolation framework
Simple, extensible interpolation framework

THIS PROJECT IS NO LONGER MAINTAINED. Popsicle is a Swift framework for creating and managing interpolations of different value types with built-in UI

WWDC 2019'da tanıtılan yeni UI framework'ü SwiftUI'ya giriş amacı gütmektedir.

SwiftUI-Presentation Burada SwiftUI'yı Türkçe bir şekilde sunmak istiyorum. Playground'u açtıktan sonra Editor - Show Rendered Markup seçeneği ile bi

A framework and generator for displaying SwiftUI component libraries

Exhibition Exhibition is a framework and generator for displaying a SwiftUI component library. Inspired by Storybook and Showkase Installation Swift P

Framework to help you better manage UITableViews
Framework to help you better manage UITableViews

UITableView made simple 🙌 Main Features 🙉 Skip the UITableViewDataSource & UITableViewDelegate boilerplate and get right to building your UITableVie

SYBlinkAnimationKit is a blink effect animation framework for iOS, written in Swift.
SYBlinkAnimationKit is a blink effect animation framework for iOS, written in Swift.

SYBlinkAnimationKit is a blink effect animation framework for iOS, written in Swift 👀 Demo There are 5 types of animation for component. border borde

iOS framework for impressive transition animations between views.

CoreTransition iOS framework for impressive transition animations between views. Built using Swift, and supports a lot of animations to navigate to a

Owner
Matan Abravanel
Software Engineer At Melio http://stackoverflow.com/users/3238178/mcmatan
Matan Abravanel
Design-system-demo - This example code is bare-bones to show you what this framework can do

Basic Style Dictionary This example code is bare-bones to show you what this fra

Tylen St Hilaire 0 Feb 3, 2022
Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers

Ease is an event driven animation system that combines the observer pattern with custom spring animations as observers. It's magic. Features Animate a

Robert-Hein Hooijmans 1.3k Nov 17, 2022
Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

Design and prototype customized UI, interaction, navigation, transition and animation for App Store ready Apps in Interface Builder with IBAnimatable.

IBAnimatable 8.6k Jan 2, 2023
Sample way of integrating animations into a design system for iOS app projects.

Animations in Design System The project presents a sample way of integrating animations into a design system for iOS developers. Project setup A sampl

Bulat Khabirov 1 Nov 26, 2021
Neomorphism - Neomorphism Design for ios

Neomorphism This is package for deigning the view of an IOS applction in the neo

CyberPixelC 1 Jan 9, 2022
ButtonClickStyle - This is a Customizable/Designable Button View, with 15 animated click styles, that allows you to design your own buttons from subviews, in storyboard and xib right away.

ButtonClickStyle - This is a Customizable/Designable Button View, with 15 animated click styles, that allows you to design your own buttons from subviews, in storyboard and xib right away.

Rustam 25 Oct 10, 2022
Colorful - A SwiftUI implementation of AppleCard's animated colorful blur background.

Colorful - A SwiftUI implementation of AppleCard's animated colorful blur background.

Lakr Aream 200 Jan 3, 2023
A SwiftUI implementation of Wave animating shape.

WaveAnimation A SwiftUI implementation of Wave animating shape. Preview Usage import SineWaveShape SineWaveShape(percent: 0.4, strength: 30, frequency

Zerlz 16 Sep 23, 2022
WordleDemo - A SwiftUI implementation of Wordle's core logic and UI

WordleDemo Just a SwiftUI implementation of Wordle's core logic and UI Contains

null 0 Feb 13, 2022
Gemini is rich scroll based animation framework for iOS, written in Swift.

Overview What is the Gemini? Gemini is rich scroll based animation framework for iOS, written in Swift. You can easily use GeminiCollectionView, which

Shohei Yokoyama 3k Dec 27, 2022