Ion is a lightweight data transfer layer loosely based on publish-subscribe messaging pattern. It provides following components:
- Subscriber, which acts as a wrapper for a data processing logic
- Emitter, which manages subscribers, receives and propagates data
- Matcher, which acts as a wrapper for a data validation logic
- Collector, which provides convenience functions for working with subscribers
- Case data wrapper, for wrapping a non-equatable object in an equatable container
- Some data wrapper, for wrapping an equatable object in a non-equatable container
- Result data wrapper, for wrapping an object or an error in a container
It also provides AnyEventSource
& AnyEventSync
wrapper for Emitter
object for convenient access level management.
- Swift 4.1+
The most basic use case would look something like this:
// Service.swift
class Service {
// Prepare emitter & eventSource
private let emitter = Emitter<String>()
lazy var eventSource = AnyEventSource(emitter) // read-only access
// ...
// Send updates
func update() {
emitter.emit("Hello")
}
// ...
}
// ViewModel.swift
class ViewModel {
// Prepare collector
private let service = Service()
private lazy var collector = Collector(source: service.eventSource)
// ...
// Subscribe
init() {
collector.subscribe { (data: String) in
print(data)
}
}
// ...
It's possible to create subscribers manually, but you'd need to take care of unsubscribing then, in order to avoid retain loops:
let subscriber = Subscriber(AnyMatcher()) { (data: String) in
print(data)
}
service.eventSource.subscribe(subscriber)
Matcher objects provide basic filtering functionality, so in order to receive only certain data, it's possible to do something like this:
let matcher = ObjectMatcher(object: "Hello")
let subscriber = Subscriber(matcher) { (data: String) in
print(data) // only "Hello" string will be received
}
service.eventSource.subscribe(subscriber)
Ion provides following matchers out of the box:
AnyMatcher
, which matches any objectsObjectMatcher
, which matches equatable objectsPropertyMatcher
, which matches objects by a specified keyPathClosureMatcher
, which uses user-defined closure to match objectsTrainMatcher
, which matches sequencesResultMatcher
, which matches objects ofResult
wrapper type
It's possible to write custom matchers as well by implementing Matcher
protocol.
Ion is not intended and never will be a replacement for RxSwift or any similar library. Ion is aimed at providing minimal functionality and be as lightweight and simple as possible.
You can use Depo to install Ion by adding it to your Depofile
:
carts:
- kind: github
identifier: rosberry/ion
Carthage
Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add the framework to your project.
github "rosberry/ion"
- Anton Kormakov, [email protected]
This project is owned and maintained by Rosberry. We build mobile apps for users worldwide 🌏.
Check out our open source projects, read our blog or give us a high-five on 🐦 @rosberryapps.
Ion is available under the MIT license. See the LICENSE file for more info.