Created
June 4, 2020 09:45
-
-
Save MrSmart00/71479909beb6aa6dd60c3028c6086055 to your computer and use it in GitHub Desktop.
PublishRelay with Property Wrapper
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
import Foundation | |
import RxSwift | |
import RxCocoa | |
@propertyWrapper | |
struct WrappedPublishRelay<Value> { | |
private let value: AnyObserver<Value> | |
private let observable: Observable<Value> | |
init(_ relay: PublishRelay<Value> = .init()) { | |
value = .init { | |
guard case let .next(value) = $0 else { | |
return | |
} | |
relay.accept(value) | |
} | |
observable = relay.asObservable() | |
} | |
var wrappedValue: AnyObserver<Value> { | |
return value | |
} | |
var projectedValue: Observable<Value> { | |
return observable | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment