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 | |
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 { |
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 Combine | |
import SwiftUI | |
import WebKit | |
@Observable final class WebViewContent: NSObject { | |
let id = UUID() | |
var url: URL? | |
var title: 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 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) |
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 | |
// 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>) |
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
// 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 { |
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 | |
struct Weak<Value: AnyObject> { | |
weak var value: Value? | |
init(_ value: Value) { | |
self.value = value | |
} | |
} |
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 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() | |
}) | |
} |
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 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) |
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 SwiftUI | |
func ?? <T>(optional: Binding<T?>, defaultValue: @escaping @autoclosure () -> T) -> Binding<T> { | |
return Binding { | |
optional.wrappedValue ?? defaultValue() | |
} set: { newValue in | |
optional.wrappedValue = newValue | |
} | |
} |
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
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 |
NewerOlder