Skip to content

Instantly share code, notes, and snippets.

View ollieatkinson's full-sized avatar
🪐

Oliver Atkinson ollieatkinson

🪐
View GitHub Profile
// Simplified version of https://gist.github.com/anandabits/d9494d14fef221983ff4f1cafa318d47#file-areequatablyequal-swift
func isEqual(x: Any, y: Any) -> Bool {
func f<LHS>(_ lhs: LHS) -> Bool {
let p = Wrapper<LHS>.self as? AnyEquatable.Type
return p?.isEqual(x, y) ?? false
}
return _openExistential(x, do: f)
}
@IanKeen
IanKeen / ShareReplay.swift
Created June 3, 2021 19:38
Combine: ShareReplay - Shares the underlying resource and replays the last emitted value (if there was one) to new subscribers
extension Publisher {
func shareReplay() -> AnyPublisher<Output, Failure> {
let subject = CurrentValueSubject<Output?, Failure>(nil)
return map { $0 }
.multicast(subject: subject)
.autoconnect()
.compactMap { $0 }
.eraseToAnyPublisher()
}