Skip to content

Instantly share code, notes, and snippets.

View jasdev's full-sized avatar

Jasdev Singh jasdev

View GitHub Profile
@jasdev
jasdev / FoundationErrorWrapper.swift
Created July 14, 2016 02:05
An attempt at a protocol that can imply the present of specific cases
import Foundation
public protocol FoundationErrorWrapper: ErrorProtocol {
static func wrappingError(_ error: NSError) -> Self
}
enum SomeError: ErrorProtocol, FoundationErrorWrapper {
case a
case wrappingError(NSError)
}
@jasdev
jasdev / postfix_type_erasure.swift
Last active March 25, 2020 21:22
Postfix type erasure in Combine.
import Combine
postfix operator ^
postfix func ^ <Publisher: Combine.Publisher>(_ publisher: Publisher)
-> AnyPublisher<Publisher.Output, Publisher.Failure> {
publisher.eraseToAnyPublisher()
}
@jasdev
jasdev / final_cancellation_handling.swift
Last active April 20, 2020 16:00
Final `ReplaySubject` and `.Subscription` cancellation handling.
final class ReplaySubject<Output, Failure: Error>: Subject {
typealias Output = Output
typealias Failure = Failure
private let bufferSize: Int
private var completion: Subscribers.Completion<Failure>?
private var isActive: Bool { /* … */ }
@jasdev
jasdev / geometryPreference.swift
Last active April 24, 2020 19:57
objc.io’s geometryPreference modifier and Preference view for propagating up proxied geometries and reading them.
import SwiftUI
struct Preference<PreferenceKey: SwiftUI.PreferenceKey, Content: View>: View
where PreferenceKey.Value: Equatable {
private let content: (PreferenceKey.Value) -> Content
@State private var value: PreferenceKey.Value = PreferenceKey.defaultValue
init(
_ key: PreferenceKey.Type,
@jasdev
jasdev / srt_parser.swift
Last active November 15, 2022 20:50
Sketch of an SRT file parser.
import Parsing
import Foundation
// [SubRip file format spec.](https://en.wikipedia.org/wiki/SubRip#File_format)
let sampleSRTString =
"""
1
00:00:00,540 --> 00:00:00,960
Yo-yo
@jasdev
jasdev / ScrimLoader.swift
Last active April 19, 2024 03:47
Rough sketch of Arc’s scrim loading view.
import SwiftUI
/**
### Exercises for the viewer
- Phase interrupt handling.
- Use Swift concurrency.
- Color scheme awareness.
- Rework animations to be more spring-like à la what shipped in `0.90.0`.