This file contains hidden or 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 Child: View { | |
| var count: Int | |
| var body: some View { | |
| Text("Child: \(count)") | |
| .useEffect(Int(floor(Double(count) / 2))) { source in | |
| // this only gets called when the source changes | |
| print("onAppear \(source)") | |
| } | |
| } |
This file contains hidden or 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 Foundation | |
| import SwiftUI | |
| class ObservablePublisher<Output>: ObservableObject { | |
| @Published var output: Output? | |
| @Published var error: Swift.Error? | |
| private var observer: AnyCancellable? | |
| init() {} |
This file contains hidden or 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
| @propertyWrapper | |
| struct IgnoreHashable<T>: Hashable { | |
| var wrappedValue: T | |
| init(wrappedValue: T) { | |
| self.wrappedValue = wrappedValue | |
| } | |
| func hash(into hasher: inout Hasher) {} | |
This file contains hidden or 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
| class Loader<Output>: ObservableObject { | |
| @Published var isLoding: Bool = false | |
| @Published var error: Swift.Error? | |
| @Published var value: Output? | |
| private var observer: AnyCancellable? | |
| func load<P: Publisher>(_ publisher: P) where P.Output == Output { | |
| self.observer?.cancel() | |
This file contains hidden or 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 Lock { | |
| private var unfairLock = os_unfair_lock_s() | |
| mutating func lock(_ work: () -> Void) { | |
| self.lock() | |
| work() | |
| self.unlock() | |
| } | |
| mutating func trylock(_ work: () -> Void) -> Bool { |
This file contains hidden or 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 ObjectiveC | |
| extension objc_AssociationPolicy { | |
| public static let retain = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN | |
| public static let retainNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC | |
| public static let copy = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY | |
| public static let copyNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC | |
| public static let assign = objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN | |
| } |
This file contains hidden or 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 | |
| /// Allows a property to fail decoding, defaulting to nil instead of surfacing the error. | |
| @propertyWrapper | |
| struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable { | |
| var wrappedValue: Wrapped? | |
| init() { | |
| self.wrappedValue = nil | |
| } |
This file contains hidden or 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 _if<T>(_ test: Bool, action: () -> T) -> T? { | |
| if test { | |
| return action() | |
| } else { | |
| return nil | |
| } | |
| } | |
| extension Optional { | |
| func _else(action: () -> Wrapped) -> Wrapped { |
This file contains hidden or 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
| extension Collection where Element: FloatingPoint { | |
| func average() -> Element { | |
| return self.reduce(Element.zero, +) / Element(self.count) | |
| } | |
| } | |
| extension Collection where Element: BinaryInteger { | |
| func average() -> Double { | |
| return Double(self.reduce(0, +)) / Double(self.count) | |
| } |