Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
sundeepgupta / ci-completion.swift
Last active May 17, 2018
FlatMap Article - CI Completion
View ci-completion.swift
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
FlatMap Article - Developer Completions
View developer-completions.swift
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
FlatMap Article - Project Completion
View project-completion.swift
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 Jun 4, 2018
FlatMap Article - Normal Operation
View normal.swift
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
FlatMap Article - Setup
View setup.swift
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
FlatMap Article - Type Declarations
View types.swift
class Project {
private let developerSubject = PublishSubject<Developer>()
var developerStream: Observable<Developer> {
return developerSubject.asObservable()
}
func addDeveloper(_ developer: Developer) {
developerSubject.onNext(developer)
}
View networking-pattern.swift
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())
View semaphore-async-test.swift
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 Apr 13, 2017
Swift thread-safe dependency injection
View thread-safe-dependency-injection.swift
import Foundation
protocol HatType { }
class Hat: HatType {
init() {
print("real hat 1")
}
}
@sundeepgupta
sundeepgupta / main.swift
Created Mar 30, 2017
Dynamically swap AppDelegate at runtime for unit testing in Swift
View main.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)