Skip to content

Instantly share code, notes, and snippets.

View swhitty's full-sized avatar

Simon Whitty swhitty

View GitHub Profile
import Combine
/// Subscriber that weakly references its target with a closure.
/// The target is sent to the action when it exists, so the caller can avoid
/// the [weak self] dance.
///
/// ```
/// let binder = Binder(self) { $0.doSomething($1) )
/// ```
///
import Foundation
/// Property wrapper that protects the wrapped value with `NSLock`.
/// Non trivial read/writes should access via `projectedValue` which acquires lock
/// and runs the supplied closure against it.
///
/// ```
/// @Locked var count: Int
/// $count { $0 = doSomething($0) }
/// ```
// Closures cannot be weak, but we can wrap an object and its static factory method that returns an
// instance to create a pseudo weak closure
//
struct Method {
static func weak<T: AnyObject, A>(_ base: T, factory: @escaping (T) -> ((A) -> Void)) -> (A) -> Void {
{ [weak base] in base.map(factory)?($0) }
}
import Combine
// `UIScheduler` eagerly executes actions on the current queue if
// it is the main thread, avoiding unnecessary async behaviour.
// If the current queue is not the main thread, then the action is
// then scheduled on the main queue asynchonously.
//
// `DispatchQueue` schedulers execute all actions asynchronously.
//
struct UIScheduler: Scheduler {
import Combine
extension Publisher where Failure == Never {
/// Converts a publisher into a `Driver`
func driver() -> Driver<Output> { Driver(self) }
}
extension Driver {
func driver() -> Driver<Output> { self }
import Combine
public extension Publisher {
/// Converts a publisher's output into the most recent output of another publisher.
///
/// - parameter other: A publisher that will provide the latest value
/// - returns: A publisher that emits the latest ouput of another publisher
func withLatestFrom<Other: Publisher>(_ other: Other) -> WithLatestFrom<Self, Other, Other.Output> {
@swhitty
swhitty / Task+GroupOperations.swift
Last active January 13, 2021 00:20
Helper to make concurrent task group with any number of operations
import _Concurrency
extension Task {
// Create group with n tasks, executed concurrently.
// Returned array preserves original order of operations.
static func withGroup<TaskResult>(operations: [() async throws -> TaskResult]) async throws -> [TaskResult] {
return try await Task.withGroup(resultType: (Int, TaskResult).self) { group in
for (idx, provider) in operations.enumerated() {
await group.add {
import UIKit
public extension UIApplication {
// When unit testing some UIKit accessibility label properties are not updated until either;
// - Accessibility Inspector is connected to the specific simulator once, then closed. 🤷🏻‍♂️
// - Calling accessibilityActivate() one time.
func activateSimulatorForAccessibilityTesting() {
accessibilityActivate()
import CoreGraphics
extension CGPath {
// Creates a smooth CGPath line between supplied points.
//
// Typical Polyline, straight line from point to point.
//
// /\
// __ ___/ \ /
extension CGPattern {
static func make(bounds: CGRect,
matrix: CGAffineTransform,
step: CGSize,
tiling: CGPatternTiling,
isColored: Bool,
draw: @escaping (CGContext) -> Void) -> CGPattern {
let drawPattern: CGPatternDrawPatternCallback = { info, ctx in