Skip to content

rosberry/ion

Repository files navigation

Ion

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.

Requirements

  • Swift 4.1+

Usage

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 objects
  • ObjectMatcher, which matches equatable objects
  • PropertyMatcher, which matches objects by a specified keyPath
  • ClosureMatcher, which uses user-defined closure to match objects
  • TrainMatcher, which matches sequences
  • ResultMatcher, which matches objects of Result wrapper type

It's possible to write custom matchers as well by implementing Matcher protocol.

Disclaimer

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.

Installation

Depo

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"

Authors

About

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.

License

Ion is available under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •