Skip to content

crowe-alex/AHFuture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AHFuture

CI Status Version License Platform

AHFuture is a concepts of asynchronous api using idea of futures.

AHFuture implements proven functional concepts in Swift to provide a powerful alternative to completion blocks and support typesafe error handling in asynchronous code.

Example

We write a lot of asynchronous code. Whether we're waiting for something to come in from the network or want to perform an expensive calculation off the main thread and then update the UI, we often do the 'fire and callback' dance. Here's a typical snippet of asynchronous code:

User.logIn(username, password) { user, error in
    if !error {
        Posts.fetchPosts(user, success: { posts in
            // do something with the user's posts
        }, failure: handleError)
    } else {
        handleError(error) // handeError is a custom function to handle errors
    }
}

Now let's see what AHFuture can do for you:

User.logIn(username, password).flatMap { user in
    Posts.fetchPosts(user)
}.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

Supported operations:

map

If success will transfrom User response to UserViewModel that can be used in success block

User.logIn(username, password).map { UserViewModel.init }
.onSuccess { viewModel in
    // do something with viewModel
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

flatMap

If logIn succeed the response will be tranfromed to another AHFuture and the success block will contain result from the second AHFuture

User.logIn(username, password).flatMap { Posts.fetchPosts }
.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

filter

Will execute completion if predicate is true.

User.logIn(username, password).flatMap { Posts.fetchPosts }
			      .filter { PostFilter.isTodaysPost }
.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

retry

Retry operator will try to execute AHFuture if there is an error. The parametr shows the number of attempts

User.logIn(username, password).flatMap { Posts.fetchPosts }
			      .retry(5)
.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

recover

Recover operator helps transform error to a placeholder object.

User.logIn(username, password).flatMap { Posts.fetchPosts }
			      .retry(5)
                              .recover {ErrorHandler.transformToPlaceholderModel}
.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

run/observe

Sometimes we need to run our work on a particular thread and observe on another. For this purposes there is a run operator

Calculator.primeNumber(number)
.run(on: myGlobalQueue)
.observe(on: .main)
.onSuccess { posts in
    // do something with the user's posts
}.onFailure { error in
    // either logging in or fetching posts failed
}.execute()

Installation

AHFuture is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "AHFuture"

TODO

  • add timeout operator
  • add synchronize/concat/zip operators

Author

Alex Hmelevski, alexei.hmelevski@gmail.com

License

AHFuture is available under the MIT license. See the LICENSE file for more info.

About

Simple framework for asynchronous code in swift using Futures

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors