Code Monkey home page Code Monkey logo

eventual's Introduction

Eventual

Simple implementation of an async value in swift.

Inside your services

func getUserFromInternet() -> Eventual<User> {
  let r = Resolver<User>()
  
  networkClient.get("whatever") { json in
    // do your json -> domain object
    let user: User = userFromJSON(json)
    r.resolve(user)
  }
  
  return r.eventual
}

Consuming your services

let eventualUser = getUserFromInternet()
eventualUser.finally { user in 
  // now you have a user!
}

NOTE Eventual does not care about threading/queuing, so you need to manage where you do work. Handlers will be called from whatever context the Eventual is resolved in. Need to be on the main thread after getting a value? Use one of the wealth of options in Foundation.

You can also chain without losing type safety:

let nameEventual = eventualUser.then { user in user.firstName }
nameEventual.finally { name in print(name) }

Or chain with other async operations:

func getReposForUser(user: User) -> Eventual<[Repo]> { ... }

let eventualRepos = getUserFromInternet().then{ user in getReposForUser(user) }

Convenient functions

join:

join(Eventual("hi"), Eventual(23)).finally { tuple in
  print(tuple.0) // hi
  print(tuple.1) // 23
}

lift:

Convert functions from operating on concrete types to work on eventuals instead:

func add(lhs: Int, rhs: Int) -> Int {
    return lhs + rhs
}
// add is Int, Int -> Int
let liftedAdd = lift(add)
// liftedAdd is Eventual<Int>, Eventual<Int> -> Eventual<Int>
// the final value will be resolved as the addition of the two values once they resolve

UnsafeGetEventualValue:

This method is intended to help while testing, when you expect an Eventual to be resolved in a particular way. Use of this in production code is heavily discouraged as it sidesteps much of the point of the tool.

func testResolvesAfterWork() {
    let workDoer = WorkDoer()
    let eventual = workDoer.work()

    XCTAssertNil(UnsafeGetEventualValue(eventual))

    workDoer.finishWorking()

    XCTAssertEqual(UnsafeGetEventualValue(eventual), "work finished!")    
}

eventual's People

Contributors

zacclark avatar

Watchers

James Cloos avatar Nayshawn Danner avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.