Skip to content

Commit

Permalink
Merge pull request #13 from thefuntasty/housekeep/promise-creation
Browse files Browse the repository at this point in the history
Housekeep: Use pending promise creation in extension
  • Loading branch information
mkj-is authored Feb 11, 2019
2 parents e1d36fe + d948368 commit a6f59c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 56 deletions.
32 changes: 16 additions & 16 deletions Extensions/PromiseKit/APIAdapter+PromiseKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ import PromiseKit

extension APIAdapter {
public func request<Endpoint: APIResponseEndpoint>(response endpoint: Endpoint) -> Promise<Endpoint.Response> {
return Promise { resolver in
request(response: endpoint) { result in
switch result {
case .value(let value):
resolver.fulfill(value)
case .error(let error):
resolver.reject(error)
}
let (promise, seal) = Promise<Endpoint.Response>.pending()
request(response: endpoint) { result in
switch result {
case .value(let value):
seal.fulfill(value)
case .error(let error):
seal.reject(error)
}
}
return promise
}

public func request(data endpoint: APIEndpoint) -> Promise<Data> {
return Promise { resolver in
request(data: endpoint) { result in
switch result {
case .value(let value):
resolver.fulfill(value)
case .error(let error):
resolver.reject(error)
}
let (promise, seal) = Promise<Data>.pending()
request(data: endpoint) { result in
switch result {
case .value(let value):
seal.fulfill(value)
case .error(let error):
seal.reject(error)
}
}
return promise
}
}
68 changes: 28 additions & 40 deletions Extensions/PromiseKit/URLSessionAPIAdapter+PromiseKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,39 @@ public struct APIDataTask<T> {

extension URLSessionAPIAdapter {
public func dataTask<Endpoint: APIResponseEndpoint>(response endpoint: Endpoint) -> APIDataTask<Endpoint.Response> {
var taskResolver: ((URLSessionTask?) -> Void)?
let taskPromise = Guarantee<URLSessionTask?> { resolver in
taskResolver = resolver
}
let task = Guarantee<URLSessionTask?>.pending()
let response = Promise<Endpoint.Response>.pending()

let responsePromise = Promise<Endpoint.Response> { resolver in
dataTask(response: endpoint, creation: { dataTask in
taskResolver?(dataTask)
}, completion: { result in
if taskPromise.isPending {
taskResolver?(nil)
}
switch result {
case .value(let value):
resolver.fulfill(value)
case .error(let error):
resolver.reject(error)
}
})
}
return APIDataTask(sessionTask: taskPromise, response: responsePromise)
dataTask(response: endpoint, creation: task.resolve, completion: { result in
if task.guarantee.isPending {
task.resolve(nil)
}
switch result {
case .value(let value):
response.resolver.fulfill(value)
case .error(let error):
response.resolver.reject(error)
}
})
return APIDataTask(sessionTask: task.guarantee, response: response.promise)
}

public func dataTask(data endpoint: APIEndpoint) -> APIDataTask<Data> {
var taskResolver: ((URLSessionTask?) -> Void)?
let taskPromise = Guarantee<URLSessionTask?> { resolver in
taskResolver = resolver
}
let task = Guarantee<URLSessionTask?>.pending()
let response = Promise<Data>.pending()

let responsePromise = Promise<Data> { resolver in
dataTask(data: endpoint, creation: { dataTask in
taskResolver?(dataTask)
}, completion: { result in
if taskPromise.isPending {
taskResolver?(nil)
}
switch result {
case .value(let value):
resolver.fulfill(value)
case .error(let error):
resolver.reject(error)
}
})
}
return APIDataTask(sessionTask: taskPromise, response: responsePromise)
dataTask(data: endpoint, creation: task.resolve, completion: { result in
if task.guarantee.isPending {
task.resolve(nil)
}
switch result {
case .value(let value):
response.resolver.fulfill(value)
case .error(let error):
response.resolver.reject(error)
}
})
return APIDataTask(sessionTask: task.guarantee, response: response.promise)
}
}

0 comments on commit a6f59c5

Please sign in to comment.