izqui / Taylor

A lightweight library for writing HTTP web servers with Swift
MIT License
926 stars 79 forks source link

[Suggestion] If the continue 'Callback' is called last, send it as a response anyway. #12

Open Danappelxx opened 8 years ago

Danappelxx commented 8 years ago

Basically if I were to have this:

mast.server.get("/") { req, res, cb in
            res.bodyString = "Hello!"
            cb(.Continue(req,res))
}

currently, Taylor would return a not found page. I propose adding detection which correct the .Continue to .Send if its the last one.

perezpaya commented 8 years ago

@Danappelxx I'll have a look into it! Thanks man!

izqui commented 8 years ago

The problem with this right now, is that as we rely on the callback in order to make it async, the only way to do that would be to have some kind of timeout for each handler.

Thinking a lot about whether the callback thing is worth it vs just returning the request and response at the end of the closure.

typealias Handler = (Request, Response) -> (Request, Response)

or even

typealias Handler = (inout Request, inout Response) -> Void

but I'm not very confident with the last one

perezpaya commented 8 years ago

I belive we could try with something like this screen shot 2015-09-30 at 11 22 14 Tell me what do you think about it

Danappelxx commented 8 years ago

The timeout solution seems really dirty to me since it would create a delay if a continue is indeed the last callback, and it would also get in the way if you have a long-running operation.

Perhaps you could use promises internally? I know promisekit has a useful method called when that waits for a set of promises to complete and afterwards performs an action. The example they provide is:

let search1 = MKLocalSearch(request: rq1).promise()
let search2 = MKLocalSearch(request: rq2).promise()

when(search1, search2).then { response1, response2 in
    //...
}

I don't think it would be too hard to port it to fit Taylor's needs.

Danappelxx commented 8 years ago

Alright - if nobody else will I'll play around with this when I can find the time :P