Skip to content

Instantly share code, notes, and snippets.

@ndurell
Created November 27, 2018 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndurell/43231646cbf45ecfc2507ac7cfafa595 to your computer and use it in GitHub Desktop.
Save ndurell/43231646cbf45ecfc2507ac7cfafa595 to your computer and use it in GitHub Desktop.
point free caching github api example
let cachingGitHub = CachingGitHub()
struct GitHub {
var latestRepos: () -> [Repo] = { return cachingGitHub.latestRepos }
var fetchRepos: (@escaping (Result<[Repo], APIError>) -> Void) -> Void = cachingGitHub.fetchRepos
}
class CachingGitHub {
var latestRepos = [Repo]()
func fetchRepos(completion: @escaping (Result<[Repo], APIError>) -> Void) {
_fetchRepos { result in
switch result {
case .success(let repos):
self.latestRepos = repos
default:
break
}
completion(result)
}
}
}
static func _fetchRepos : (@escaping (Result<[Repo], APIError>) -> Void) -> Void {
...
}
private var latestRepos: [Repo] {
get { return latestReposQueue.sync { _latestRepos } }
set { latestReposQueue.async { _latestRepos = newValue } }
}
private let latestReposQueue = DispatchQueue(label: "latestRepos")
private var _latestRepos: [Repo] = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment