{"id":13462654,"url":"https://github.com/malcommac/RealEventsBus","last_synced_at":"2025-03-25T05:32:08.617Z","repository":{"id":46594958,"uuid":"432774526","full_name":"malcommac/RealEventsBus","owner":"malcommac","description":"🚎 Simple type-safe event bus implementation in swift","archived":false,"fork":false,"pushed_at":"2021-11-28T19:08:40.000Z","size":25,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T05:05:01.313Z","etag":null,"topics":["eventbus","notifications","nsnotifications","swift"],"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/malcommac.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":"2021-11-28T17:09:27.000Z","updated_at":"2025-03-14T10:41:40.000Z","dependencies_parsed_at":"2022-09-05T21:40:32.923Z","dependency_job_id":null,"html_url":"https://github.com/malcommac/RealEventsBus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FRealEventsBus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FRealEventsBus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FRealEventsBus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FRealEventsBus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/malcommac","download_url":"https://codeload.github.com/malcommac/RealEventsBus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245407462,"owners_count":20610226,"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":["eventbus","notifications","nsnotifications","swift"],"created_at":"2024-07-31T13:00:18.210Z","updated_at":"2025-03-25T05:32:08.328Z","avatar_url":"https://github.com/malcommac.png","language":"Swift","funding_links":[],"categories":["Call Back(Delegate)"],"sub_categories":[],"readme":"# 🚎 RealEventsBus\n\nRealEventsBus is a small swift experiment package to implement a basic **type-safe event bus** mechanism.  \nSome other implementations in GitHub are the sources of inspiration for this package.  \n\nYou can use it as replacement for standard `NSNotification`'s one-to-many messaging.\nIt uses GCD for messagings, it's thread-safe and, best of all, it's type safe.\n\n## ⭐️ Feature Highlights\n\n- It's **type safe**\n- Implement **custom messages**; just set conformance to `Event` or `BufferedEvent` type\n- Messages/observers are posted and registered in thread safe\n- **Easy to use**; just one line to register and post events\n- Supports for **buffered events** (get the last value published by a bus)\n\n## 🕵️ How It Works\n\nThis example uses `enum` as datatype for event.  \nBtw you can use any type you want as event, `struct` or `class` (see the other example below).\nFirst of all we need to define a custom event; if your event is a group of different messages this is the best thing you can do:\n\n```swift\npublic enum UserEvents: Event {\n    case userDidLogged(username: String)\n    case userLoggedOut\n    case profileUpdated(fullName: String, age: Int)\n}\n```\n\nSuppose you want to be notified about this kind of events in your `UIViewController`:\n\n```swift\nclass ViewController: UIViewController {\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        Bus\u003cUserEvents\u003e.register(self) { event in\n            switch event {\n            case .profileUpdated(let fullName, let age):\n                print(\"Profile updated with '\\(fullName)', which is \\(age) old\")\n            case .userDidLogged(let username):\n                print(\"User '\\(username)' logged in\")\n            case .userLoggedOut:\n                print(\"User logged out\")\n            }\n        }\n    }\n\n    deinit {\n        // While it's not required (it does not generate any leak) \n        // you may want to unregister an observer when it's not needed anymore.\n        Bus\u003cUserEvents\u003e.unregister(self)\n    }\n}\n```\n\nWhen you need to post new events to any registered obserer like the one above just use `post` function:\n\n```swift\nBus\u003cUserEvents\u003e.post(.userDidLogged(username: \"danielemm\"))\n```\n\n### BufferedEvent\n\nIf your event is conform to `BufferedEvent` instead of `Event` you can use the `lastValue()` function to get the latest posted value into the bus. It's like Rx.  \nMoreover: when a new observer is registered it will receive the last value posted into the bus, if any.\n\nThis is an example.\n\nFirst of all we define the message:\n\n```swift\npublic class CustomEvent: BufferedEvent {\n    \n    var messageValue: String\n    var options: [String: Any?]?\n    \n    public init(value: String, options: [String: Any?]) {\n        self.messageValue = value\n        self.options = options\n    }\n\n}\n```\n\n```swift\n    // Post a new event\n    Bus\u003cCustomEvent\u003e.post(.init(value: \"Some message\", options: [\"a\": 1, \"b\": \"some\"]))\n\n    // At certain point in your code:\n    let lastValue = Bus\u003cCustomEvent\u003e.lastValue() // print the type above!\n```\n\n### Custom Dispatch Queue\n\nYou can also specify a queue where the message callback will be called.  \nBy default the `.main` queue is used.\n\n```swift\nBus\u003cCustomEvent\u003e.register(self, queue: .global()) { _ in // in background queue\n   // do something\n}        \n```\n\n## Install\n\n### Swift Package Manager\n\nTo install it using the Swift Package Manager specify it as dependency in the Package.swift file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/malcommac/RealEventsBus.git\", branch: \"main\"),\n],\n```\n\n### CocoaPods\n\nNot yet supported.\n\n## Author \n\nThis little experiment was created by [Daniele Margutti](mailto:hello@danielemargutti.com).  \nIf you like it you can fork or open a PR or report an issue.  \nIf you want to support my work just [take a look at my Github Profile](https://github.com/malcommac).\n\n## License\n\nIt's the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalcommac%2FRealEventsBus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmalcommac%2FRealEventsBus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalcommac%2FRealEventsBus/lists"}