Skip to content

Instantly share code, notes, and snippets.

@barron9
Created October 29, 2021 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barron9/53b6e6395ae9f5e9fe5596fb0fd89312 to your computer and use it in GitHub Desktop.
Save barron9/53b6e6395ae9f5e9fe5596fb0fd89312 to your computer and use it in GitHub Desktop.
cleanse_swift_subcomponent_injection(modules).swift
import Foundation
import Core
import Cleanse
public class FeatureX{
public init(){
// _ = CoreDummy()
let f = try! ComponentFactory.of(APIComponent.self)
RootViewController2(loggedInComponent: f)
}
}
class RootViewController2: UIViewController {
let loggedInComponent: ComponentFactory<APIComponent>
init(loggedInComponent: ComponentFactory<APIComponent>) {
self.loggedInComponent = loggedInComponent
super.init(nibName: nil, bundle: nil)
logIn()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func logIn() {
let apiRoot = loggedInComponent.build(())
print(apiRoot.somethingUsingTheAPI.primaryURL)
print(apiRoot.somethingUsingTheAPI.secondaryURL)
}
}
public struct PrimaryAPIURL : Tag {
public typealias Element = NSURL
}
public struct SecondaryAPIURL : Tag {
public typealias Element = NSURL
}
struct PrimaryAPIURLModule : Module {
static func configure(binder: Binder<Unscoped>) {
binder
.bind(NSURL.self)
.tagged(with: PrimaryAPIURL.self)
.to(value: NSURL(string: "https://connect.squareup.com/v2/")!)
binder
.bind(NSURL.self)
.tagged(with: SecondaryAPIURL.self)
.to(value: NSURL(string: "https://connect.squareup.com/v2/secondary")!)
}
}
class SomethingThatDoesAnAPICall {
let primaryURL: NSURL
let secondaryURL: NSURL
init(primaryURL: TaggedProvider<PrimaryAPIURL>,secondaryURL:TaggedProvider<SecondaryAPIURL>) {
self.primaryURL = primaryURL.get()
self.secondaryURL = secondaryURL.get()
}
struct Module : Cleanse.Module {
static func configure(binder: Binder<Unscoped>) {
binder
.bind(SomethingThatDoesAnAPICall.self)
.to(factory: SomethingThatDoesAnAPICall.init)
}
}
}
struct RootAPI {
let somethingUsingTheAPI: SomethingThatDoesAnAPICall
}
struct APIComponent : RootComponent{
typealias Root = RootAPI
static func configureRoot(binder bind: ReceiptBinder<RootAPI>) -> BindingReceipt<RootAPI> {
return bind.to(factory: RootAPI.init)
}
static func configure(binder: Binder<Unscoped>) {
// "include" the modules that create the component
binder.include(module: PrimaryAPIURLModule.self)
binder.include(module: SomethingThatDoesAnAPICall.Module.self)
// bind our root Object
binder
.bind(RootAPI.self)
// .to(factory: RootAPI.init)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment