Skip to content

Instantly share code, notes, and snippets.

@kiarashvosough1999
Created June 30, 2022 19:04
Show Gist options
  • Save kiarashvosough1999/567fedb24dcad79690b1dbfe803d13fa to your computer and use it in GitHub Desktop.
Save kiarashvosough1999/567fedb24dcad79690b1dbfe803d13fa to your computer and use it in GitHub Desktop.
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Self.Failure == Never {
func assign(to failablePublished: CurrentValuePublished<Self.Output,Self.Failure>) -> AnyCancellable {
self.assign(to: \.wrappedValue, on: failablePublished)
}
}
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper
class CurrentValuePublished<Value,Failure> where Failure: Error {
var wrappedValue: Value {
get {
_subject.value
}
set {
_subject.send(newValue)
}
}
var subject: AnyPublisher<Value,Failure> {
_subject.eraseToAnyPublisher()
}
var projectedValue: CurrentValuePublished<Value,Failure> {
self
}
private var _subject: CurrentValueSubject<Value,Failure>
init(wrappedValue: Value, ErrorType: Failure.Type) {
self._subject = .init(wrappedValue)
}
init(wrappedValue: Value) where Failure == Never {
self._subject = .init(wrappedValue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment