Skip to content

Instantly share code, notes, and snippets.

View thomsmed's full-sized avatar
:octocat:
✌️😎👌

Thomas Asheim Smedmann thomsmed

:octocat:
✌️😎👌
View GitHub Profile
@thomsmed
thomsmed / PendingOperation.swift
Created March 16, 2025 14:11
A utility type for letting multiple caller Tasks wait for a future value produced by an asynchronous operation.
//
// PendingOperation.swift
//
import Foundation
/// A utility type for letting multiple caller Tasks wait for a future value produced by an asynchronous operation.
public final class PendingOperation<T: Sendable>: @unchecked Sendable {
private let lock = NSLock()
@thomsmed
thomsmed / MockURLProtocol.swift
Last active March 14, 2025 21:38
A simple MockURLProtocol for Unit/Integration tests relying on URLSession.
//
// MockURLProtocol.swift
//
/// Inspired by [Testing Tips & Tricks](https://developer.apple.com/videos/play/wwdc2018/417).
final class MockURLProtocol: URLProtocol {
static nonisolated(unsafe) var responseBuilders: [(URLRequest) throws -> (Data, HTTPURLResponse, URLRequest?)] = []
override static func canInit(with request: URLRequest) -> Bool {
!responseBuilders.isEmpty
@thomsmed
thomsmed / AsyncValues.swift
Last active February 16, 2025 13:54
An `AsyncSequence` that accept a closure to produce its values at a optional `TimeInterval`, with a `Context` that can be used to track relevant information between each iteration.
//
// AsyncValues.swift
//
/// An `AsyncSequence` that accept a closure to produce its values at an optional `TimeInterval`.
///
/// The `Context` can be used to track relevant information between each iteration of the `AsyncSequence`.
public struct AsyncValues<Value: Sendable, Context: Sendable>: AsyncSequence {
public struct Iterator: AsyncIteratorProtocol {
private let interval: TimeInterval
@thomsmed
thomsmed / PendingValue.swift
Last active March 16, 2025 12:01
A utility type for letting multiple caller Tasks wait for a future value, whom exact arrival is unknown and will be manually triggered some time in the future.
//
// PendingValue.swift
//
import Foundation
/// A utility type for letting multiple caller Tasks wait for a future value, whom exact arrival is unknown and will be manually triggered some time in the future.
public final class PendingValue<T: Sendable>: @unchecked Sendable {
private let lock = NSLock()
@thomsmed
thomsmed / HTTPAPIProblem.swift
Last active January 19, 2025 11:48
A simple Swift implementation of [RFC 7807 - Problem Details for HTTP APIs](https://www.rfc-editor.org/rfc/rfc7807).
//
// HTTPAPIProblem.swift
//
import Foundation
// MARK: HTTP API Problem
/// Error thrown when decoding a`HTTPAPIProblem` if the decoded HTTP API Problem's type does not match the type of the associated `Extras`.
public struct HTTPAPIProblemTypeMismatch: Error {}
@thomsmed
thomsmed / AppModuleEnvironmentConfiguration.swift
Created October 6, 2024 19:51
Simple example on how to do initial configuration/setup for different modules in an app on app startup.
//
// AppModuleEnvironmentConfiguration.swift
//
import SwiftUI
// MARK: AppDelegate
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(
@thomsmed
thomsmed / RestrictiveResourceLoader.swift
Created August 20, 2024 17:13
Simple example on how to restrict access to one or more "resources" (local storage, remote endpoint, etc). Very similar to how one would use a semaphore for protected access.
//
// RestrictiveResourceLoader.swift
//
import Foundation
/// Simple example on how to restrict access to one or more "resources" (local storage, remote endpoint, etc).
/// Very similar to how one would use a semaphore for protected access.
final actor RestrictiveResourceLoader {
private var isFetchingResource: Bool = false
@thomsmed
thomsmed / AsyncSemaphore.swift
Created August 18, 2024 14:56
An AsyncSemaphore suited for use in asynchronous contexts. Heavily inspired by https://github.com/groue/Semaphore.
//
// AsyncSemaphore.swift
//
import Foundation
/// An AsyncSemaphore suited for use in asynchronous contexts. Heavily inspired by https://github.com/groue/Semaphore.
public final class AsyncSemaphore: @unchecked Sendable {
final class QueuedContinuation: @unchecked Sendable {
enum State {
@thomsmed
thomsmed / RunLoopSignaling.swift
Created February 12, 2024 16:48
Snippet showing how to configure a simple custom CFRunLoopSource.
//
// StopRunLoopSource.swift
//
import Foundation
final class StopRunLoopSource: NSObject {
// More on how to define custom run loop input sources: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW3
// And tips on how to terminate threads: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW10
@thomsmed
thomsmed / RunLoopObserving.swift
Created February 12, 2024 16:25
Snippet showing how to configure a CFRunLoopObserver.
//
// RunLoopObserving.swift
//
import Foundation
func name(for thread: Thread) -> String {
if let name = Thread.current.name, !name.isEmpty {
return name
} else {