Skip to content

Instantly share code, notes, and snippets.

@rustle
Created October 28, 2019 19:25
Show Gist options
  • Save rustle/6a096a861ea97446d81d28fa008bd48e to your computer and use it in GitHub Desktop.
Save rustle/6a096a861ea97446d81d28fa008bd48e to your computer and use it in GitHub Desktop.
@propertyWrapper
struct ErasedToAnyPublisher<Upstream: Publisher> {
var wrappedValue: AnyPublisher<Upstream.Output, Upstream.Failure> {
upstream.eraseToAnyPublisher()
}
let upstream: Upstream
init(upstream: Upstream) {
self.upstream = upstream
}
}
struct Foo {
enum Error: Swift.Error {
case bar
}
...
@ErasedToAnyPublisher<String, Error> var publisher: AnyPublisher<String, Error>
init(a: A) {
_publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>())
...
}
init(b: B) {
_publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>())
...
}
func baz() {
_publisher.upstream.send("")
}
...
}
let foo = Foo(...)
foo.publisher.sink { print($0) } // publisher: AnyPublisher<String, Error>
// I'd be much happier if it was closer to
struct Foo {
enum Error: Swift.Error {
case bar
}
@ErasedToAnyPublisher var publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>())
...
init(a: A) {
...
}
init(b: B) {
...
}
func baz() {
_publisher.upstream.send("")
}
...
}
let foo = Foo(...)
foo.publisher.sink { print($0) } // publisher: AnyPublisher<String, Error>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment