Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flattens any future into void #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rafiki270
Copy link

@rafiki270 rafiki270 commented Feb 20, 2018

using this in my app to transform any future to void easily and was wondering if you think it could be useful elsewhere ... obviously would add tests if so

Also, in my app I have the extension constrained to Content so it doesn't show on Future<Void> which would be pointless but not sure how to do this in pure async ...

@twof
Copy link
Contributor

twof commented Feb 20, 2018

Also, in my app I have the extension constrained to Content so it doesn't show on Future which would be pointless but not sure how to do this in pure async

Perhaps the extension would be better added to Vapor or Fluent in that case?

Mind sharing a code snippet where you're using the extension?

@rafiki270
Copy link
Author

rafiki270 commented Feb 20, 2018

@twof my version of this is:

extension Future where T: Content {
    
    public func flatten() -> Future<Void> {
        return map(to: Void.self, { (_) -> Void in
            return Void()
        })
    }
    
}

and using it in:

    static func handleTags(db: DbCoreConnection, request req: Request, app: App) -> Future<Void> {
        if let query = try? req.query.decode([String: String].self) {
            if let tags = query["tags"]?.split(separator: "|") {
                var futures: [Future<Void>] = []
                // TODO: Optimise to work without the for loop
                for tagSubstring in tags {
                    let tag = String(tagSubstring)
                    let future = Tag.query(on: db).filter(\Tag.identifier == tag).first().flatMap(to: Void.self, { (tagObject) -> Future<Void> in
                        guard let tagObject = tagObject else {
                            let t = Tag(id: nil, name: tag, identifier: tag.safeText)
                            return t.save(on: db).flatMap(to: Void.self, { (tag) -> Future<Void> in
                                return app.tags.attach(tag, on: db).flatten() // Extension used here
                            })
                        }
                        return app.tags.attach(tagObject, on: db).flatten() // and here
                    })
                    futures.append(future)
                }
                return futures.flatten() // Current async method
            }
        }
        return Future<Void>.make()
    }

also in stuff like:

return try app.delete(on: db).flatten().asResponse(to: req)

where the delete I think was still returning the deleted object? (not 100% sure) and I got a helper method that returns a response with 204 status code on empty void futures

@tanner0101 tanner0101 self-assigned this Feb 23, 2018
@tanner0101 tanner0101 added this to Backlog in Vapor 3 Feb 26, 2018
@tanner0101 tanner0101 removed this from Backlog in Vapor 3 Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants