{"id":1115,"url":"https://github.com/mcmatan/topicEventBus","last_synced_at":"2025-08-06T13:32:28.677Z","repository":{"id":56924425,"uuid":"143418773","full_name":"mcmatan/TopicEventBus","owner":"mcmatan","description":"Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.","archived":false,"fork":false,"pushed_at":"2019-03-20T12:21:01.000Z","size":188,"stargazers_count":55,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-08T04:47:11.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcmatan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-03T11:23:51.000Z","updated_at":"2021-11-29T10:12:10.000Z","dependencies_parsed_at":"2022-08-21T05:20:47.965Z","dependency_job_id":null,"html_url":"https://github.com/mcmatan/TopicEventBus","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcmatan%2FTopicEventBus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcmatan%2FTopicEventBus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcmatan%2FTopicEventBus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcmatan%2FTopicEventBus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcmatan","download_url":"https://codeload.github.com/mcmatan/TopicEventBus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228905469,"owners_count":17989772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:39.235Z","updated_at":"2024-12-09T14:30:52.574Z","avatar_url":"https://github.com/mcmatan.png","language":"Swift","funding_links":[],"categories":["EventBus","Libs","Events [🔝](#readme)"],"sub_categories":["Getting Started","Events","Linter"],"readme":"# TopicEventBus\nPublish–subscribe design pattern implementation framework, with ability to publish events by topic.\n(NotificationCenter extended alternative)\n\nOn Medium: https://medium.com/@matancohen_22770/why-you-should-stop-using-notificationcenter-and-start-using-topiceventbus-c4c7ab312643\n\n[![Language](https://img.shields.io/badge/language-swift-orange.svg?style=flat)](https://developer.apple.com/swift)\n\n\u003cp align = \"center\"\u003e\u003cimg src=\"https://image.ibb.co/ixFt0e/logo_transparent.png\" alt=\"TopicEventBus Icon\"/\u003e\u003c/p\u003e\n\n## About:\n\nTopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.\n\nThere 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.\n\nAlso, TopicEventBus holds weak referenced for Its observers, so you don't have to be afraid of memory leaks.\n\n## What is a topic?\n\nThe 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.\n\nSpecifying the conversation Id is specifying a topic for that event.\n\n## Show me the code! and what's in it for me.\n\nLet's build a chat app!\n\nIn this app, we are going to have multiple conversations screens, each one of them would like to know only about changes to Its conversation.\n\nThe first step, create an event for conversation update:\n\n```Swift\nclass ConversationChangedEvent: TopicEvent {\n    let newTitle: String\n    init(conversationId: String, newTitle: String) {\n        self.newTitle = newTitle\n        super.init()\n        self.key = conversationId\n    }\n}\n```\n\nEvery 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.\n\nNow inside ConversationVC, subscribe to this event:\n\n*Notice you only need to specify the return value you are expecting, for TopicEventBus to figure out the event you are subscribing for*\n\n```Swift\nclass ConversationVC: UIViewController {\n    let topicEventBus: TopicEventBus\n    let conversationId = \"1234\"\n    \n    init(topicEventBus: TopicEventBus) {\n        self.topicEventBus = topicEventBus\n    }\n   \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        _ = self.topicEventBus.subscribe(topic: conversationId, callback: { (conversationChangedEvent: ConversationChangedEvent) in\n            // This will run every time \"ConversationChangedEvent\" with id 1234 will be fired.\n        })\n    }\n}\n```\n\nThis is how you fire an event:\n\n```Swift\n\nclass FiringService {\n    let topicEventBus: TopicEventBus\n    init(topicEventBus: TopicEventBus) {\n        self.topicEventBus = topicEventBus\n    }\n    \n    func conversationTitleChanged() {\n        self.topicEventBus.fire(event: ConversationChangedEvent.init(conversationId: \"1234\",\n                                                                                            newTitle: \"First update\"))\n    }\n}\n```\n\nYou did it! You fired your first event with topic  🤗 🎉\n\n## API \n\nIf you subscribe to event and specify no topic, you will receive all events from that type, no matter their key:\n\n```Swift\n_ = self.topicEventBus.subscribe { (conversationChangedEvent: ConversationChangedEvent) in\n  // All events from this type: ConversationChangedEvent will trigger this block\n}\n```\n\nUnsubscribe by calling \"stop\" on the returned object after subscribing:\n\n```Swift\nlet listener = self.topicEventBus.subscribe(topic: conversationId, callback: { (conversationChangedEvent: ConversationChangedEvent) in\n//\n})\nlistener.stop()        \n```\n\nOn app log out, terminate TopicEventBus by just calling:\n\n```Swift\nself.topicEventBus.terminate()\n```\n\n\n## Example project\n\nPlease notice example project contains tests to support all edge cases, plus example code from the above snippets.\n\n## Installation\n\n#### Cocoapods\n**TopicEventBus** is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'TopicEventBus'\n```\n\n#### Manually\n1. Download and drop ```/TopicEventBus``` folder in your project.  \n2. Congratulations!  \n\n## Author\n\n[Matan](https://github.com/mcmatan) made this with ❤️.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcmatan%2FtopicEventBus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcmatan%2FtopicEventBus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcmatan%2FtopicEventBus/lists"}