Skip to content

Instantly share code, notes, and snippets.

View maxsokolov's full-sized avatar
🏝️

Max Sokolov maxsokolov

🏝️
View GitHub Profile
// https://github.com/avito-tech
private class ObserverBox<T>: Hashable {
typealias Observer = T
private(set) weak var disposable: AnyObject?
private let objectIdentifier: ObjectIdentifier
var observers = [Observer]()
let hashValue: Int
// https://github.com/avito-tech
//
// Sampler is like a baby of Debouncer and Throttler.
//
// Unlike Debouncer, sampler executes action immediately (if there are no actions before, for time > delay)
// Unlike Throttler it guarantees that last action in sequence will be executed (see last error in example)
/// CAUTION: This class isn't thread-safe
public final class Sampler {
// MARK: - Public properite
public func with<T>(_ item: inout T, action: (inout T) -> Void) {
action(&item)
}
public func with<T>(_ item: T, action: (T) -> Void) {
action(item)
}
public func with<T: AnyObject>(_ item: T, action: (T) -> Void) {
action(item)
@maxsokolov
maxsokolov / Result.swift
Created December 4, 2016 21:27
A nice one
enum Result<T, E> {
case value(T)
case error(E)
var value: T? {
switch self {
case .value(let value):
return value
case .error:
@maxsokolov
maxsokolov / Timer.swift
Created November 3, 2016 08:51
gcd based timer using swift
private let timerQueue = DispatchQueue(label: "com.timer.queue", attributes: [])
final class Timer : NSObject {
private var timer: DispatchSourceTimer?
var active: Bool {
return timer != nil
}
func start(_ interval: Int, repeats: Bool = false, handler: @escaping () -> Void) {