Skip to content

Instantly share code, notes, and snippets.

@binhapp
Last active March 15, 2018 14:30
Show Gist options
  • Save binhapp/2c30b58d99197ddb99b04b4f05bc9a75 to your computer and use it in GitHub Desktop.
Save binhapp/2c30b58d99197ddb99b04b4f05bc9a75 to your computer and use it in GitHub Desktop.
protocol LoadProtocol {
  func beginLoad()
  func endLoad()
}

extension LoadProtocol {
  func load<T>(_ action: T, _ perform: (T, @escaping () -> Void) -> Void) {
    beginLoad()
    perform(action, endLoad)
  }
}

Example

struct Loading: LoadProtocol {
  func beginLoad() {
    print("Show loading")
  }
  
  func endLoad() {
    print("Hide loading")
  }
  
  func loginApi(name: String, completion: @escaping (Bool) -> Void) {
    print("Call api login")
    completion(name == "blcsntb")
  }
  
  func test() {
    load(loginApi) { (login, fulfill) in
      login("blcsntb") { success in
        print("result:", success)
        fulfill()
      }
    }
  }
}

let loading = Loading()
loading.test()

Console

Show loading
Call api login
result: true
Hide loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment