Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created February 20, 2020 11:01
Show Gist options
  • Save anupamchugh/a671affc83558eee11797f36590e3fd1 to your computer and use it in GitHub Desktop.
Save anupamchugh/a671affc83558eee11797f36590e3fd1 to your computer and use it in GitHub Desktop.
struct FetchItem: Publisher {
typealias Output = StoryItem
typealias Failure = Error
let id: Int
func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input {
let request = URLRequest(url: URL(string: "https://hacker-news.firebaseio.com/v0/item/\(id).json")!)
URLSession.DataTaskPublisher(request: request, session: URLSession.shared)
.map { $0.0 }
.decode(type: StoryItem.self, decoder: JSONDecoder())
.receive(subscriber: subscriber)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment