Skip to content

Instantly share code, notes, and snippets.

import Foundation
import Combine
/// Calls all handlers when triggered, waiting for them to finish running.
actor HandlerManager<Input: Sendable> {
typealias Handler = @Sendable (Input) async -> Void
private var handlers: [UUID: Handler] = [:]
func register(_ handler: @escaping Handler) -> any Cancellable {
import Combine
import SwiftUI
import WebKit
@Observable final class WebViewContent: NSObject {
let id = UUID()
var url: URL?
var title: String?
import ConcurrencyExtras
private struct ObservationTrackingState: Sendable {
var isCancelled: Bool = false
var updateTaskID: UUID?
}
public func withObservationTracking<T>(_ apply: @escaping () -> T) -> AsyncThrowingStream<T, any Error> {
let (stream, continuation) = AsyncThrowingStream.makeStream(of: T.self)
import Foundation
// Foundation.CharacterSet should be called "UnicodeScalarSet"
// this operates on actual Characters
// TODO: Equatable doesn't work right: for instance defining a range vs defining a set with the same values will not be equal
indirect enum CharacterSet: Sendable, Hashable {
case closedRange(ClosedRange<Character>)
case set(Set<Character>)
// example of what the macros could be expanded into for XCTestCase
final class PostsCoordinatorTests: XCTestCase {
func test_addPost() async throws {
let coordinator = PostsCoordinator()
coordinator.createPost()
XCTAssert(coordinator.postCount == 1)
}
func test_hasExistingPosts_countsPosts() async throws {
import Foundation
struct Weak<Value: AnyObject> {
weak var value: Value?
init(_ value: Value) {
self.value = value
}
}
import UIKit
import Combine
public extension Task {
/// Keep a reference to a task that can be cancelled.
func store(in set: inout Set<AnyCancellable>) {
set.insert(AnyCancellable {
self.cancel()
})
}
func fetch<Result: Codable>(_ endpoint: Endpoint<Result>, cacheBehavior: CacheBehavior = .cacheElseNetwork) async throws -> Result {
switch cacheBehavior {
case .cacheElseNetwork:
if let cached = self.cache[endpoint.cacheKey] {
return cached.payload
} else {
return try await self.networkFetch(endpoint)
}
case .networkOnly:
return try await self.networkFetch(endpoint)
import SwiftUI
func ?? <T>(optional: Binding<T?>, defaultValue: @escaping @autoclosure () -> T) -> Binding<T> {
return Binding {
optional.wrappedValue ?? defaultValue()
} set: { newValue in
optional.wrappedValue = newValue
}
}
struct ContentView: View {
var text: Text {
Text("Bacon ipsum dolor amet fatback rump beef ribs jerky pork loin strip steak.")
.font(.headline) +
Text("\nCorned beef t-bone leberkas ball tip tongue burgdoggen picanha swine porchetta flank hamburger strip steak tail pork. Filet mignon prosciutto venison tongue meatball shankle pancetta. Hamburger prosciutto turkey chicken venison tenderloin porchetta spare ribs burgdoggen cupim pork turducken. Short ribs andouille kielbasa short loin beef. Ham kevin pork loin bacon, pastrami turducken jowl pig venison pork shank beef picanha.")
.font(.body)
}
var body: some View {
text