Fetcher.swift 986 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // Fetcher.swift
  3. // Haneke
  4. //
  5. // Created by Hermes Pique on 9/9/14.
  6. // Copyright (c) 2014 Haneke. All rights reserved.
  7. //
  8. import UIKit
  9. // See: http://stackoverflow.com/questions/25915306/generic-closure-in-protocol
  10. open class Fetcher<T : DataConvertible> {
  11. public let key: String
  12. public init(key: String) {
  13. self.key = key
  14. }
  15. open func fetch(failure fail: @escaping ((Error?) -> ()), success succeed: @escaping (T.Result) -> ()) {}
  16. open func cancelFetch() {}
  17. }
  18. class SimpleFetcher<T : DataConvertible> : Fetcher<T> {
  19. let getValue : () -> T.Result
  20. init(key: String, value getValue : @autoclosure @escaping () -> T.Result) {
  21. self.getValue = getValue
  22. super.init(key: key)
  23. }
  24. override func fetch(failure fail: @escaping ((Error?) -> ()), success succeed: @escaping (T.Result) -> ()) {
  25. let value = getValue()
  26. succeed(value)
  27. }
  28. override func cancelFetch() {}
  29. }