Skip to content

Instantly share code, notes, and snippets.

View V8tr's full-sized avatar

Vadim Bulavin V8tr

View GitHub Profile
import Foundation
import XCTest
protocol AutoEquatable: Equatable {}
extension AutoEquatable {
static func ==(lhs: Self, rhs: Self) -> Bool {
var lhsDump = String()
dump(lhs, to: &lhsDump)
import Foundation
protocol JSONSerializable {
func toJSON() throws -> Any?
}
enum CouldNotSerializeError: Error {
case noImplementation(source: Any, type: String)
case undefinedKey(source: Any, type: String)
}
@V8tr
V8tr / Bechmark.swift
Last active January 20, 2021 12:07
Benchmark arbitrary block of code in Swift. See blog post for more details: http://www.vadimbulavin.com/benchmarking-locking-apis/
func benchmark(block: () -> Void) -> TimeInterval {
let startTime = CFAbsoluteTimeGetCurrent()
block()
let endTime = CFAbsoluteTimeGetCurrent()
let totalTime = endTime - startTime
return totalTime
}
@V8tr
V8tr / ReadWriteLockAtomicProperty.swift
Created July 14, 2018 07:15
Atomic property in Swift using ReadWriteLock. See blog post for more details: http://www.vadimbulavin.com/atomic-properties/
final class ReadWriteLock {
private var rwlock: pthread_rwlock_t = {
var rwlock = pthread_rwlock_t()
pthread_rwlock_init(&rwlock, nil)
return rwlock
}()
func writeLock() {
pthread_rwlock_wrlock(&rwlock)
}
@V8tr
V8tr / git-tips-and-tricks.md
Last active April 15, 2019 11:00
Git tips & tricks
@V8tr
V8tr / MassiveAppDelegate-Commands.swift
Last active August 6, 2018 08:38
Refactoring Massive App Delegate using Command pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
protocol Command {
func execute()
}
struct InitializeThirdPartiesCommand: Command {
func execute() {
// Third parties are initialized here
}
}
@V8tr
V8tr / RefactoringAppDelegate-Command.swift
Last active August 3, 2018 08:58
Refactoring Massive App Delegate using Command pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
// MARK: - Builder
final class StartupCommandsBuilder {
private var window: UIWindow!
func setKeyWindow(_ window: UIWindow) -> StartupCommandsBuilder {
self.window = window
return self
}
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active August 3, 2018 08:04
Refactoring Massive App Delegate using Composite pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
enum AppDelegateFactory {
static func makeDefault() -> AppDelegateType {
return CompositeAppDelegate(appDelegates: [PushNotificationsAppDelegate(), StartupConfiguratorAppDelegate(), ThirdPartiesConfiguratorAppDelegate()])
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
@V8tr
V8tr / RefactoringAppDelegate-Composite.swift
Last active August 3, 2018 08:04
Refactoring Massive App Delegate using Mediator pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
typealias AppDelegateType = UIResponder & UIApplicationDelegate
class CompositeAppDelegate: AppDelegateType {
private let appDelegates: [AppDelegateType]
init(appDelegates: [AppDelegateType]) {
self.appDelegates = appDelegates
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
@V8tr
V8tr / RefactoringAppDelegate-Mediator.swift
Created August 2, 2018 14:48
Refactoring Massive App Delegate using Mediator pattern. See blog post for more details: https://www.vadimbulavin.com/refactoring-massive-app-delegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let mediator = AppLifecycleMediator.makeDefaultMediator()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
}