Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
sundeepgupta / ci-completion.swift
Last active May 17, 2018 14:29
FlatMap Article - CI Completion
project.addDeveloper(jim) // Jim started coding...
project.addDeveloper(anna) // Anna started coding...
project.stop()
jim.stopCoding()
anna.stopCoding() // CI stopped.
project.addDeveloper(bob)
bob.pushCommit("1")
jim.pushCommit("2")
@sundeepgupta
sundeepgupta / developer-completions.swift
Last active May 18, 2018 13:36
FlatMap Article - Developer Completions
project.addDeveloper(jim) // Jim started coding...
project.addDeveloper(anna) // Anna started coding...
jim.stopCoding()
anna.stopCoding()
jim.pushCommit("1")
project.addDeveloper(bob) // Bob started coding...
bob.pushCommit("2") // CI is building Commit(author: "Bob", hash: "2").
@sundeepgupta
sundeepgupta / project-completion.swift
Last active May 18, 2018 13:36
FlatMap Article - Project Completion
project.addDeveloper(jim) // Jim started coding...
project.stop()
project.addDeveloper(bob)
bob.pushCommit("1")
jim.pushCommit("2") // CI is building Commit(author: "Jim", hash: "2").
@sundeepgupta
sundeepgupta / normal.swift
Last active June 4, 2018 20:24
FlatMap Article - Normal Operation
project.addDeveloper(jim) // Jim started coding...
jim.pushCommit("1") // CI is building Commit(author: "Jim", hash: "1").
project.addDeveloper(anna) // Anna started coding...
jim.pushCommit("2") // CI is building Commit(author: "Jim", hash: "2").
anna.pushCommit("3") // CI is building Commit(author: "Anna", hash: "3").
jim.pushCommit("4") // CI is building Commit(author: "Jim", hash: "4").
@sundeepgupta
sundeepgupta / setup.swift
Last active May 16, 2018 13:56
FlatMap Article - Setup
let project = Project()
let jim = Developer("Jim")
let anna = Developer("Anna")
let bob = Developer("Bob")
let ci = CI()
project.developerStream
.flatMap { developer -> Observable<Commit> in
print("\(developer.name) started coding...")
return developer.startCoding()
@sundeepgupta
sundeepgupta / types.swift
Last active May 16, 2018 17:36
FlatMap Article - Type Declarations
class Project {
private let developerSubject = PublishSubject<Developer>()
var developerStream: Observable<Developer> {
return developerSubject.asObservable()
}
func addDeveloper(_ developer: Developer) {
developerSubject.onNext(developer)
}
import Foundation
// MARK: - Public
public struct WebService {
let actionManager: ActionManager
// As public API, optimize for simplicity by only providing a Result and not throwing.
func getProfile(id: String, completion: @escaping (Result<Profile>) -> Void) {
let action = Action<NSNull, Profile>(urlString: "api.com/profile", method: "GET", body: NSNull())
let semaphore = DispatchSemaphore(value: 0)
asyncFunction() { result in
XCTAssert(result)
semaphore.signal
}
while semaphore.wait(timeout: .now()) == .timedOut {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 3))
}
@sundeepgupta
sundeepgupta / thread-safe-dependency-injection.swift
Created April 13, 2017 15:38
Swift thread-safe dependency injection
import Foundation
protocol HatType { }
class Hat: HatType {
init() {
print("real hat 1")
}
}
@sundeepgupta
sundeepgupta / main.swift
Created March 30, 2017 18:39
Dynamically swap AppDelegate at runtime for unit testing in Swift
let appDelegateClass: AnyClass? = NSClassFromString("TEST_TARGET_MODULE_NAME.TestAppDelegate") ?? AppDelegate.self
let classString = NSStringFromClass(appDelegateClass!)
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))
UIApplicationMain(CommandLine.argc, argv, nil, classString)