Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/malcommac/RealEventsBus
🚎 Simple type-safe event bus implementation in swift
https://github.com/malcommac/RealEventsBus
eventbus notifications nsnotifications swift
Last synced: 2 months ago
JSON representation
🚎 Simple type-safe event bus implementation in swift
- Host: GitHub
- URL: https://github.com/malcommac/RealEventsBus
- Owner: malcommac
- License: mit
- Created: 2021-11-28T17:09:27.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-28T19:08:40.000Z (about 3 years ago)
- Last Synced: 2024-07-03T01:40:21.979Z (6 months ago)
- Topics: eventbus, notifications, nsnotifications, swift
- Language: Swift
- Homepage:
- Size: 24.4 KB
- Stars: 13
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - RealEventsBus
README
# 🚎 RealEventsBus
RealEventsBus is a small swift experiment package to implement a basic **type-safe event bus** mechanism.
Some other implementations in GitHub are the sources of inspiration for this package.You can use it as replacement for standard `NSNotification`'s one-to-many messaging.
It uses GCD for messagings, it's thread-safe and, best of all, it's type safe.## ⭐️ Feature Highlights
- It's **type safe**
- Implement **custom messages**; just set conformance to `Event` or `BufferedEvent` type
- Messages/observers are posted and registered in thread safe
- **Easy to use**; just one line to register and post events
- Supports for **buffered events** (get the last value published by a bus)## 🕵️ How It Works
This example uses `enum` as datatype for event.
Btw you can use any type you want as event, `struct` or `class` (see the other example below).
First 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:```swift
public enum UserEvents: Event {
case userDidLogged(username: String)
case userLoggedOut
case profileUpdated(fullName: String, age: Int)
}
```Suppose you want to be notified about this kind of events in your `UIViewController`:
```swift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()Bus.register(self) { event in
switch event {
case .profileUpdated(let fullName, let age):
print("Profile updated with '\(fullName)', which is \(age) old")
case .userDidLogged(let username):
print("User '\(username)' logged in")
case .userLoggedOut:
print("User logged out")
}
}
}deinit {
// While it's not required (it does not generate any leak)
// you may want to unregister an observer when it's not needed anymore.
Bus.unregister(self)
}
}
```When you need to post new events to any registered obserer like the one above just use `post` function:
```swift
Bus.post(.userDidLogged(username: "danielemm"))
```### BufferedEvent
If 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.
Moreover: when a new observer is registered it will receive the last value posted into the bus, if any.This is an example.
First of all we define the message:
```swift
public class CustomEvent: BufferedEvent {
var messageValue: String
var options: [String: Any?]?
public init(value: String, options: [String: Any?]) {
self.messageValue = value
self.options = options
}}
``````swift
// Post a new event
Bus.post(.init(value: "Some message", options: ["a": 1, "b": "some"]))// At certain point in your code:
let lastValue = Bus.lastValue() // print the type above!
```### Custom Dispatch Queue
You can also specify a queue where the message callback will be called.
By default the `.main` queue is used.```swift
Bus.register(self, queue: .global()) { _ in // in background queue
// do something
}
```## Install
### Swift Package Manager
To install it using the Swift Package Manager specify it as dependency in the Package.swift file:
```swift
dependencies: [
.package(url: "https://github.com/malcommac/RealEventsBus.git", branch: "main"),
],
```### CocoaPods
Not yet supported.
## Author
This little experiment was created by [Daniele Margutti](mailto:[email protected]).
If you like it you can fork or open a PR or report an issue.
If you want to support my work just [take a look at my Github Profile](https://github.com/malcommac).## License
It's the MIT license.