This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// An abstract class that makes building simple asynchronous operations easy. | |
/// Subclasses must override `main()` to perform any work and call `finish()` | |
/// when they are done. All `NSOperation` work will be handled automatically. | |
/// | |
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233 | |
open class AsyncOperation: Operation { | |
public init(name: String? = nil) { | |
super.init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Enforces a function to not be called again until a certain amount of time has passed without it being called. | |
/// As in "execute this function only if 100 milliseconds have passed without it being called." | |
public final class Debouncer { | |
// MARK: - Properties | |
public let limit: DispatchTimeInterval | |
public let queue: DispatchQueue | |
private var workItem: DispatchWorkItem? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class GCDMulticastDelegate<T> { | |
private var nodes = Set<Node<AnyObject>>() | |
public init() { } | |
deinit { | |
removeAllDelegates() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SpinlockTestTests.swift | |
// SpinlockTestTests | |
// | |
// Created by Peter Steinberger on 04/10/2016. | |
// Copyright ยฉ 2016 PSPDFKit GmbH. All rights reserved. | |
// | |
import XCTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class SafeSyncQueue { | |
struct QueueIdentity { | |
let label: String | |
} | |
let queue: DispatchQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Manager of asynchronous download `Operation` objects | |
class DownloadManager: NSObject { | |
/// Dictionary of operations, keyed by the `taskIdentifier` of the `URLSessionTask` | |
fileprivate var operations = [Int: DownloadOperation]() | |
/// Serial NSOperationQueue for downloads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
@available(*, deprecated, message: "Swift 4 supports conversion between NSRange and Range ( Range.init?(_:in) )") | |
private func range(from nsRange: NSRange) -> Range<Index>? { | |
guard let range = Range(nsRange) else { return nil } | |
let utf16Start = UTF16Index(encodedOffset: range.lowerBound) | |
let utf16End = UTF16Index(encodedOffset: range.upperBound) | |
guard | |
let start = Index(utf16Start, within: self), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class ConcealingTitleView: UIView { | |
private let label = UILabel() | |
private var contentOffset: CGFloat = 0 { | |
didSet { | |
label.frame.origin.y = titleVerticalPositionAdjusted(by: contentOffset) | |
} | |
} | |
var text: String = "" { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PlaygroundSupport | |
import Foundation | |
class Worker { | |
private let queue = DispatchQueue.global(qos: .background) | |
private let serialQueue = DispatchQueue(label: "com.acme.serial") | |
public private(set) var count = 0 | |
func incrementCount() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func time() -> DispatchTimeInterval { | |
//https://stackoverflow.com/questions/12488481/getting-ios-system-uptime-that-doesnt-pause-when-asleep | |
var uptime = timespec() | |
precondition(clock_gettime(CLOCK_MONOTONIC_RAW, &uptime) == 0, "Could not execute clock_gettime, errno: \(errno)") | |
let nanoseconds = (uptime.tv_sec * 1_000_000_000) + uptime.tv_nsec | |
return .nanoseconds(nanoseconds) | |
} |