Skip to content

Instantly share code, notes, and snippets.

@soxjke
Created February 9, 2020 19:16
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 soxjke/71e374302f4737f88250a38424a75b13 to your computer and use it in GitHub Desktop.
Save soxjke/71e374302f4737f88250a38424a75b13 to your computer and use it in GitHub Desktop.
private(set) var searchTermThrottle = Throttler<String>(1.5)
override func viewDidLoad() {
super.viewDidLoad()
searchField.addTarget(self, action: #selector(textDidChange(_:)), for: .editingChanged)
searchTermThrottle.on { (searchTerm) in
GithubAPI.search(term: searchTerm) { [weak self] result in
switch (result) {
case .success(let models): self?.onSuccess(models)
case .failure(let error): self?.onError(error)
}
}
}
}
@objc func textDidChange(_ sender: UITextField) {
searchTermThrottle.receive(sender.text ?? "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment