Skip to content

Releases: 0xLet/Later

0.4.1

03 Oct 01:10
3ec8bc9
Compare
Choose a tag to compare

Later+EventLoopFuture

02 Oct 23:07
e2d6c2e
Compare
Choose a tag to compare
static func andAllComplete<Value>(_ futures: [EventLoopFuture<Value>]) -> EventLoopFuture<Void>
static func andAllSucceed<Value>(_ futures: [EventLoopFuture<Value>]) -> EventLoopFuture<Void>
static func whenAllComplete<Value>(_ futures: [EventLoopFuture<Value>]) -> EventLoopFuture<[Result<Value, Error>]> 
static func whenAllSucceed<Value>(_ futures: [EventLoopFuture<Value>]) -> EventLoopFuture<[Value]>

Example Usage

.navigationBarItems(
    leading: Button("Delete All") {
        Later.whenAllSucceed(
            items.map {
                store.persist.delete(model: $0) // EventLoopFuture<Void>
            } // [EventLoopFuture<Void>]
        )// EventLoopFuture<[Void]>
        .flatMap { _ in store.persist.all(model: Planet.self) }
        .whenSuccess { items = $0 }
    },
    trailing: Button("100") {
        Later.whenAllSucceed(
            (0 ... 99).map { _ in
                store.persist.add(model: Planet.random) // EventLoopFuture<Void>
            } // [EventLoopFuture<Void>]
        ) // EventLoopFuture<[Void]>
        .flatMap { _ in store.persist.all(model: Planet.self) }
        .whenSuccess { items = $0 }
    }
)

Fixed Contract resign

31 Aug 16:09
cb567dd
Compare
Choose a tag to compare
Merge pull request #8 from 0xLeif/develop

Resign promise with ContractError.resigned

Contracts

25 Aug 19:25
7f3b2e2
Compare
Choose a tag to compare

Later.Contracts

Contract Spec

public class Contract<Value> {
    public init(initialValue: Value? = nil,
                onResignHandler: ((Value?) -> Void)? = nil,
                onChangeHandler: ((Value?) -> Void)? = nil) {
        onResign = onResignHandler
        onChange = onChangeHandler
        value = initialValue
        start()
        promise?.succeed(value)
    }
}

Example Usage

let label = Label("❗️👀")

let textContract = Contract(initialValue: "Hello, World!") { value in
    Later.main {
        label.text = value
    }
}
.onResign { lastValue in
    Later.main {
        label.text = "Contract was Resigned\nLast Value: \(lastValue ?? "-1")"
    }
}

print(label.text) // Outputs: "Hello, World!"

main -> LaterValue<T>

24 Aug 23:47
12a3b6d
Compare
Choose a tag to compare
func main<T>(withDelay delay: UInt32 = 0,
                     work: @escaping () throws -> T) -> LaterValue<T> 

Version 0.2.0

13 Aug 22:31
9fce265
Compare
Choose a tag to compare

Updated do and main to take in a closure that can throw and error.

func `do`<T>(withDelay delay: UInt32 = 0,
                        work: @escaping () throws -> T) -> LaterValue<T> 
    
func `do`(withDelay delay: UInt32 = 0,
                     work: @escaping () throws -> Void) -> LaterValue<Void> 

func main(withDelay delay: UInt32 = 0,
                     work: @escaping () throws -> Void) -> LaterValue<Void> 

Calls that provide a failure response

  • do
  • main
  • post

Later/ Tests now support Linux.

Version 0.1.0

10 Aug 12:32
3bf693f
Compare
Choose a tag to compare

Later.Methods

promise

promise<T>(work: @escaping (LaterPromise<T>) -> Void) -> LaterValue<T>
    
promise(work: @escaping (LaterPromise<Void>) -> Void) -> LaterValue<Void>

do

do<T>(withDelay delay: UInt32 = 0,
      work: @escaping () -> T) -> LaterValue<T>

do(withDelay delay: UInt32 = 0,
   work: @escaping () -> Void) -> LaterValue<Void>

schedule

scheduleRepeatedTask(initialDelay: TimeAmount = TimeAmount.seconds(0),
                    delay: TimeAmount = TimeAmount.seconds(0),
                    work: @escaping (RepeatedScheduledTask) -> Void) -> RepeatedScheduledTask
                    
scheduleTask(in time: TimeAmount = TimeAmount.seconds(0),
            work: @escaping () -> Void) -> ScheduledTask<Void>

main

main(withDelay delay: UInt32 = 0,
     work: @escaping () -> Void) -> LaterValue<Void>

fetch

fetch(url: URL) -> LaterValue<(Data?, URLResponse?, Error?)>

fetch(url: URL,
      work: @escaping (Data?, URLResponse?, Error?) -> Void) -> LaterValue<Void>

fetch(url: URL,
      errorHandler: ((Error) -> Void)? = nil,
      responseHandler: ((URLResponse) -> Void)? = nil,
      dataHandler: ((Data) -> Void)? = nil) -> LaterValue<Void>

post

/// ["Content-Type": "application/json; charset=utf-8"]
post(url: URL, withData data: () -> Data) -> LaterValue<(Data?, URLResponse?, Error?)>

LaterValue

and: Later.Type { Later.self }
when(value: @escaping (LaterValue<Value>) -> Void) -> Later.Type

Examples

promise

let promise = Later.promise { (promise) in
    sleep(10)
    promise.succeed(())
}

promise.whenSuccess { page in
    print("Page received")
}

promise.whenFailure { error in
    print("Error: \(error)")
}

do

Later.do(withDelay: 2) {
    work()
}

schedule

let task = Later.scheduleRepeatedTask(initialDelay: .seconds(0),
               delay: .seconds(1)) { (task) in
               work()
}

Later.scheduleTask(in: .seconds(3)) {
    task.cancel()
}

main

Later.main { 
    // Update UI
    self.value = "Fetching Again..." 
}

fetch

Later.fetch(url: URL(string: "https://jsonplaceholder.typicode.com/todos/1")!)
    .whenSuccess { (data, response, error) in
    guard let data = data else {
        return
    }
    
    self.value = String(data: data, encoding: .utf8) ?? "-1"
}

post

Later.post(url: URL(string: "https://postman-echo.com/post")!) {
    "Some Data".data(using: .utf8)!
}
.when { event in
    event
        .whenSuccess { (data, reponse, _) in
            print(String(data: data!, encoding: .utf8) ?? "-1")
            print(reponse)
    }
}

and

Later
    .do { print("Do Something") }
    .and
    .do { print("Do Something Else") }

when

Later
    .do { print("Do First") }
    .when { event in
        event
            .whenComplete { _ in
                print("Do Last")
        }
    }
    .do { print("Do Something Else") }

promise + fetch

private lazy var futureImage: LaterValue<UIImage?> = Later.promise { (promise) in
    URL(string: self.imageURL).map { url in
        Later.fetch(url: url) { data in
            guard let image = UIImage(data: data) else {
                promise.succeed(nil)
                return
            }
            promise.succeed(image)
        }
    }
}