Skip to content

A bus, like the one on your motherboard

License

Notifications You must be signed in to change notification settings

IgorMuzyka/Omnibus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Omnibus

Swift dependency container.

Installation

To install it, simply add the following line to your Podfile:

pod 'Omnibus', :git => 'https://github.com/IgorMuzyka/Omnibus.git'

Usage

Create your module and conform to Module. You can also conform to Installable, Uninstallable protocols to be able to handle those events.

import Omnibus

public class MyFirstModule: Module, Installable, Uninstallable {

	public weak var bus: Bus?

	public var preInstall: ((Bus) -> Void)! {
		return { bus in
			print("will install module on", bus)
		}
	}
	public var postInstall: ((Bus) -> Void)!
	public var preUninstall: ((Bus) -> Void)! {
		return { bus in
			print("will uninstall module from", bus)
		}
	}
	public var postUninstall: ((Bus) -> Void)!

	public init() {

	}
}

Or some struct Object without conforming to Module

public struct MyObject {

	var name: String
}

Extend the Bus. You can also specify Installable, Uninstallable calls on ModuleWrap object.

import Omnibus

extension Bus {

	public static var common: Bus {
		return .new { bus in
			bus.install(MyFirstModule())
			bus.install(
				ModuleWrap(MyObject(name: "Object"))
					.preInstall {
						print("will install object on", $0)
					}.preUninstall {
						print("will uninstall object from", $0)
					}
			)
		}
	}
}

Create the Bus and install a Backdoor on it

// somewhere in your main.swift or AppDelegate.swift

Backdoor.install(on: .common)

And whenever you need your specific Module just access them

// like this
let myFirstModuleOne: MyFirstModule? = Backdoor.bus?.access()
let myObjectOne: MyObject? = Backdoor.bus?.access()

// or like this
let myFirstModuleTwo = Backdoor.bus?.access() as MyFirstModule?
let myObjectTwo = Backdoor.bus?.access() as MyObject?

And you can always uninstall your Backdoor from the Bus which will also destroy the Bus uninstalling all the modules it has.

Backdoor.uninstall(from: Backdoor.bus)

If you want to just change your Bus you should

Backdoor.uninstall(from: Backdoor.bus)
Backdoor.install(on: someBus)

You can install and uninstall modules on Bus at any time

let module = MyFirstModule()
Backdoor.bus?.install(module)
Backdoor.bus?.uninstall(module)

Since the Bus is Module you can place it on any Bus as long as you have a unique instance inheriting from Bus

public class MySubBus: Bus {}

And then you can access it just like a usual Module from any other Bus

let sub: MySubBus? = bus?.access()

Author

Igor Muzyka, igormuzyka42@gmail.com

License

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