Created
February 20, 2020 11:01
-
-
Save anupamchugh/a671affc83558eee11797f36590e3fd1 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
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