Created
June 30, 2022 19:04
-
-
Save kiarashvosough1999/567fedb24dcad79690b1dbfe803d13fa to your computer and use it in GitHub Desktop.
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
@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